Skip to content

Commit 96c2e8f

Browse files
authored
Merge pull request #348 from daniloglima/feature/duration-ttl
Add 'Duration' as flavor to configure TTL
2 parents 2d99b22 + 62453a7 commit 96c2e8f

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ Changing Unirest's config should ideally be done once, or rarely. There are seve
514514
| ```clientCertificateStore(String,String)``` | Add a PKCS12 KeyStore by path for doing client certificates | |
515515
| ```clientCertificateStore(KeyStore,String)``` | Add a PKCS12 KeyStore for doing client certificates | |
516516
| ```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 |
517+
| ```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 |
517518
| ```errorHandler(Consumer<HttpResponse<?>> consumer)``` | Set a global error handler that will be invoked for any status > 400 or a parsing error | |
518519
| ```interceptor(Interceptor value)``` | Set a global Interceptor handler that will be invoked before and after each request | |
519520
| ```hostNameVerifier(HostNameVerifier value)``` | Set a custom HostNameVerifier for the security configuration | DefaultHostNameVerifier |

unirest/src/main/java/kong/unirest/Config.java

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.InputStream;
3737
import java.nio.charset.StandardCharsets;
3838
import java.security.KeyStore;
39+
import java.time.Duration;
3940
import java.util.ArrayList;
4041
import java.util.List;
4142
import java.util.Objects;
@@ -561,6 +562,20 @@ public Config connectionTTL(long duration, TimeUnit unit) {
561562
return this;
562563
}
563564

565+
566+
/**
567+
* Sugar!
568+
* Total time to live (TTL) defines maximum life span of persistent connections regardless of their expiration setting.
569+
* No persistent connection will be re-used past its TTL value.
570+
*
571+
* @param duration of ttl.
572+
* @return this config object
573+
*/
574+
public Config connectionTTL(Duration duration){
575+
this.ttl = duration.toMillis();
576+
return this;
577+
}
578+
564579
/**
565580
* Register the client with a system shutdown hook. Note that this creates up to two threads
566581
* (depending on if you use both sync and async clients). default is false

unirest/src/test/java/kong/unirest/ConfigTest.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
3636
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
3737
import org.apache.http.nio.client.HttpAsyncClient;
38-
import org.junit.After;
39-
import org.junit.Before;
4038
import org.junit.Test;
4139
import org.junit.runner.RunWith;
4240
import org.mockito.InjectMocks;
@@ -46,6 +44,7 @@
4644
import javax.net.ssl.SSLContext;
4745
import java.io.IOException;
4846
import java.security.KeyStore;
47+
import java.time.Duration;
4948
import java.util.concurrent.TimeUnit;
5049

5150
import static org.junit.Assert.*;
@@ -149,8 +148,12 @@ public void testShutdown() throws IOException {
149148
@Test
150149
public void settingTTl() {
151150
assertEquals(-1, config.getTTL());
151+
152152
assertEquals(42, config.connectionTTL(42, TimeUnit.MILLISECONDS).getTTL());
153153
assertEquals(2520000, config.connectionTTL(42, TimeUnit.MINUTES).getTTL());
154+
155+
assertEquals(43, config.connectionTTL(Duration.ofMillis(43)).getTTL());
156+
assertEquals(2580000, config.connectionTTL(Duration.ofMinutes(43)).getTTL());
154157
}
155158

156159
@Test

0 commit comments

Comments
 (0)