sunqi created HTTPASYNC-98:
------------------------------
Summary: http request throw SocketTimeoutException in 1 ms
Key: HTTPASYNC-98
URL: https://issues.apache.org/jira/browse/HTTPASYNC-98
Project: HttpComponents HttpAsyncClient
Issue Type: Bug
Affects Versions: Future
Environment: httpasyncclient-4.0.2.jar
httpclient-4.3.2.jar
httpcore-4.3.2.jar
httpcore-nio-4.3.2.jar
jdk 1.7.0_75
tomcat 7
Reporter: sunqi
Priority: Critical
use demo as this :
package com.taobao.amdc.web.servlet;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.nio.reactor.IOReactorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TimeOutServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private CloseableHttpAsyncClient httpAsyncClient;
private String healthCheckPath = "/status.taobao";
private int port = 80;
private static final Logger logger = LoggerFactory
.getLogger(TimeOutServlet.class);
@Override
public void init() throws ServletException {
IOReactorConfig ioReactorConfig =
IOReactorConfig
.custom()
.setIoThreadCount(
2)
.setSoKeepAlive(true)
.build();
ConnectingIOReactor ioReactor = null;
try {
ioReactor = new
DefaultConnectingIOReactor(
ioReactorConfig);
} catch (IOReactorException e) {
e.printStackTrace();
}
PoolingNHttpClientConnectionManager connManager
= new PoolingNHttpClientConnectionManager(
ioReactor);
connManager.setMaxTotal(2048);
connManager.setDefaultMaxPerRoute(100);
if (connManager != null) {
httpAsyncClient = HttpAsyncClients.custom()
.setConnectionManager(connManager).build();
httpAsyncClient.start();
}
}
private static String ips[]={
"1.1.1.1",
"10.125.197.132",
"2.2.2.2",
"10.218.128.9",
"3.3.3.3",
"4.4.4.4"
};
private static String workIps[]={
"100.81.0.149",
"10.218.134.29",
"10.218.129.215",
"10.101.97.141"
};
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try {
AtomicInteger count=new AtomicInteger(0);
CountDownLatch cdl=new
CountDownLatch(ips.length);
for(String ip:ips){
HttpGet h=buildHttpGet(ip);
httpAsyncClient.execute(h, new
CallBackHandler(h,cdl,count));
}
cdl.await(3000,TimeUnit.MILLISECONDS);
CountDownLatch cdl1=new
CountDownLatch(workIps.length);
for(String ip:workIps){
HttpGet h=buildHttpGet(ip);
httpAsyncClient.execute(h, new
CallBackHandler(h,cdl1,count));
}
cdl1.await(3000,TimeUnit.MILLISECONDS);
res.getWriter().println("total ip:
"+(ips.length+workIps.length)+" , success : "+count);
} catch (Exception e) {
}
}
private HttpGet buildHttpGet(String ip) {
try {
URI uri = new URIBuilder().setScheme("http").setHost(ip)
.setPort(port).setPath(healthCheckPath).build();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(1000)
.setConnectTimeout(500)
.setConnectionRequestTimeout(500)
.setStaleConnectionCheckEnabled(false)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.build();
HttpGet httpGet = new HttpGet();
httpGet.setURI(uri);
httpGet.setConfig(requestConfig);
return httpGet;
} catch (Exception t) {
logger.error(t.getMessage(), t);
}
return null;
}
class CallBackHandler implements FutureCallback<HttpResponse> {
private long t;
private HttpGet h;
private CountDownLatch cdl;
private AtomicInteger count;
public CallBackHandler(HttpGet h,CountDownLatch
cdl,AtomicInteger count) {
this.cdl=cdl;
this.h=h;
this.t=System.currentTimeMillis();
this.count=count;
}
@Override
public void completed(HttpResponse result) {
cdl.countDown();
if (null == result || null == result.getStatusLine()
|| HttpStatus.SC_OK !=
result.getStatusLine().getStatusCode()) {
logger.error(h.toString()+" http response is
null or statusCode is not 200");
} else{
count.incrementAndGet();
}
}
@Override
public void failed(Exception ex) {
cdl.countDown();
logger.error("request is failed: " + h.toString()+"
time used : "+(System.currentTimeMillis()-t)+" ms", ex);
}
@Override
public void cancelled() {
cdl.countDown();
logger.error("request is canceled {} ", h.toString());
}
}
}
the ip 1.1.1.1,2.2.2.2,3.3.3.3,4.4.4.4 is can't connected,and other ip is work,
run many times ,we well find log as this :
2015-12-31 15:51:21 [ - ] UserAgent: ERROR servlet.TimeOutServlet - request
is failed: GET http://4.4.4.4:80/status.taobao HTTP/1.1 time used : 1011 ms
java.net.SocketTimeoutException
at
org.apache.http.nio.pool.RouteSpecificPool.timeout(RouteSpecificPool.java:170)
at
org.apache.http.nio.pool.AbstractNIOConnPool.requestTimeout(AbstractNIOConnPool.java:546)
at
org.apache.http.nio.pool.AbstractNIOConnPool$InternalSessionRequestCallback.timeout(AbstractNIOConnPool.java:779)
at
org.apache.http.impl.nio.reactor.SessionRequestImpl.timeout(SessionRequestImpl.java:177)
at
org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processTimeouts(DefaultConnectingIOReactor.java:212)
at
org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:157)
at
org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:348)
at
org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:189)
at
org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.doExecute(CloseableHttpAsyncClientBase.java:67)
at
org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.access$000(CloseableHttpAsyncClientBase.java:38)
at
org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:57)
at java.lang.Thread.run(Thread.java:745)
2015-12-31 15:51:21 [ - ] UserAgent: ERROR servlet.TimeOutServlet - request
is failed: GET http://100.81.0.149:80/status.taobao HTTP/1.1 time used : 1 ms
java.net.SocketTimeoutException
at
org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:285)
at
org.apache.http.impl.nio.client.LoggingAsyncRequestExecutor.timeout(LoggingAsyncRequestExecutor.java:120)
at
org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:84)
at
org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:37)
at
org.apache.http.impl.nio.reactor.AbstractIODispatch.timeout(AbstractIODispatch.java:172)
at
org.apache.http.impl.nio.reactor.BaseIOReactor.sessionTimedOut(BaseIOReactor.java:255)
at
org.apache.http.impl.nio.reactor.AbstractIOReactor.timeoutCheck(AbstractIOReactor.java:491)
at
org.apache.http.impl.nio.reactor.BaseIOReactor.validate(BaseIOReactor.java:205)
at
org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:281)
at
org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
at
org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
at java.lang.Thread.run(Thread.java:745)
2015-12-31 15:51:21 [ - ] UserAgent: ERROR servlet.TimeOutServlet - request
is failed: GET http://10.218.129.215:80/status.taobao HTTP/1.1 time used : 1 ms
java.net.SocketTimeoutException
at
org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:285)
at
org.apache.http.impl.nio.client.LoggingAsyncRequestExecutor.timeout(LoggingAsyncRequestExecutor.java:120)
at
org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:84)
at
org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:37)
at
org.apache.http.impl.nio.reactor.AbstractIODispatch.timeout(AbstractIODispatch.java:172)
at
org.apache.http.impl.nio.reactor.BaseIOReactor.sessionTimedOut(BaseIOReactor.java:255)
at
org.apache.http.impl.nio.reactor.AbstractIOReactor.timeoutCheck(AbstractIOReactor.java:491)
the work ip request throw java.net.SocketTimeoutException in 1ms
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]