编程日记-httpclient的导致的故障

故障很多时候都是来源于你的不小心

周五给负责的一个EPush推送平台发布的一个版本排查问题。现象就是同事刚发布完线上,几分钟就出现浏览器端连不上EPush推送服务,出现502错误。当时第一件事就是让同事把代码立马回滚到master。我们是采用分之发布的方式,EPush平台负责去哪儿网商家订单的实时推送。回滚以后推送服务正常。定位就是新上代码的问题。所以开始扒拉代码。EPush采用两种认证方式一种就是cookie,另外一种就是API接口回调的方式。
我们先来看看一段API回调代码。其实这次需求就是需要在请求头上带上uid。

历史代码

<pre><code>
private boolean authApi(String app, String cookieStr, String authRule, HandshakeData handshakeData) {
if (StringUtils.isNotEmpty(authRule)) {
List<String> list = Lists.newArrayList(Splitter.on(",").trimResults().split(authRule));
if (list.size() >= 3) {
String apiUrl = list.get(0);
String[] urls = handshakeData.getUrl().split("\?");
if (urls.length > 1) {
// 如果定义的回调链接中已经有参数,拼接起来
if (apiUrl.contains("?")) {

                    apiUrl = apiUrl + "&" + urls[1];
                } else {
                    apiUrl = apiUrl + "?" + urls[1];
                }
            }
            HttpPost post = new HttpPost(apiUrl);
            post.setHeader("Cookie", cookieStr);
            HttpEntity entity = null;
            String result = null;
            try {
                CloseableHttpResponse response = httpClient.execute(post);
                StatusLine status = response.getStatusLine();
                if ((status != null) && (status.getStatusCode() == 200)) {
                    entity = response.getEntity();
                    result = EntityUtils.toString(entity, "UTF-8");
                } else {
                    logger.warn("[EPush]连接权限校验时返回错误,app={},apiUrl={},status={}", app, apiUrl,
                            status != null ? status.getStatusCode() : "");
                }
            } catch (Exception e) {
                logger.error("[EPush]连接权限校验时发生错误,app={},apiUrl={}", app, apiUrl, e);
            } finally {
                try {
                    EntityUtils.consume(entity);
                } catch (IOException e) {
                    logger.error("", e);
                }
            }
            if (StringUtils.isNotEmpty(result)) {
                JSONObject jo = JSON.parseObject(result);
                String authResult = jo.getString(list.get(1));
                if (list.get(2).equalsIgnoreCase(authResult)) {
                    return true;
                }
            }
            logger.warn("[EPush]auth fail, app={},apiUrl={},authResult={}", app, apiUrl, result);
        }
    }
    logger.info("[EPush]auth fail, app={}, cookie={}", app, cookieStr);
    return false;
}

</code></pre>

新修改的故障代码

<pre>
<code>
private boolean authApi(String app, String cookieStr, String authRule, HandshakeData handshakeData) {
if (StringUtils.isNotEmpty(authRule)) {
List<String> list = Lists.newArrayList(Splitter.on(",").trimResults().split(authRule));
if (list.size() > 0) {
String apiUrl = list.get(0);
String[] urls = handshakeData.getUrl().split("\?");
if (urls.length > 1) {
// 如果定义的回调链接中已经有参数,拼接起来
if (apiUrl.contains("?")) {
apiUrl = apiUrl + "&" + urls[1];
} else {
apiUrl = apiUrl + "?" + urls[1];
}
}
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Cookie", cookieStr);
CloseableHttpResponse response = null;
HttpEntity entity = null;
String result = null;
try {
response = httpClient.execute(post);
StatusLine status = response.getStatusLine();
if ((status != null) && (status.getStatusCode() == 200)) {
Header header = response.getFirstHeader(EpushContants.EPUSH_UID);
String uid = header != null ? header.getValue() : "";
handshakeData.getHeaders().put(EpushContants.EPUSH_UID, Lists.newArrayList(uid));
return true;
} else {
logger.warn("[EPush]连接权限校验时返回错误,app={},apiUrl={},status={}", app, apiUrl,
status != null ? status.getStatusCode() : "");
}
} catch (Exception e) {
logger.error("[EPush]连接权限校验时发生错误,app={},apiUrl={}", app, apiUrl, e);
} finally {
try {
EntityUtils.consume(entity);
if (response != null) {
response.close();
}
} catch (IOException e) {
logger.error("", e);
}
}
logger.warn("[EPush]auth fail, app={},apiUrl={},authResult={}", app, apiUrl, result);
}
}
logger.info("[EPush]auth fail, app={}, cookie={}", app, cookieStr);
return false;
}
</code>
</pre>

排除问题的正常代码

<pre><code>
private boolean authApi(String app, String cookieStr, String authRule, HandshakeData handshakeData) {
if (StringUtils.isNotEmpty(authRule)) {
List<String> list = Lists.newArrayList(Splitter.on(",").trimResults().split(authRule));
if (list.size() > 0) {
String apiUrl = list.get(0);
String[] urls = handshakeData.getUrl().split("\?");
if (urls.length > 1) {
// 如果定义的回调链接中已经有参数,拼接起来
if (apiUrl.contains("?")) {
apiUrl = apiUrl + "&" + urls[1];
} else {
apiUrl = apiUrl + "?" + urls[1];
}
}
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Cookie", cookieStr);
CloseableHttpResponse response = null;
HttpEntity entity = null;
String result = null;
try {
response = httpClient.execute(post);
StatusLine status = response.getStatusLine();
if ((status != null) && (status.getStatusCode() == 200)) {
entity = response.getEntity();
Header header = response.getFirstHeader(EpushContants.EPUSH_UID);
String uid = header != null ? header.getValue() : "";
handshakeData.getHeaders().put(EpushContants.EPUSH_UID, Lists.newArrayList(uid));
return true;
} else {
logger.warn("[EPush]连接权限校验时返回错误,app={},apiUrl={},status={}", app, apiUrl,
status != null ? status.getStatusCode() : "");
}
} catch (Exception e) {
logger.error("[EPush]连接权限校验时发生错误,app={},apiUrl={}", app, apiUrl, e);
} finally {
try {
EntityUtils.consume(entity);
if (response != null) {
response.close();
}
} catch (IOException e) {
logger.error("", e);
}
}
logger.warn("[EPush]auth fail, app={},apiUrl={},authResult={}", app, apiUrl, result);
}
}
logger.info("[EPush]auth fail, app={}, cookie={}", app, cookieStr);
return false;
}
</code></pre>
其实经过这三段代码的对比相信大家也看到了问题的所在。同事在上线新功能的时候将获取httpEntity实例的代码删除了,导致http连接的entity没有被consume。上线以后http连接一直没有释放。导致后面的连接服务失败。所以大家在习惯代码的时候需要注意原来代码的每一项逻辑。这段代码,其实主要是粗心的认为entity没有在try方法里面使用导致的。我们在修改代码时候尽可能的扩大编程影响范围。最最起码得知道这个方法体中的所有处理逻辑。避免这种代码级别的故障。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容