[feat] - Optimize HTTP Client for Detectors #2805
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This PR introduces optimizations to the HTTP client used by detectors to improve connection reuse. It also updates all the detectors to utilize the new approach when setting up their HTTP clients. The main changes include:
WithDetectorTransport
function that returns aTransportOption
. This function sets the desired transport settings for detectors, such as:Timeout
to 5 secondsKeepAlive
to 30 secondsMaxIdleConns
to 100MaxIdleConnsPerHost
to 10IdleConnTimeout
to 90 secondsI would appreciate feedback on these settings, as I'm not familiar with the ideal values. I found these online. 😅 They seemed ok.
Modified the
SaneHttpClient
function to accept a variadic parameteropts
of type...TransportOption
. This allows callers to pass in optional transport options.Updated the
SaneHttpClient
function to apply the provided transport options to thehttp.Transport
before creating thehttp.Client
.Updated all the detectors to use the new approach when setting up their HTTP clients. Instead of directly calling
SaneHttpClient()
, the detectors now callSaneHttpClient(WithDetectorTransport())
to create an HTTP client with the optimized transport settings specific to detectors.Why:
During the analysis of CPU profiles, it was observed that a significant amount of time was being spent in the TLS handshake phase, specifically in the
crypto/tls.(*Conn).clientHanshake
function. This indicated that there was room for optimization in how the HTTP client handled TLS connections, particularly in scenarios with a high volume of concurrent requests, such as in the case of our detectors.By introducing detector-specific transport settings and updating the detectors to use the optimized HTTP client, we aim to reduce the overhead associated with the TLS handshake. The increased timeouts, keep-alive duration, and maximum idle connections allow for better connection reuse and more efficient handling of concurrent requests.
These changes address the observed performance bottleneck in the TLS handshake and provide a more optimized HTTP client configuration for the specific needs of detectors while maintaining compatibility with the existing codebase.
Checklist:
make test-community
)?make lint
this requires golangci-lint)?