Skip to content

Commit 0049808

Browse files
stepanchegslandelle
authored andcommitted
Store partition key in Channel attribute instead of global hash map (AsyncHttpClient#1373)
1 parent e85e9ba commit 0049808

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.netty.handler.logging.LoggingHandler;
3636
import io.netty.handler.ssl.SslHandler;
3737
import io.netty.handler.stream.ChunkedWriteHandler;
38+
import io.netty.util.AttributeKey;
3839
import io.netty.util.Timer;
3940
import io.netty.util.concurrent.DefaultThreadFactory;
4041
import io.netty.util.concurrent.GlobalEventExecutor;
@@ -93,6 +94,8 @@ public class ChannelManager {
9394
public static final String AHC_WS_HANDLER = "ahc-ws";
9495
public static final String LOGGING_HANDLER = "logging";
9596

97+
private static final AttributeKey<Object> partitionKeyAttr = AttributeKey.valueOf(ChannelManager.class, "partitionKey");
98+
9699
private final AsyncHttpClientConfig config;
97100
private final SslEngineFactory sslEngineFactory;
98101
private final EventLoopGroup eventLoopGroup;
@@ -105,7 +108,6 @@ public class ChannelManager {
105108

106109
private final ChannelPool channelPool;
107110
private final ChannelGroup openChannels;
108-
private final ConcurrentHashMap<Channel, Object> channelId2PartitionKey = new ConcurrentHashMap<>();
109111
private final boolean maxTotalConnectionsEnabled;
110112
private final Semaphore freeChannels;
111113
private final boolean maxConnectionsPerHostEnabled;
@@ -148,7 +150,7 @@ public boolean remove(Object o) {
148150
if (maxTotalConnectionsEnabled)
149151
freeChannels.release();
150152
if (maxConnectionsPerHostEnabled) {
151-
Object partitionKey = channelId2PartitionKey.remove(Channel.class.cast(o));
153+
Object partitionKey = Channel.class.cast(o).attr(partitionKeyAttr).getAndSet(null);
152154
if (partitionKey != null) {
153155
Semaphore hostFreeChannels = freeChannelsPerHost.get(partitionKey);
154156
if (hostFreeChannels != null)
@@ -315,7 +317,7 @@ public final void tryToOfferChannelToPool(Channel channel, AsyncHandler<?> async
315317
AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionOffer(channel);
316318
if (channelPool.offer(channel, partitionKey)) {
317319
if (maxConnectionsPerHostEnabled)
318-
channelId2PartitionKey.putIfAbsent(channel, partitionKey);
320+
channel.attr(partitionKeyAttr).setIfAbsent(partitionKey);
319321
} else {
320322
// rejected by pool
321323
closeChannel(channel);
@@ -390,7 +392,7 @@ public void releaseChannelLock(Object partitionKey) {
390392
public void registerOpenChannel(Channel channel, Object partitionKey) {
391393
openChannels.add(channel);
392394
if (maxConnectionsPerHostEnabled) {
393-
channelId2PartitionKey.put(channel, partitionKey);
395+
channel.attr(partitionKeyAttr).set(partitionKey);
394396
}
395397
}
396398

0 commit comments

Comments
 (0)