21
21
import io .netty .util .Timer ;
22
22
import io .netty .util .TimerTask ;
23
23
24
- import java .util .ArrayList ;
25
- import java .util .Collections ;
26
- import java .util .List ;
27
- import java .util .Map ;
24
+ import java .util .*;
28
25
import java .util .concurrent .ConcurrentHashMap ;
29
26
import java .util .concurrent .ConcurrentLinkedDeque ;
30
27
import java .util .concurrent .TimeUnit ;
@@ -52,6 +49,7 @@ public final class DefaultChannelPool implements ChannelPool {
52
49
private final int maxIdleTime ;
53
50
private final boolean maxIdleTimeEnabled ;
54
51
private final long cleanerPeriod ;
52
+ private final PoolLeaseStrategy poolLeaseStrategy ;
55
53
56
54
public DefaultChannelPool (AsyncHttpClientConfig config , Timer hashedWheelTimer ) {
57
55
this (config .getPooledConnectionIdleTimeout (),//
@@ -66,12 +64,23 @@ private ChannelId channelId(Channel channel) {
66
64
public DefaultChannelPool (int maxIdleTime ,//
67
65
int connectionTtl ,//
68
66
Timer nettyTimer ) {
67
+ this (maxIdleTime ,//
68
+ connectionTtl ,//
69
+ PoolLeaseStrategy .LIFO ,//
70
+ nettyTimer );
71
+ }
72
+
73
+ public DefaultChannelPool (int maxIdleTime ,//
74
+ int connectionTtl ,//
75
+ PoolLeaseStrategy poolLeaseStrategy ,//
76
+ Timer nettyTimer ) {
69
77
this .maxIdleTime = (int ) maxIdleTime ;
70
78
this .connectionTtl = connectionTtl ;
71
79
connectionTtlEnabled = connectionTtl > 0 ;
72
80
channelId2Creation = connectionTtlEnabled ? new ConcurrentHashMap <>() : null ;
73
81
this .nettyTimer = nettyTimer ;
74
82
maxIdleTimeEnabled = maxIdleTime > 0 ;
83
+ this .poolLeaseStrategy = poolLeaseStrategy ;
75
84
76
85
cleanerPeriod = Math .min (connectionTtlEnabled ? connectionTtl : Integer .MAX_VALUE , maxIdleTimeEnabled ? maxIdleTime : Long .MAX_VALUE );
77
86
@@ -266,7 +275,7 @@ public Channel poll(Object partitionKey) {
266
275
ConcurrentLinkedDeque <IdleChannel > partition = partitions .get (partitionKey );
267
276
if (partition != null ) {
268
277
while (idleChannel == null ) {
269
- idleChannel = partition . pollFirst ( );
278
+ idleChannel = poolLeaseStrategy . lease ( partition );
270
279
271
280
if (idleChannel == null )
272
281
// pool is empty
@@ -342,4 +351,19 @@ public void flushPartitions(ChannelPoolPartitionSelector selector) {
342
351
flushPartition (partitionKey , partitionsEntry .getValue ());
343
352
}
344
353
}
354
+
355
+ public enum PoolLeaseStrategy {
356
+ LIFO {
357
+ public <E > E lease (Deque <E > d ) {
358
+ return d .pollFirst ();
359
+ }
360
+ },
361
+ FIFO {
362
+ public <E > E lease (Deque <E > d ) {
363
+ return d .pollLast ();
364
+ }
365
+ };
366
+
367
+ abstract <E > E lease (Deque <E > d );
368
+ }
345
369
}
0 commit comments