Skip to content

Commit

Permalink
Add 'Duration' as flavor to configure TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloglima committed Apr 9, 2020
1 parent 18f46ed commit 62453a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ Changing Unirest's config should ideally be done once, or rarely. There are seve
| ```clientCertificateStore(String,String)``` | Add a PKCS12 KeyStore by path for doing client certificates | |
| ```clientCertificateStore(KeyStore,String)``` | Add a PKCS12 KeyStore for doing client certificates | |
| ```connectionTTL(long,TimeUnit)``` | 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.| -1 |
| ```connectionTTL(Duration)``` | Add total time to live (TTL) by [Duration](https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html). Good for moderns Java APIs. | -1 |
| ```errorHandler(Consumer<HttpResponse<?>> consumer)``` | Set a global error handler that will be invoked for any status > 400 or a parsing error | |
| ```interceptor(Interceptor value)``` | Set a global Interceptor handler that will be invoked before and after each request | |
| ```hostNameVerifier(HostNameVerifier value)``` | Set a custom HostNameVerifier for the security configuration | DefaultHostNameVerifier |
Expand Down
15 changes: 15 additions & 0 deletions unirest/src/main/java/kong/unirest/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -561,6 +562,20 @@ public Config connectionTTL(long duration, TimeUnit unit) {
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;
}

/**
* Register the client with a system shutdown hook. Note that this creates up to two threads
* (depending on if you use both sync and async clients). default is false
Expand Down
7 changes: 5 additions & 2 deletions unirest/src/test/java/kong/unirest/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.nio.client.HttpAsyncClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand All @@ -46,6 +44,7 @@
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyStore;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -149,8 +148,12 @@ public void testShutdown() throws IOException {
@Test
public void settingTTl() {
assertEquals(-1, config.getTTL());

assertEquals(42, config.connectionTTL(42, TimeUnit.MILLISECONDS).getTTL());
assertEquals(2520000, config.connectionTTL(42, TimeUnit.MINUTES).getTTL());

assertEquals(43, config.connectionTTL(Duration.ofMillis(43)).getTTL());
assertEquals(2580000, config.connectionTTL(Duration.ofMinutes(43)).getTTL());
}

@Test
Expand Down

0 comments on commit 62453a7

Please sign in to comment.