Skip to content

Commit c1dc343

Browse files
authored
Merge pull request AsyncHttpClient#1188 from tomfitzhenry/connection-pool-lifo-or-fifo
Allow user to specify whether pool is LIFO or FIFO
2 parents 82e1f6d + 9bcb2d2 commit c1dc343

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import io.netty.util.Timer;
2222
import io.netty.util.TimerTask;
2323

24-
import java.util.ArrayList;
25-
import java.util.Collections;
26-
import java.util.List;
27-
import java.util.Map;
24+
import java.util.*;
2825
import java.util.concurrent.ConcurrentHashMap;
2926
import java.util.concurrent.ConcurrentLinkedDeque;
3027
import java.util.concurrent.TimeUnit;
@@ -52,6 +49,7 @@ public final class DefaultChannelPool implements ChannelPool {
5249
private final int maxIdleTime;
5350
private final boolean maxIdleTimeEnabled;
5451
private final long cleanerPeriod;
52+
private final PoolLeaseStrategy poolLeaseStrategy;
5553

5654
public DefaultChannelPool(AsyncHttpClientConfig config, Timer hashedWheelTimer) {
5755
this(config.getPooledConnectionIdleTimeout(),//
@@ -66,12 +64,23 @@ private ChannelId channelId(Channel channel) {
6664
public DefaultChannelPool(int maxIdleTime,//
6765
int connectionTtl,//
6866
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) {
6977
this.maxIdleTime = (int) maxIdleTime;
7078
this.connectionTtl = connectionTtl;
7179
connectionTtlEnabled = connectionTtl > 0;
7280
channelId2Creation = connectionTtlEnabled ? new ConcurrentHashMap<>() : null;
7381
this.nettyTimer = nettyTimer;
7482
maxIdleTimeEnabled = maxIdleTime > 0;
83+
this.poolLeaseStrategy = poolLeaseStrategy;
7584

7685
cleanerPeriod = Math.min(connectionTtlEnabled ? connectionTtl : Integer.MAX_VALUE, maxIdleTimeEnabled ? maxIdleTime : Long.MAX_VALUE);
7786

@@ -266,7 +275,7 @@ public Channel poll(Object partitionKey) {
266275
ConcurrentLinkedDeque<IdleChannel> partition = partitions.get(partitionKey);
267276
if (partition != null) {
268277
while (idleChannel == null) {
269-
idleChannel = partition.pollFirst();
278+
idleChannel = poolLeaseStrategy.lease(partition);
270279

271280
if (idleChannel == null)
272281
// pool is empty
@@ -342,4 +351,19 @@ public void flushPartitions(ChannelPoolPartitionSelector selector) {
342351
flushPartition(partitionKey, partitionsEntry.getValue());
343352
}
344353
}
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+
}
345369
}

0 commit comments

Comments
 (0)