Skip to content

Commit 8e3990e

Browse files
committed
2 parents ee371ed + 7fb4fa1 commit 8e3990e

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
import java.util.Iterator;
101101
import java.util.List;
102102
import java.util.Map.Entry;
103-
import java.util.concurrent.Callable;
104103
import java.util.concurrent.ConcurrentHashMap;
105104
import java.util.concurrent.Executors;
106105
import java.util.concurrent.Future;
@@ -315,17 +314,20 @@ public void operationComplete(ChannelFuture cf) {
315314

316315
try {
317316
future.touch();
318-
future.setReaperFuture(config.reaper().scheduleAtFixedRate(new Runnable() {
319-
public void run() {
320-
if (future.hasExpired()) {
321-
if (log.isDebugEnabled()) {
322-
log.debug("Request Timeout expired for " + future);
317+
int delay = requestTimeout(config, future.getRequest().getPerRequestConfig());
318+
if (delay != -1) {
319+
future.setReaperFuture(config.reaper().scheduleAtFixedRate(new Runnable() {
320+
public void run() {
321+
if (future.hasExpired()) {
322+
if (log.isDebugEnabled()) {
323+
log.debug("Request Timeout expired for " + future);
324+
}
325+
future.abort(new TimeoutException("Request timed out."));
326+
closeChannel(channel.getPipeline().getContext(NettyAsyncHttpProvider.class));
323327
}
324-
future.abort(new TimeoutException("Request timed out."));
325-
closeChannel(channel.getPipeline().getContext(NettyAsyncHttpProvider.class));
326328
}
327-
}
328-
}, 0, requestTimeout(config, future.getRequest().getPerRequestConfig()), TimeUnit.MILLISECONDS));
329+
}, 0, delay, TimeUnit.MILLISECONDS));
330+
}
329331
} catch (RejectedExecutionException ex) {
330332
future.abort(ex);
331333
}

src/main/java/com/ning/http/client/providers/NettyResponseFuture.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ public boolean cancel(boolean force) {
107107

108108
/**
109109
* Is the Future still valid
110+
*
111+
* @return <code>true</code> iff response has expired and should be terminated.
110112
*/
111113
public boolean hasExpired(){
112-
return ((System.currentTimeMillis() - touch.get()) > responseTimeoutInMs );
114+
return responseTimeoutInMs != -1 && ((System.currentTimeMillis() - touch.get()) > responseTimeoutInMs);
113115
}
114116

115117
/**
@@ -130,7 +132,13 @@ public V get() throws InterruptedException, ExecutionException{
130132
/* @Override */
131133
public V get(long l, TimeUnit tu) throws InterruptedException, TimeoutException, ExecutionException {
132134
if (!isDone() && !isCancelled()) {
133-
if (!latch.await(l, tu)) {
135+
boolean failed = false;
136+
if (l == -1) {
137+
latch.await();
138+
} else {
139+
failed = !latch.await(l, tu);
140+
}
141+
if (failed) {
134142
isCancelled.set(true);
135143
TimeoutException te = new TimeoutException("No response received");
136144
try {

src/test/java/com/ning/http/client/async/PerRequestTimeoutTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.concurrent.TimeoutException;
3434

3535
import static org.testng.Assert.assertEquals;
36+
import static org.testng.Assert.assertNotNull;
3637
import static org.testng.Assert.assertNull;
3738
import static org.testng.Assert.assertTrue;
3839
import static org.testng.Assert.fail;
@@ -86,6 +87,25 @@ public void testRequestTimeout() throws IOException {
8687
}
8788
}
8889

90+
@Test(groups = "standalone")
91+
public void testGlobalDefaultPerRequestInfiniteTimeout() throws IOException {
92+
AsyncHttpClient client = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeoutInMs(100).build());
93+
PerRequestConfig requestConfig = new PerRequestConfig();
94+
requestConfig.setRequestTimeoutInMs(-1);
95+
Future<Response> responseFuture =
96+
client.prepareGet(getTargetUrl()).setPerRequestConfig(requestConfig).execute();
97+
try {
98+
Response response = responseFuture.get();
99+
assertNotNull(response);
100+
client.close();
101+
} catch (InterruptedException e) {
102+
fail("Interrupted.", e);
103+
} catch (ExecutionException e) {
104+
assertTrue(e.getCause() instanceof TimeoutException);
105+
assertEquals(e.getCause().getMessage(), "Request timed out.");
106+
}
107+
}
108+
89109
@Test(groups = "standalone")
90110
public void testGlobalRequestTimeout() throws IOException {
91111
AsyncHttpClient client = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeoutInMs(100).build());

0 commit comments

Comments
 (0)