How can I ensure that my HttpClient 4.1 does not leak sockets?

后端 未结 3 1886
孤街浪徒
孤街浪徒 2021-02-13 17:31

My server uses data from an internal web service to construct its response, on a per request basis. I\'m using Apache HttpClient 4.1 to make the requests. Each initial request

3条回答
  •  不思量自难忘°
    2021-02-13 18:25

    I've marked oleg's answer as correct, as it highlights an important usage point about HttpClient's connection pooling.

    To answer my specific original question, though, which was "Should I be trying to solve for 0 unused sockets or trying to maximize pooling?"

    Now that the pooling solution is in place and working correctly the application throughput has increased by about 150%. I attribute this to not having to renegotiate SSL and multiple handshakes, instead reusing persistent connections in accordance with HTTP 1.1.

    It is definitely worth working to utilize pooling as intended, rather than trying to hack around with calling ThreadSafeClientConnManager.shutdown() after each request etcetera. If, on the other hand, you were calling arbitrary hosts and not reusing routes the way I am you might easily find that it becomes necessary to do that sort of hackery, as the JVM might surprise you with the long life of CLOSE_WAIT designated sockets if you're not garbage collecting very often.

提交回复
热议问题