88import com .contentstack .cms .stack .Stack ;
99import com .contentstack .cms .user .User ;
1010import com .google .gson .Gson ;
11+ import okhttp3 .ConnectionPool ;
1112import okhttp3 .OkHttpClient ;
1213import okhttp3 .ResponseBody ;
1314import okhttp3 .logging .HttpLoggingInterceptor ;
2223import java .util .HashMap ;
2324import java .util .Map ;
2425import java .util .Objects ;
26+ import java .util .concurrent .TimeUnit ;
2527import java .util .logging .Logger ;
2628
2729import static com .contentstack .cms .core .Util .*;
@@ -467,6 +469,12 @@ public static class Builder {
467469 private int timeout = Util .TIMEOUT ; // Default timeout 30 seconds
468470 private Boolean retry = Util .RETRY_ON_FAILURE ;// Default base url for contentstack
469471
472+ /**
473+ * Default ConnectionPool holds up to 5 idle connections which
474+ * will be evicted after 5 minutes of inactivity.
475+ */
476+ private ConnectionPool connectionPool = new ConnectionPool (); // Connection
477+
470478 /**
471479 * Instantiates a new Builder.
472480 */
@@ -479,14 +487,13 @@ public Builder() {
479487 * Proxy(Proxy.Type.HTTP, new
480488 * InetSocketAddress(proxyHost, proxyPort));
481489 * <br>
482- *
483- * <pre>
484- * {
485- * Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("hostname", 433));
486- * Contentstack contentstack = new Contentstack.Builder().setProxy(proxy).build();
490+ * <p>
491+ * {@code
492+ * <p>
493+ * Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("hostname", 433));
494+ * Contentstack contentstack = new Contentstack.Builder().setProxy(proxy).build();
495+ * <p>
487496 * }
488- * </pre>
489- *
490497 * @param proxy the proxy
491498 * @return the Builder instance
492499 */
@@ -550,6 +557,36 @@ public Builder setTimeout(int timeout) {
550557 return this ;
551558 }
552559
560+
561+ /**
562+ * Create a new connection pool with tuning parameters appropriate for a single-user application.
563+ * The tuning parameters in this pool are subject to change in future OkHttp releases. Currently,
564+ * this pool holds up to 5 idle connections which will be evicted after 5 minutes of inactivity.
565+ * <p>
566+ * <p>
567+ * public ConnectionPool() {
568+ * this(5, 5, TimeUnit.MINUTES);
569+ * }
570+ *
571+ * @param maxIdleConnections Maximum number of idle connections
572+ * @param keepAliveDuration The Keep Alive Duration
573+ * @param timeUnit A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units
574+ * @return instance of Builder
575+ * <p>
576+ * Example:
577+ * {@code
578+ * Contentstack cs = new Contentstack.Builder()
579+ * .setAuthtoken(AUTHTOKEN)
580+ * .setConnectionPool(5, 400, TimeUnit.MILLISECONDS)
581+ * .setHost("host")
582+ * .build();
583+ * Connection}
584+ */
585+ public Builder setConnectionPool (int maxIdleConnections , int keepAliveDuration , TimeUnit timeUnit ) {
586+ this .connectionPool = new ConnectionPool (maxIdleConnections , keepAliveDuration , timeUnit );
587+ return this ;
588+ }
589+
553590 /**
554591 * Sets authtoken for the client
555592 *
@@ -582,7 +619,9 @@ private void validateClient(Contentstack contentstack) {
582619
583620 private OkHttpClient httpClient (Contentstack contentstack , Boolean retryOnFailure ) {
584621 this .authInterceptor = contentstack .interceptor = new AuthInterceptor ();
585- return new OkHttpClient .Builder ().addInterceptor (this .authInterceptor )
622+ return new OkHttpClient .Builder ()
623+ .connectionPool (this .connectionPool )
624+ .addInterceptor (this .authInterceptor )
586625 .addInterceptor (logger ())
587626 .proxy (this .proxy )
588627 .connectTimeout (Duration .ofSeconds (this .timeout ))
0 commit comments