java.net.SocketException: No buf

2021-11-16  本文已影响0人  wyn_做自己

今日份鸡汤:没有一劳永逸的开始,也没有无法承受的结束。人生中,你需要把握的是:该开始的,要义无反顾地开始;该结束的。就干净利落地结束。

问题:
在写一个小工具获取线上有问题的数据,大概查询接口2W多次,本来没有用多线程跑,整体数据整合出来比较慢,就想着使用线程池跑来优化一下,谁成想,跑着跑着控制台就开始报这个错误了。

 Caused by: java.net.SocketException: No buffer space available (maximum connections reached?): connect
    at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)

问题分析:
错误大概的意思就是:机器端口号用尽导致tcp无法申请新的端口去建立连接。初步估计可能是客户端每次去请求服务端都创建了一次tcp连接,使用完就关闭连接,但是tcp协议中如果客户端主动关闭,为了确保发送的报文不影响下一次连接,主动关闭的一方需要停留在TIME_WAIT状态2*MSL(Max Segment Lifetime)。当并发高的时候,就会产生大量TIME_WAIT,而处于TIME_WAIT状态的端口是不能马上去建立新的连接的,这就导致No buffer space available (maximum connections reached?)这个异常。

整一个小demo吧,更直观一些。

public class SocketExceptionDemo{

    public static String myTest() throws IOException {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://www.hehe.com/")
                .get()
                .addHeader("cache-control", "no-cache")
                .addHeader("postman-token", "87803954-393f-67a8-18d6-5c8d16ac106d")
                .build();

        Response response = client.newCall(request).execute();

        return response.body().string();
    }

    public static void main(String[] args) {
        final ExecutorService FIXED_THREAD_POOL = new ThreadPoolExecutor(20, 80, 60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                new ThreadPoolExecutor.DiscardOldestPolicy());

        for (int i = 0; i < 23000; i++) {
            FIXED_THREAD_POOL.execute(() -> {
                try {
                    myTest();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}

就是这样一段的代码,然后问题主要就是出现在:OkHttpClient client = new OkHttpClient();每次都创建一个OkHttpClient 。相当于每次都要重新建立tcp连接,完成3次握手和4次挥手。

问题解决:
使用连接池,服用重复的连接。这样不必每次都去建立新的tcp连接。不仅可以避免端口被占满,也可以提升io的性能(tcp连接建立和关闭是需要开销的)。

将OkHttpClient client = new OkHttpClient();替换为如下即可:
ConnectionPool pool = new ConnectionPool(1500,5L, TimeUnit.MINUTES);
OkHttpClient client = new OkHttpClient().newBuilder().connectionPool(pool).build();

上一篇 下一篇

猜你喜欢

热点阅读