Skip to content

Commit 98b720c

Browse files
author
Chris Wilson
committed
Adds ability to configure some HTTP transport behaivior
1 parent ce1bf73 commit 98b720c

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

libs/sparkpost-lib/src/main/java/com/sparkpost/Client.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public class Client {
3434

3535
private String fromEmail;
3636

37+
private boolean disconnectAfterRequest = false;
38+
39+
private int httpConnectTimeout = 0; // 0 - system default
40+
41+
private int httpReadTimeout = 0; // 0 - system default
42+
3743
public Client() {
3844
}
3945

@@ -78,6 +84,49 @@ public String getFromEmail() {
7884
return this.fromEmail;
7985
}
8086

87+
/**
88+
* If true will be more aggressive about disconnecting idle HTTP connections
89+
*
90+
* @return true
91+
*/
92+
public boolean isDisconnectAfterRequest() {
93+
return this.disconnectAfterRequest;
94+
}
95+
96+
/**
97+
* If true the underlying HTTP transport will be more aggressive about closing idle HTTP connection so may not resuse TCP sockets as much.
98+
*
99+
* @param disconnectAfterRequest
100+
* default is false
101+
*/
102+
public void setDisconnectAfterRequest(boolean disconnectAfterRequest) {
103+
this.disconnectAfterRequest = disconnectAfterRequest;
104+
}
105+
106+
public int getHttpConnectTimeout() {
107+
return this.httpConnectTimeout;
108+
}
109+
110+
/**
111+
* Sets a specified timeout value, in milliseconds, to be used
112+
* when opening a communications link to the resource referenced
113+
* by this URLConnection. If the timeout expires before the
114+
* connection can be established, a
115+
* java.net.SocketTimeoutException is raised. A timeout of zero is
116+
* interpreted as an infinite timeout.
117+
**/
118+
public void setHttpConnectTimeout(int timeout) {
119+
this.httpConnectTimeout = timeout;
120+
}
121+
122+
public int getHttpReadTimeout() {
123+
return this.httpReadTimeout;
124+
}
125+
126+
public void setHttpReadTimeout(int httpReadTimeout) {
127+
this.httpReadTimeout = httpReadTimeout;
128+
}
129+
81130
/**
82131
* @param fromEmail
83132
* the fromEmail to set

libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ private HttpURLConnection createConnectionObject(String path, Method method) thr
117117
// got one of its streams)
118118
conn = (HttpURLConnection) url.openConnection();
119119

120+
conn.setConnectTimeout(this.client.getHttpConnectTimeout());
121+
conn.setReadTimeout(this.client.getHttpReadTimeout());
122+
120123
if (StringUtils.isNotEmpty(this.client.getAuthKey())) {
121124
conn.setRequestProperty("Authorization", this.client.getAuthKey());
122125
} 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
412415

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

435439
return response;
436440
} finally {
437-
if (conn != null) {
441+
if (this.client.isDisconnectAfterRequest() && conn != null) {
438442
conn.disconnect();
439443
}
440444
}

0 commit comments

Comments
 (0)