1515
1616import io .netty .channel .Channel ;
1717import io .netty .channel .ChannelId ;
18- import io .netty .util .Timeout ;
18+ import io .netty .util .* ;
1919import io .netty .util .Timer ;
2020import io .netty .util .TimerTask ;
2121import org .asynchttpclient .AsyncHttpClientConfig ;
4343public final class DefaultChannelPool implements ChannelPool {
4444
4545 private static final Logger LOGGER = LoggerFactory .getLogger (DefaultChannelPool .class );
46+ private static final AttributeKey <ChannelCreation > CHANNEL_CREATION_ATTRIBUTE_KEY = AttributeKey .valueOf ("channelCreation" );
4647
4748 private final ConcurrentHashMap <Object , ConcurrentLinkedDeque <IdleChannel >> partitions = new ConcurrentHashMap <>();
48- private final ConcurrentHashMap <ChannelId , ChannelCreation > channelId2Creation ;
4949 private final AtomicBoolean isClosed = new AtomicBoolean (false );
5050 private final Timer nettyTimer ;
5151 private final int connectionTtl ;
@@ -81,7 +81,6 @@ public DefaultChannelPool(int maxIdleTime,
8181 this .maxIdleTime = maxIdleTime ;
8282 this .connectionTtl = connectionTtl ;
8383 connectionTtlEnabled = connectionTtl > 0 ;
84- channelId2Creation = connectionTtlEnabled ? new ConcurrentHashMap <>() : null ;
8584 this .nettyTimer = nettyTimer ;
8685 maxIdleTimeEnabled = maxIdleTime > 0 ;
8786 this .poolLeaseStrategy = poolLeaseStrategy ;
@@ -100,7 +99,7 @@ private boolean isTtlExpired(Channel channel, long now) {
10099 if (!connectionTtlEnabled )
101100 return false ;
102101
103- ChannelCreation creation = channelId2Creation . get ( channel .id () );
102+ ChannelCreation creation = channel .attr ( CHANNEL_CREATION_ATTRIBUTE_KEY ). get ( );
104103 return creation != null && now - creation .creationTime >= connectionTtl ;
105104 }
106105
@@ -134,8 +133,9 @@ private boolean offer0(Channel channel, Object partitionKey, long now) {
134133
135134 private void registerChannelCreation (Channel channel , Object partitionKey , long now ) {
136135 ChannelId id = channel .id ();
137- if (!channelId2Creation .containsKey (id )) {
138- channelId2Creation .putIfAbsent (id , new ChannelCreation (now , partitionKey ));
136+ Attribute <ChannelCreation > channelCreationAttribute = channel .attr (CHANNEL_CREATION_ATTRIBUTE_KEY );
137+ if (channelCreationAttribute .get () == null ) {
138+ channelCreationAttribute .set (new ChannelCreation (now , partitionKey ));
139139 }
140140 }
141141
@@ -169,7 +169,7 @@ else if (!Channels.isChannelActive(idleChannel.channel)) {
169169 * {@inheritDoc}
170170 */
171171 public boolean removeAll (Channel channel ) {
172- ChannelCreation creation = connectionTtlEnabled ? channelId2Creation . remove ( channel .id () ) : null ;
172+ ChannelCreation creation = connectionTtlEnabled ? channel .attr ( CHANNEL_CREATION_ATTRIBUTE_KEY ). get ( ) : null ;
173173 return !isClosed .get () && creation != null && partitions .get (creation .partitionKey ).remove (new IdleChannel (channel , Long .MIN_VALUE ));
174174 }
175175
@@ -188,17 +188,11 @@ public void destroy() {
188188 return ;
189189
190190 partitions .clear ();
191- if (connectionTtlEnabled ) {
192- channelId2Creation .clear ();
193- }
194191 }
195192
196193 private void close (Channel channel ) {
197194 // FIXME pity to have to do this here
198195 Channels .setDiscard (channel );
199- if (connectionTtlEnabled ) {
200- channelId2Creation .remove (channel .id ());
201- }
202196 Channels .silentlyCloseChannel (channel );
203197 }
204198
@@ -369,11 +363,6 @@ public void run(Timeout timeout) {
369363 List <IdleChannel > closedChannels = closeChannels (expiredChannels (partition , start ));
370364
371365 if (!closedChannels .isEmpty ()) {
372- if (connectionTtlEnabled ) {
373- for (IdleChannel closedChannel : closedChannels )
374- channelId2Creation .remove (closedChannel .channel .id ());
375- }
376-
377366 partition .removeAll (closedChannels );
378367 closedCount += closedChannels .size ();
379368 }
0 commit comments