Skip to content

Commit aafd735

Browse files
committed
free semaphore permit is future is cancelled or done before channel is added to cleanup group
1 parent 568fc84 commit aafd735

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,10 @@ private <T> ListenableFuture<T> doConnect(final Request request, final AsyncHand
11361136
if (!c.future().isCancelled() || !c.future().isDone()) {
11371137
openChannels.add(channelFuture.getChannel());
11381138
c.future().attachChannel(channelFuture.getChannel(), false);
1139+
} else {
1140+
if (acquiredConnection) {
1141+
freeConnections.release();
1142+
}
11391143
}
11401144
return c.future();
11411145
}

src/test/java/com/ning/http/client/async/netty/NettyConnectionPoolTest.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
import static org.testng.Assert.assertEquals;
1616
import static org.testng.Assert.assertNotNull;
1717
import static org.testng.Assert.assertNull;
18-
18+
import static org.testng.Assert.assertTrue;
19+
import static org.testng.Assert.fail;
20+
import java.net.ConnectException;
1921
import java.util.concurrent.TimeUnit;
2022

23+
import com.ning.http.client.Response;
24+
import com.ning.http.client.providers.netty.NettyAsyncHttpProviderConfig;
2125
import org.jboss.netty.channel.Channel;
2226
import org.testng.annotations.Test;
2327

@@ -116,4 +120,36 @@ public void destroy() {
116120
client.close();
117121
}
118122
}
123+
124+
@Test
125+
public void testHostNotContactable() {
126+
NettyAsyncHttpProviderConfig conf = new NettyAsyncHttpProviderConfig();
127+
conf.addProperty(NettyAsyncHttpProviderConfig.EXECUTE_ASYNC_CONNECT,false);
128+
AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setAsyncHttpClientProviderConfig(conf)
129+
.setAllowPoolingConnection(true).setMaximumConnectionsTotal(1).build());
130+
try {
131+
String url = null;
132+
try {
133+
url = "http://127.0.0.1:" + findFreePort();
134+
} catch (Exception e) {
135+
fail("unable to find free port to simulate downed host");
136+
}
137+
int i;
138+
for (i = 0; i < 2; i++) {
139+
try {
140+
log.info("{} requesting url [{}]...", i, url);
141+
Response response = client.prepareGet(url).execute().get();
142+
log.info("{} response [{}].", i, response);
143+
} catch (Exception ex) {
144+
assertNotNull(ex.getCause());
145+
Throwable cause = ex.getCause();
146+
assertTrue(cause instanceof ConnectException);
147+
}
148+
}
149+
} finally {
150+
client.close();
151+
}
152+
}
153+
154+
119155
}

0 commit comments

Comments
 (0)