【发布时间】:2015-10-27 19:01:53
【问题描述】:
我需要达到 1000 个线程来同时发送 HTTP 请求。然而,在 Mac 上,我似乎遇到了一些限制,这导致 Apache HTTPClient 返回异常:
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://my.url:80: Connection reset
限制似乎在 200 到 300 个并发连接之间。
我创建了一个简单的应用程序,即使使用该应用程序也能重现该问题:
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class App implements Runnable
{
private static final int NUMBER_THREADS = 1000;
public static void main( String[] args )
{
Thread[] threads = new Thread[NUMBER_THREADS];
for(int i = 0; i < NUMBER_THREADS; i++)
{
threads[i] = new Thread(new App());
}
for(int i = 0; i < NUMBER_THREADS; i++)
{
threads[i].start();
}
}
public void run()
{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://my.url/test.html");
CloseableHttpResponse response = null;
try
{
System.out.println( System.nanoTime() + " " + Thread.currentThread().getName() + " started" );
response = httpclient.execute(httpget);
System.out.println( System.nanoTime() + " " + Thread.currentThread().getName() + " finished" );
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(response != null)
{
response.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
我更新了一些 Mac 设置,但没有任何帮助:
> sysctl -a | grep files
kern.maxfiles = 12288
kern.maxfilesperproc = 10240
kern.maxfiles: 12288
kern.maxfilesperproc: 10240
kern.num_files: 3202
> ulimit -n 10240
> ulimit -n
10240
> sysctl -a | grep somax
kern.ipc.somaxconn: 128
> sudo sysctl -w kern.ipc.somaxconn=2048
Password:
kern.ipc.somaxconn: 128 -> 2048
> sysctl -a | grep somax
kern.ipc.somaxconn: 2048
所以我想了解这个限制来自哪里? OsX / Apache HttpClient 本身?并且可以控制吗?
提前致谢。
【问题讨论】:
-
升级到最新的 MacOS 10.11/El Capitan 没有帮助
标签: java macos apache httpclient apache-httpclient-4.x