Skip to content

Commit 6697a2a

Browse files
committed
Use CHMV8 fork until we target JDK8, close AsyncHttpClient#886
1 parent ba2acb8 commit 6697a2a

File tree

8 files changed

+12624
-19
lines changed

8 files changed

+12624
-19
lines changed

src/main/java/com/ning/http/client/providers/netty/channel/pool/DefaultChannelPool.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,33 @@
2525

2626
import com.ning.http.client.AsyncHttpClientConfig;
2727
import com.ning.http.client.providers.netty.channel.Channels;
28+
import com.ning.http.client.providers.netty.chmv8.ConcurrentHashMapV8;
2829
import com.ning.http.client.providers.netty.future.NettyResponseFuture;
2930

3031
import java.util.ArrayList;
3132
import java.util.Collections;
3233
import java.util.List;
3334
import java.util.Map;
34-
import java.util.concurrent.ConcurrentHashMap;
3535
import java.util.concurrent.ConcurrentLinkedQueue;
3636
import java.util.concurrent.TimeUnit;
3737
import java.util.concurrent.atomic.AtomicBoolean;
3838

3939
/**
40-
* A simple implementation of {@link com.ning.http.client.providers.netty.channel.pool.ChannelPool} based on a {@link java.util.concurrent.ConcurrentHashMap}
40+
* A simple implementation of {@link com.ning.http.client.providers.netty.channel.pool.ChannelPool} based on a {@link com.ning.http.client.providers.netty.chmv8.ConcurrentHashMapV8}
4141
*/
4242
public final class DefaultChannelPool implements ChannelPool {
4343

4444
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultChannelPool.class);
4545

46-
private final ConcurrentHashMap<String, ConcurrentLinkedQueue<IdleChannel>> partitions = new ConcurrentHashMap<>();
47-
private final ConcurrentHashMap<Integer, ChannelCreation> channelId2Creation = new ConcurrentHashMap<>();
46+
private static final ConcurrentHashMapV8.Fun<String, ConcurrentLinkedQueue<IdleChannel>> PARTITION_COMPUTER = new ConcurrentHashMapV8.Fun<String, ConcurrentLinkedQueue<IdleChannel>>() {
47+
@Override
48+
public ConcurrentLinkedQueue<IdleChannel> apply(String partitionId) {
49+
return new ConcurrentLinkedQueue<>();
50+
}
51+
};
52+
53+
private final ConcurrentHashMapV8<String, ConcurrentLinkedQueue<IdleChannel>> partitions = new ConcurrentHashMapV8<>();
54+
private final ConcurrentHashMapV8<Integer, ChannelCreation> channelId2Creation = new ConcurrentHashMapV8<>();
4855
private final AtomicBoolean isClosed = new AtomicBoolean(false);
4956
private final Timer nettyTimer;
5057
private final boolean sslConnectionPoolEnabled;
@@ -228,19 +235,7 @@ public void run(Timeout timeout) throws Exception {
228235
}
229236
}
230237

231-
private ConcurrentLinkedQueue<IdleChannel> getPartition(String partitionId) {
232-
ConcurrentLinkedQueue<IdleChannel> partition = partitions.get(partitionId);
233-
if (partition == null) {
234-
// lazy init partition
235-
ConcurrentLinkedQueue<IdleChannel> newPartition = new ConcurrentLinkedQueue<>();
236-
partition = partitions.putIfAbsent(partitionId, newPartition);
237-
if (partition == null)
238-
partition = newPartition;
239-
}
240-
return partition;
241-
}
242-
243-
public boolean offer(Channel channel, String partition) {
238+
public boolean offer(Channel channel, String partitionId) {
244239
if (isClosed.get() || (!sslConnectionPoolEnabled && channel.getPipeline().get(SslHandler.class) != null))
245240
return false;
246241

@@ -249,9 +244,9 @@ public boolean offer(Channel channel, String partition) {
249244
if (isTTLExpired(channel, now))
250245
return false;
251246

252-
boolean added = getPartition(partition).add(new IdleChannel(channel, now));
247+
boolean added = partitions.computeIfAbsent(partitionId, PARTITION_COMPUTER).add(new IdleChannel(channel, now));
253248
if (added)
254-
channelId2Creation.putIfAbsent(channel.getId(), new ChannelCreation(now, partition));
249+
channelId2Creation.putIfAbsent(channel.getId(), new ChannelCreation(now, partitionId));
255250

256251
return added;
257252
}

src/main/java/com/ning/http/client/providers/netty/chmv8/ConcurrentHashMapV8.java

Lines changed: 6207 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)