Skip to content

Commit

Permalink
fixes #494 properly set the connection ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Sep 20, 2023
1 parent a37216a commit c5bcce7
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions unirest/src/main/java/kong/unirest/core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

public class Config {
public static final int DEFAULT_CONNECT_TIMEOUT = 10000;
public static final String JDK_HTTPCLIENT_KEEPALIVE_TIMEOUT = "jdk.httpclient.keepalive.timeout";

private Optional<Client> client = Optional.empty();
private Supplier<ObjectMapper> objectMapper;
Expand All @@ -62,7 +63,6 @@ public class Config {
private Supplier<String> keystorePassword = () -> null;
private String cookieSpec;
private UniMetric metrics = new NoopMetric();
private long ttl = -1;
private SSLContext sslContext;
private String[] ciphers;
private String[] protocols;
Expand Down Expand Up @@ -501,18 +501,37 @@ public Config setDefaultResponseEncoding(String value) {
}

/**
* Total time to live (TTL) defines maximum life span of persistent connections regardless of their expiration setting.
* No persistent connection will be re-used past its TTL value.
* Sets the jdk.httpclient.keepalive.timeout setting
* https://docs.oracle.com/en/java/javase/20/docs/api/java.net.http/module-summary.html
* The number of seconds to keep idle HTTP connections alive in the keep alive cache.
* This property applies to both HTTP/1.1 and HTTP/2.
*
* @param duration of ttl.
* @param unit the time unit of the ttl
* @return this config object
*/
public Config connectionTTL(long duration, TimeUnit unit) {
this.ttl = unit.toMillis(duration);
Objects.requireNonNull(unit, "TimeUnit required");
var ttl = unit.toMillis(duration);
if(ttl > -1){
System.setProperty(JDK_HTTPCLIENT_KEEPALIVE_TIMEOUT, String.valueOf(ttl));
}
return this;
}

/**
* Sets the jdk.httpclient.keepalive.timeout setting
* https://docs.oracle.com/en/java/javase/20/docs/api/java.net.http/module-summary.html
* The number of seconds to keep idle HTTP connections alive in the keep alive cache.
* This property applies to both HTTP/1.1 and HTTP/2.
*
* @param duration of ttl.
* @return this config object
*/
public Config connectionTTL(Duration duration){
return connectionTTL(duration.toMillis(), TimeUnit.MILLISECONDS);
}

/**
* Automatically retry synchronous requests on 429/529 responses with the Retry-After response header
* Default is false
Expand All @@ -538,19 +557,6 @@ public Config retryAfter(boolean value, int maxRetryAttempts) {
return this;
}

/**
* Sugar!
* Total time to live (TTL) defines maximum life span of persistent connections regardless of their expiration setting.
* No persistent connection will be re-used past its TTL value.
*
* @param duration of ttl.
* @return this config object
*/
public Config connectionTTL(Duration duration){
this.ttl = duration.toMillis();
return this;
}

/**
* Requests a specific HTTP protocol version where possible.
*
Expand Down Expand Up @@ -783,13 +789,6 @@ public UniMetric getMetric() {
return metrics;
}

/**
* @return the maximum life span of persistent connections regardless of their expiration setting.
*/
public long getTTL() {
return ttl;
}

/**
* @return the currently configured Interceptor
*/
Expand Down Expand Up @@ -848,4 +847,16 @@ public boolean isAutomaticRetryAfter(){
public int maxRetries() {
return maxRetries;
}

/**
* @return the maximum life span of persistent connections regardless of their expiration setting.
*/
public long getTTL() {
try {
return Long.parseLong(System.getProperty(JDK_HTTPCLIENT_KEEPALIVE_TIMEOUT));
}catch (NumberFormatException e){
return -1;
}
}

}

0 comments on commit c5bcce7

Please sign in to comment.