Avoid per-connect lock on shared Bootstrap options map (#2218)#2219
Open
pavel-ptashyts wants to merge 2 commits into
Open
Avoid per-connect lock on shared Bootstrap options map (#2218)#2219pavel-ptashyts wants to merge 2 commits into
pavel-ptashyts wants to merge 2 commits into
Conversation
…nt#2218) AHC reuses a single Bootstrap for every outbound connection. Netty's AbstractBootstrap.newOptionsArray() copies the shared options map under "synchronized (options)" on every connect, serializing all connection attempts on one monitor. Under high connection-establishment rates this becomes a lock convoy. Resolve the configured ChannelOptions once at construction into a fixed array and apply them to each channel from the existing channel initializer via Channel.config().setOption(...), leaving the Bootstrap options map empty. This removes the global lock from the connect path and avoids re-reading config per connection, while preserving identical option values, ordering, and timing (options are applied during channel registration, before connect).
…nt#2218) AHC reuses a single Bootstrap for every outbound connection. Netty's AbstractBootstrap.newOptionsArray() copies the shared options map under "synchronized (options)" on every connect, serializing all connection attempts on one monitor. Under high connection-establishment rates this becomes a lock convoy. Resolve the configured ChannelOptions once at construction into a fixed array and apply them to each channel from the existing channel initializer via Channel.config().setOption(...), leaving the Bootstrap options map empty. This removes the global lock from the connect path and avoids re-reading config per connection, while preserving identical option values, ordering, and timing (options are applied during channel registration, before connect).
Contributor
Author
|
Hi @hyperxpro could you check this PR. Thanks in advance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #2218.
AsyncHttpClient reuses a single
Bootstrapinstance for every outboundconnection. On each connect, Netty's
AbstractBootstrap.newOptionsArray()copies the shared options map under
synchronized (options):Because the same Bootstrap (and its options map) backs every connection,
this is a single global monitor that all connection attempts contend on.
Under high connection-establishment rates — many concurrent outbound
requests, short-lived connections with low reuse, or no effective per-host
connection cap — threads queue on this lock and a convoy forms.
Fix
Stop configuring options on the Bootstrap. Instead:
fixed array (buildChannelOptions). Conditional options (connect timeout,
SO_LINGER, send/receive buffers) and all config getters are evaluated a
single time rather than per connection.
initializer via the public Channel.config().setOption(...) API
(applyChannelOptions), leaving the Bootstrap options map empty.
This removes the global lock from the connect path entirely while preserving
identical behavior:
applied last so they override defaults, matching the previous Bootstrap
order.
registration (handlerAdded, channel already registered), which is before
the connect listener fires, so options are set before the channel connects.
The connect-timeout option is honored because Netty reads it at connect time.
channel initializer into its pipeline.
Scope is limited to ChannelManager.
Benefits
(fewer per-connect allocations).