Fix HTTP timeout enforcement and improve connection management#1795
Fix HTTP timeout enforcement and improve connection management#1795
Conversation
Co-authored-by: gcatanese <1771700+gcatanese@users.noreply.github.com>
Co-authored-by: gcatanese <1771700+gcatanese@users.noreply.github.com>
961f8b8 to
23f4c55
Compare
Issues identified and proposed changes1.
|
| # | File | Change |
|---|---|---|
| 1 | AdyenHttpClient.java |
Fix getOrCreateHttpClient to eliminate the close-vs-read race by returning sharedHttpClient from inside the synchronized block (removing the early-return on the local copy) |
| 2 | AdyenHttpClient.java |
Add @GuardedBy("lock") annotation comment to sharedHttpClient field |
| 3 | AdyenHttpClient.java |
Add a clarifying comment to setProxy() that SOCKS proxies are not supported |
| 4 | AdyenHttpClient.java |
Correct the Javadoc on createRequest to note that socketTimeout is fixed at creation time |
Only change #1 is a behavioral fix; the rest are documentation/annotation improvements.
This pull request introduces comprehensive improvements to HTTP timeout configuration and connection pool management.
Root Cause
The
AdyenHttpClientwas only setting timeouts onRequestConfig(HTTP-level), but not onConnectionConfig(socket-level). When a server accepted the TCP connection but stalled at the socket/TLS layer, theHTTP-level responseTimeout alone was insufficient to terminate the request. Additionally, a new
CloseableHttpClientwas created and destroyed for every request, defeating connection pooling entirely.This originated the issue reported in #1794
Changes
Timeout enforcement
• Added
ConnectionConfigwithsocketTimeoutandconnectTimeouton thePoolingHttpClientConnectionManager, enforcing hard OS-level timeouts on socket reads and TCP/TLS handshakes.• Moved connectTimeout from the deprecated
RequestConfig.setConnectTimeout()to the properConnectionConfig.setConnectTimeout().• Added
defaultKeepAliveto the client-levelRequestConfigfor consistency.Shared HTTP client
• Refactored
AdyenHttpClientfrom creating a newCloseableHttpClientper request to lazily initializing and reusing a single shared instance (double-checked locking for thread safety).•
Client.getHttpClient()now stores and reuses the sameAdyenHttpClientinstance.Lifecycle management
•
ClientInterfacenow extendsCloseablewith a defaultno-op close()for backward compatibility with custom implementations.•
ClientimplementsCloseable, delegating to the underlying HTTP client for proper connection pool cleanup.Documentation
• Improved README by adding "HTTP timeout configuration" section with default values and recommended best practises.
Fix #1794