Skip to content

Commit

Permalink
Convenient timeout configuration for the HTTP client.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jun 6, 2017
1 parent 3295471 commit b9c7549
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Expand Up @@ -57,6 +57,8 @@ public class HttpClient extends RapidoidThing {

private volatile int maxRedirects = 5;

private volatile int timeout = 5000;

private final Map<String, String> cookies = Coll.synchronizedMap();

private final LazyInit<CloseableHttpAsyncClient> client = new LazyInit<CloseableHttpAsyncClient>(
Expand Down Expand Up @@ -179,6 +181,15 @@ public Map<String, String> cookies() {
return this.cookies;
}

public int timeout() {
return timeout;
}

public HttpClient timeout(int timeout) {
this.timeout = timeout;
return this;
}

public HttpReq req() {
return new HttpReq(this);
}
Expand Down
Expand Up @@ -59,14 +59,17 @@ public class HttpReq extends RapidoidThing {

private volatile boolean raw = false;

private volatile int socketTimeout = 5000;
private volatile int socketTimeout;

private volatile int connectTimeout = 5000;
private volatile int connectTimeout;

private volatile int connectionRequestTimeout = 5000;
private volatile int connectionRequestTimeout;

public HttpReq(HttpClient client) {
this.client = client;
this.socketTimeout = client.timeout();
this.connectTimeout = client.timeout();
this.connectionRequestTimeout = client.timeout();
}

public HttpReq verb(HttpVerb verb) {
Expand Down
Expand Up @@ -82,7 +82,7 @@ public void run() {

private void checkNoCookies(boolean keepCookies) {
HttpClient client;
client = HTTP.client().keepCookies(keepCookies);
client = HTTP.client().keepCookies(keepCookies).timeout(10000);

eq(client.get(localhost("/a")).fetch(), "0");
eq(client.get(localhost("/b")).fetch(), "0");
Expand Down

0 comments on commit b9c7549

Please sign in to comment.