Skip to content

Commit

Permalink
Adds ability to configure some HTTP transport behaivior
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Wilson committed May 30, 2017
1 parent ce1bf73 commit 98b720c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
49 changes: 49 additions & 0 deletions libs/sparkpost-lib/src/main/java/com/sparkpost/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public class Client {

private String fromEmail;

private boolean disconnectAfterRequest = false;

private int httpConnectTimeout = 0; // 0 - system default

private int httpReadTimeout = 0; // 0 - system default

public Client() {
}

Expand Down Expand Up @@ -78,6 +84,49 @@ public String getFromEmail() {
return this.fromEmail;
}

/**
* If true will be more aggressive about disconnecting idle HTTP connections
*
* @return true
*/
public boolean isDisconnectAfterRequest() {
return this.disconnectAfterRequest;
}

/**
* If true the underlying HTTP transport will be more aggressive about closing idle HTTP connection so may not resuse TCP sockets as much.
*
* @param disconnectAfterRequest
* default is false
*/
public void setDisconnectAfterRequest(boolean disconnectAfterRequest) {
this.disconnectAfterRequest = disconnectAfterRequest;
}

public int getHttpConnectTimeout() {
return this.httpConnectTimeout;
}

/**
* Sets a specified timeout value, in milliseconds, to be used
* when opening a communications link to the resource referenced
* by this URLConnection. If the timeout expires before the
* connection can be established, a
* java.net.SocketTimeoutException is raised. A timeout of zero is
* interpreted as an infinite timeout.
**/
public void setHttpConnectTimeout(int timeout) {
this.httpConnectTimeout = timeout;
}

public int getHttpReadTimeout() {
return this.httpReadTimeout;
}

public void setHttpReadTimeout(int httpReadTimeout) {
this.httpReadTimeout = httpReadTimeout;
}

/**
* @param fromEmail
* the fromEmail to set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ private HttpURLConnection createConnectionObject(String path, Method method) thr
// got one of its streams)
conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(this.client.getHttpConnectTimeout());
conn.setReadTimeout(this.client.getHttpReadTimeout());

if (StringUtils.isNotEmpty(this.client.getAuthKey())) {
conn.setRequestProperty("Authorization", this.client.getAuthKey());
} else if (StringUtils.isNotEmpty(this.client.getUsername()) && StringUtils.isNotEmpty(this.client.getPassword())) {
Expand Down Expand Up @@ -412,7 +415,8 @@ private Response doHttpMethod(Endpoint endpoint, Method method, String data, Res

return response;
} finally {
if (conn != null) {
if (this.client.isDisconnectAfterRequest() && conn != null) {
// Disconnect will more aggressively close idle connections but it is preferred to cache as much as possible
conn.disconnect();
}
}
Expand All @@ -434,7 +438,7 @@ private Response doHttpMethod(String path, Method method, String data, Response

return response;
} finally {
if (conn != null) {
if (this.client.isDisconnectAfterRequest() && conn != null) {
conn.disconnect();
}
}
Expand Down

0 comments on commit 98b720c

Please sign in to comment.