Skip to content

Commit e26500f

Browse files
committed
("Getting an error from AHC when a connection goes back into connection pool") Collaborative works with Dominic Tootell
1 parent 9350deb commit e26500f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@ public Object call() throws Exception {
11071107
return null;
11081108
}
11091109
};
1110+
11101111
if (future.getKeepAlive() && response.isChunked()) {
11111112
// We must make sure there is no bytes left before executing the next request.
11121113
ctx.setAttachment(ac);

src/main/java/com/ning/http/client/providers/netty/NettyConnectionsPool.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,12 @@ public boolean offer(String uri, Channel channel) {
158158
int size = idleConnectionForHost.size();
159159
if (maxConnectionPerHost == -1 || size < maxConnectionPerHost) {
160160
IdleChannel idleChannel = new IdleChannel(uri, channel);
161-
added = idleConnectionForHost.add(idleChannel);
162-
if (channel2IdleChannel.put(channel, idleChannel) != null) {
163-
log.error("Channel {} already exists in the connections pool!", channel);
161+
synchronized(idleConnectionForHost) {
162+
added = idleConnectionForHost.add(idleChannel);
163+
164+
if (channel2IdleChannel.put(channel, idleChannel) != null) {
165+
log.error("Channel {} already exists in the connections pool!", channel);
166+
}
164167
}
165168
} else {
166169
log.debug("Maximum number of requests per host reached {} for {}", maxConnectionPerHost, uri);
@@ -183,8 +186,12 @@ public Channel poll(String uri) {
183186
boolean poolEmpty = false;
184187
while (!poolEmpty && idleChannel == null) {
185188
if (idleConnectionForHost.size() > 0) {
186-
idleChannel = idleConnectionForHost.poll();
187-
if (idleChannel != null) channel2IdleChannel.remove(idleChannel.channel);
189+
synchronized(idleConnectionForHost) {
190+
idleChannel = idleConnectionForHost.poll();
191+
if (idleChannel != null) {
192+
channel2IdleChannel.remove(idleChannel.channel);
193+
}
194+
}
188195
}
189196

190197
if (idleChannel == null) {

0 commit comments

Comments
 (0)