From 98b720cd55541c64811ac16c5320cc32323ae3ce Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Tue, 30 May 2017 09:15:33 -0500 Subject: [PATCH] Adds ability to configure some HTTP transport behaivior --- .../src/main/java/com/sparkpost/Client.java | 49 +++++++++++++++++++ .../sparkpost/transport/RestConnection.java | 8 ++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/Client.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/Client.java index 0b2cbc9..74bc043 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/Client.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/Client.java @@ -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() { } @@ -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 diff --git a/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java b/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java index b7da975..c23b9b6 100644 --- a/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java +++ b/libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java @@ -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())) { @@ -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(); } } @@ -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(); } }