Class ChannelManager
- java.lang.Object
-
- org.asynchttpclient.netty.channel.ChannelManager
-
public class ChannelManager extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static StringAHC_HTTP_HANDLERstatic StringAHC_HTTP2_HANDLERstatic StringAHC_WS_HANDLERstatic StringCHUNKED_WRITER_HANDLERstatic StringHTTP_CLIENT_CODECstatic StringHTTP2_FRAME_CODECstatic StringHTTP2_MULTIPLEXstatic StringINFLATER_HANDLERstatic StringLOGGING_HANDLERstatic StringSOCKS_HANDLERstatic StringSSL_HANDLERstatic StringWS_COMPRESSOR_HANDLERstatic StringWS_DECODER_HANDLERstatic StringWS_ENCODER_HANDLERstatic StringWS_FRAME_AGGREGATOR
-
Constructor Summary
Constructors Constructor Description ChannelManager(AsyncHttpClientConfig config, io.netty.util.Timer nettyTimer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanaddHttp2ConnectionWaiter(Object partitionKey, Consumer<io.netty.channel.Channel> onConnection)Registers a one-shot waiter to be invoked when an HTTP/2 connection is registered for the same host (or withnullon client close).io.netty.handler.ssl.SslHandleraddSslHandler(io.netty.channel.ChannelPipeline pipeline, Uri uri, String virtualHost, boolean hasSocksProxyHandler)io.netty.handler.ssl.SslHandleraddSslHandler(io.netty.channel.ChannelPipeline pipeline, Uri uri, String virtualHost, boolean hasSocksProxyHandler, boolean http2Allowed)voidclose()voidcloseChannel(io.netty.channel.Channel channel)voidconfigureBootstraps(NettyRequestSender requestSender)voiddrainChannelAndOffer(io.netty.channel.Channel channel, NettyResponseFuture<?> future)voiddrainChannelAndOffer(io.netty.channel.Channel channel, NettyResponseFuture<?> future, boolean keepAlive, Object partitionKey)@Nullable io.netty.resolver.AddressResolverGroup<InetSocketAddress>getAddressResolverGroup()Return theAddressResolverGroupused for async DNS resolution, ornullif per-request name resolvers should be used (legacy behavior).io.netty.util.concurrent.Future<io.netty.bootstrap.Bootstrap>getBootstrap(Uri uri, io.netty.resolver.NameResolver<InetAddress> nameResolver, ProxyServer proxy)ChannelPoolgetChannelPool()ClientStatsgetClientStats()Builds a point-in-time stats snapshot, scanning open channels and idle pooled channels once each.io.netty.channel.EventLoopGroupgetEventLoopGroup()Http2HandlergetHttp2Handler()Returns the sharedHttp2Handlerinstance for use with stream child channels.static booleanisHttp2(io.netty.channel.Channel channel)Checks whether the given channel is an HTTP/2 connection: the parent that multiplexes streams, not one of its stream children, which carry neither the multiplex handler nor connection state of their own.booleanisHttp2CleartextEnabled()booleanisHttp2KnownUnavailable(Object partitionKey)static booleanisHttp2StreamChannel(io.netty.channel.Channel channel)Checks whether the given channel is an HTTP/2 stream child channel.booleanisOpen()static booleanisSslHandlerConfigured(io.netty.channel.ChannelPipeline pipeline)io.netty.channel.Channelpoll(Object partitionKey)io.netty.channel.Channelpoll(Uri uri, String virtualHost, ProxyServer proxy, ChannelPoolPartitioning connectionPoolPartitioning)Deprecated.no longer used internally.io.netty.channel.ChannelpollHttp2(Uri uri, String virtualHost, ProxyServer proxy, ChannelPoolPartitioning connectionPoolPartitioning)Deprecated.no longer used internally.io.netty.channel.ChannelpollHttp2Connection(Object partitionKey)Returns an active, non-draining HTTP/2 connection for the given partition key, ornull.io.netty.channel.ChannelpollHttp2SiblingConnection(Object baseKey)Round-robin permit-starved fallback (issue #2214): returns an active, non-draining HTTP/2 connection open to ANY IP of the host identified bybaseKey, ornullif none qualifies.voidregisterHttp2Connection(Object partitionKey, io.netty.channel.Channel channel)Registers an HTTP/2 connection in the registry for the given partition key.voidregisterOpenChannel(io.netty.channel.Channel channel)voidremoveAll(io.netty.channel.Channel connection)voidremoveHttp2Connection(Object partitionKey, io.netty.channel.Channel channel)Removes an HTTP/2 connection from the registry, but only if it's the currently registered connection for that partition key (avoids removing a replacement connection).voidremoveHttp2ConnectionWaiter(Object partitionKey, Consumer<io.netty.channel.Channel> onConnection)voidresumeHttp2ResponseBody(io.netty.channel.Channel streamChannel)Restores normal connection-window accounting after an HTTP/2 response stops being suspended.voidsuspendHttp2ResponseBody(io.netty.channel.Channel streamChannel)Enables connection-window refill for the lifetime of a suspended HTTP/2 response.voidtryToOfferChannelToPool(io.netty.channel.Channel channel, AsyncHandler<?> asyncHandler, boolean keepAlive, Object partitionKey)voidtryToOfferChannelToPool(io.netty.channel.Channel channel, NettyResponseFuture<?> future, boolean keepAlive, Object partitionKey)io.netty.util.concurrent.Future<io.netty.channel.Channel>updatePipelineForHttpsTunneling(io.netty.channel.ChannelPipeline pipeline, Uri requestUri, boolean http2Allowed)io.netty.util.concurrent.Future<io.netty.channel.Channel>updatePipelineForHttpsTunneling(io.netty.channel.ChannelPipeline pipeline, Uri requestUri, ProxyServer proxyServer)io.netty.util.concurrent.Future<io.netty.channel.Channel>updatePipelineForHttpTunneling(io.netty.channel.ChannelPipeline pipeline, Uri requestUri)io.netty.util.concurrent.Future<io.netty.channel.Channel>updatePipelineForHttpTunneling(io.netty.channel.ChannelPipeline pipeline, Uri requestUri, boolean http2Allowed)voidupgradePipelineForWebSockets(io.netty.channel.ChannelPipeline pipeline)voidupgradePipelineToHttp2(io.netty.channel.ChannelPipeline pipeline)Upgrades the pipeline from HTTP/1.1 to HTTP/2 after ALPN negotiates "h2".voidupgradePipelineToHttp2AfterProxyConnect(io.netty.channel.ChannelPipeline pipeline, Object partitionKey)Upgrades and registers a proxy tunnel when the target TLS handshake negotiated HTTP/2.voidupgradePipelineToHttp2AfterProxyConnect(io.netty.channel.ChannelPipeline pipeline, Object partitionKey, boolean shareable)
-
-
-
Field Detail
-
HTTP_CLIENT_CODEC
public static final String HTTP_CLIENT_CODEC
- See Also:
- Constant Field Values
-
SSL_HANDLER
public static final String SSL_HANDLER
- See Also:
- Constant Field Values
-
SOCKS_HANDLER
public static final String SOCKS_HANDLER
- See Also:
- Constant Field Values
-
INFLATER_HANDLER
public static final String INFLATER_HANDLER
- See Also:
- Constant Field Values
-
CHUNKED_WRITER_HANDLER
public static final String CHUNKED_WRITER_HANDLER
- See Also:
- Constant Field Values
-
WS_DECODER_HANDLER
public static final String WS_DECODER_HANDLER
- See Also:
- Constant Field Values
-
WS_FRAME_AGGREGATOR
public static final String WS_FRAME_AGGREGATOR
- See Also:
- Constant Field Values
-
WS_COMPRESSOR_HANDLER
public static final String WS_COMPRESSOR_HANDLER
- See Also:
- Constant Field Values
-
WS_ENCODER_HANDLER
public static final String WS_ENCODER_HANDLER
- See Also:
- Constant Field Values
-
AHC_HTTP_HANDLER
public static final String AHC_HTTP_HANDLER
- See Also:
- Constant Field Values
-
AHC_WS_HANDLER
public static final String AHC_WS_HANDLER
- See Also:
- Constant Field Values
-
LOGGING_HANDLER
public static final String LOGGING_HANDLER
- See Also:
- Constant Field Values
-
HTTP2_FRAME_CODEC
public static final String HTTP2_FRAME_CODEC
- See Also:
- Constant Field Values
-
HTTP2_MULTIPLEX
public static final String HTTP2_MULTIPLEX
- See Also:
- Constant Field Values
-
AHC_HTTP2_HANDLER
public static final String AHC_HTTP2_HANDLER
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ChannelManager
public ChannelManager(AsyncHttpClientConfig config, io.netty.util.Timer nettyTimer)
-
-
Method Detail
-
isSslHandlerConfigured
public static boolean isSslHandlerConfigured(io.netty.channel.ChannelPipeline pipeline)
-
configureBootstraps
public void configureBootstraps(NettyRequestSender requestSender)
-
tryToOfferChannelToPool
public void tryToOfferChannelToPool(io.netty.channel.Channel channel, NettyResponseFuture<?> future, boolean keepAlive, Object partitionKey)
-
tryToOfferChannelToPool
public final void tryToOfferChannelToPool(io.netty.channel.Channel channel, AsyncHandler<?> asyncHandler, boolean keepAlive, Object partitionKey)
-
registerHttp2Connection
public void registerHttp2Connection(Object partitionKey, io.netty.channel.Channel channel)
Registers an HTTP/2 connection in the registry for the given partition key. The connection stays in the registry (not the regular pool) to allow multiplexing — multiple requests can share the same connection concurrently.
-
addHttp2ConnectionWaiter
public boolean addHttp2ConnectionWaiter(Object partitionKey, Consumer<io.netty.channel.Channel> onConnection)
Registers a one-shot waiter to be invoked when an HTTP/2 connection is registered for the same host (or withnullon client close). See thehttp2ConnectionWaitersfield and NettyRequestSender's HTTP/2 deferral. Waiters are grouped by the per-host base key (baseKeyOf(java.lang.Object)), NOT the full per-IP partition key: in round-robin mode a permit-starved request pinned to one IP must be woken by a connection that registers for ANY IP of the host so it can multiplex onto that sibling (issue #2214) — the per-IP registration key on its own would never wake it. The waiter must be idempotent — it may be invoked by a registration, by the client-close sweep, or removed and invoked by its own timeout concurrently.- Returns:
trueif the waiter was registered;falseif the client is already closing, in which case it is NOT registered and the caller must fail its request immediately rather than arm a timeout (no connection will register and the timer that would fire the deadline is being stopped). This closes the window where a request arms a waiter between thefailHttp2ConnectionWaiters()sweep andnettyTimer.stop()and then hangs forever.
-
removeHttp2ConnectionWaiter
public void removeHttp2ConnectionWaiter(Object partitionKey, Consumer<io.netty.channel.Channel> onConnection)
-
isHttp2KnownUnavailable
public boolean isHttp2KnownUnavailable(Object partitionKey)
-
removeHttp2Connection
public void removeHttp2Connection(Object partitionKey, io.netty.channel.Channel channel)
Removes an HTTP/2 connection from the registry, but only if it's the currently registered connection for that partition key (avoids removing a replacement connection). The emptied per-host inner map is pruned atomically — computeIfPresent serializes with registerHttp2Connection's compute() on the same outer bucket, so a concurrent insert cannot be lost to the prune.
-
pollHttp2Connection
public io.netty.channel.Channel pollHttp2Connection(Object partitionKey)
Returns an active, non-draining HTTP/2 connection for the given partition key, ornull. Unlike the regular pool, this does NOT remove the connection — it remains available for concurrent multiplexed requests.
-
pollHttp2SiblingConnection
public io.netty.channel.Channel pollHttp2SiblingConnection(Object baseKey)
Round-robin permit-starved fallback (issue #2214): returns an active, non-draining HTTP/2 connection open to ANY IP of the host identified bybaseKey, ornullif none qualifies. Used only byNettyRequestSenderwhen a request pinned to one IP cannot acquire a per-host connection permit (maxConnectionsPerHost) and its own per-IP connection does not exist — it may then multiplex onto a sibling-IP connection instead of failing. NOT used on the happy path, which polls the exact per-IP key so load keeps spreading across IPs.Iterates only the inner map for this host (bounded by the host's resolved-IP count). Per-key validation and dead-entry eviction are delegated to
pollHttp2Connection(java.lang.Object); redundant coalescing-losers are never stored, so they are never returned.
-
pollHttp2
@Deprecated public io.netty.channel.Channel pollHttp2(Uri uri, String virtualHost, ProxyServer proxy, ChannelPoolPartitioning connectionPoolPartitioning)
Deprecated.no longer used internally. Compute the partition key at the call site — from the request being dispatched, so it stays correct on the filter-replay path — and callpollHttp2Connection(Object). Kept for binary compatibility; slated for removal in the next major release.Polls for an HTTP/2 connection by URI/virtualHost/proxy, using the same partition key logic as the regular pool. Returns the connection without removing it from the registry.
-
poll
@Deprecated public io.netty.channel.Channel poll(Uri uri, String virtualHost, ProxyServer proxy, ChannelPoolPartitioning connectionPoolPartitioning)
Deprecated.no longer used internally. Compute the partition key at the call site — from the request being dispatched, so it stays correct on the filter-replay path — and callpoll(Object). Kept for binary compatibility; slated for removal in the next major release.
-
poll
public io.netty.channel.Channel poll(Object partitionKey)
-
removeAll
public void removeAll(io.netty.channel.Channel connection)
-
close
public void close()
-
closeChannel
public void closeChannel(io.netty.channel.Channel channel)
-
registerOpenChannel
public void registerOpenChannel(io.netty.channel.Channel channel)
-
updatePipelineForHttpTunneling
public io.netty.util.concurrent.Future<io.netty.channel.Channel> updatePipelineForHttpTunneling(io.netty.channel.ChannelPipeline pipeline, Uri requestUri)
-
updatePipelineForHttpTunneling
public io.netty.util.concurrent.Future<io.netty.channel.Channel> updatePipelineForHttpTunneling(io.netty.channel.ChannelPipeline pipeline, Uri requestUri, boolean http2Allowed)- Parameters:
http2Allowed- false when the realm authenticates the socket the tunnel runs over.
-
updatePipelineForHttpsTunneling
public io.netty.util.concurrent.Future<io.netty.channel.Channel> updatePipelineForHttpsTunneling(io.netty.channel.ChannelPipeline pipeline, Uri requestUri, ProxyServer proxyServer)
-
updatePipelineForHttpsTunneling
public io.netty.util.concurrent.Future<io.netty.channel.Channel> updatePipelineForHttpsTunneling(io.netty.channel.ChannelPipeline pipeline, Uri requestUri, boolean http2Allowed)- Parameters:
http2Allowed- false when the realm authenticates the socket the tunnel runs over.
-
addSslHandler
public io.netty.handler.ssl.SslHandler addSslHandler(io.netty.channel.ChannelPipeline pipeline, Uri uri, String virtualHost, boolean hasSocksProxyHandler)
-
addSslHandler
public io.netty.handler.ssl.SslHandler addSslHandler(io.netty.channel.ChannelPipeline pipeline, Uri uri, String virtualHost, boolean hasSocksProxyHandler, boolean http2Allowed)- Parameters:
http2Allowed- false for a WebSocket (no RFC 8441 support) and for a realm that authenticates the socket, since every stream of an h2 connection shares it.
-
getBootstrap
public io.netty.util.concurrent.Future<io.netty.bootstrap.Bootstrap> getBootstrap(Uri uri, io.netty.resolver.NameResolver<InetAddress> nameResolver, ProxyServer proxy)
-
isHttp2
public static boolean isHttp2(io.netty.channel.Channel channel)
Checks whether the given channel is an HTTP/2 connection: the parent that multiplexes streams, not one of its stream children, which carry neither the multiplex handler nor connection state of their own.Answered from the
Http2ConnectionStateattached to the connection rather than by lookingHTTP2_MULTIPLEXup in the pipeline. The two are attached together, inupgradePipelineToHttp2(io.netty.channel.ChannelPipeline), and agree for as long as the connection is live; a pipeline lookup, though, compares handler names down the chain, and an HTTP/1.1 connection, which has no such handler, is walked to the end to say no.They part on close, where Netty's own teardown strips the pipeline and leaves the attribute: a closed connection answers yes here and would have answered no to a lookup. That divergence is harmless at the one internal call site, where a true answer only skips storing the per-request future on the parent, and the next statement re-checks liveness and routes a closed channel to handleUnexpectedClosedChannel.
-
isHttp2StreamChannel
public static boolean isHttp2StreamChannel(io.netty.channel.Channel channel)
Checks whether the given channel is an HTTP/2 stream child channel. Stream channels are single-use and don't support HTTP/1.1 operations like draining or pipeline modification.
-
getHttp2Handler
public Http2Handler getHttp2Handler()
Returns the sharedHttp2Handlerinstance for use with stream child channels.
-
upgradePipelineToHttp2
public void upgradePipelineToHttp2(io.netty.channel.ChannelPipeline pipeline)
Upgrades the pipeline from HTTP/1.1 to HTTP/2 after ALPN negotiates "h2". Removes HTTP/1.1 handlers and addsHttp2FrameCodec+Http2MultiplexHandler. The per-streamHttp2Handleris added separately on each stream child channel.
-
upgradePipelineToHttp2AfterProxyConnect
public void upgradePipelineToHttp2AfterProxyConnect(io.netty.channel.ChannelPipeline pipeline, Object partitionKey)Upgrades and registers a proxy tunnel when the target TLS handshake negotiated HTTP/2. The caller controls when this runs so the upgrade can happen immediately before the tunneled request is sent.
-
upgradePipelineToHttp2AfterProxyConnect
public void upgradePipelineToHttp2AfterProxyConnect(io.netty.channel.ChannelPipeline pipeline, Object partitionKey, boolean shareable)- Parameters:
shareable- false when the tunnel is authenticated, so no other principal can draw it.
-
upgradePipelineForWebSockets
public void upgradePipelineForWebSockets(io.netty.channel.ChannelPipeline pipeline)
-
drainChannelAndOffer
public void drainChannelAndOffer(io.netty.channel.Channel channel, NettyResponseFuture<?> future)
-
drainChannelAndOffer
public void drainChannelAndOffer(io.netty.channel.Channel channel, NettyResponseFuture<?> future, boolean keepAlive, Object partitionKey)
-
getChannelPool
public ChannelPool getChannelPool()
-
getEventLoopGroup
public io.netty.channel.EventLoopGroup getEventLoopGroup()
-
getAddressResolverGroup
@Nullable public @Nullable io.netty.resolver.AddressResolverGroup<InetSocketAddress> getAddressResolverGroup()
Return theAddressResolverGroupused for async DNS resolution, ornullif per-request name resolvers should be used (legacy behavior).
-
getClientStats
public ClientStats getClientStats()
Builds a point-in-time stats snapshot, scanning open channels and idle pooled channels once each.
-
suspendHttp2ResponseBody
public void suspendHttp2ResponseBody(io.netty.channel.Channel streamChannel)
Enables connection-window refill for the lifetime of a suspended HTTP/2 response.
-
resumeHttp2ResponseBody
public void resumeHttp2ResponseBody(io.netty.channel.Channel streamChannel)
Restores normal connection-window accounting after an HTTP/2 response stops being suspended.
-
isOpen
public boolean isOpen()
-
isHttp2CleartextEnabled
public boolean isHttp2CleartextEnabled()
-
-