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,11 +487,12 @@ 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 * }
488497 * </pre>
489498 *
@@ -550,6 +559,36 @@ public Builder setTimeout(int timeout) {
550559 return this;
551560 }
552561
562+
563+ /**
564+ * Create a new connection pool with tuning parameters appropriate for a single-user application.
565+ * The tuning parameters in this pool are subject to change in future OkHttp releases. Currently,
566+ * this pool holds up to 5 idle connections which will be evicted after 5 minutes of inactivity.
567+ * <p>
568+ * <p>
569+ * public ConnectionPool() {
570+ * this(5, 5, TimeUnit.MINUTES);
571+ * }
572+ *
573+ * @param maxIdleConnections Maximum number of idle connections
574+ * @param keepAliveDuration The Keep Alive Duration
575+ * @param timeUnit A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units
576+ * @return instance of Builder
577+ * <p>
578+ * Example:
579+ * {@code
580+ * Contentstack cs = new Contentstack.Builder()
581+ * .setAuthtoken(AUTHTOKEN)
582+ * .setConnectionPool(5, 400, TimeUnit.MILLISECONDS)
583+ * .setHost("host")
584+ * .build();
585+ * Connection}
586+ */
587+ public Builder setConnectionPool(int maxIdleConnections, int keepAliveDuration, TimeUnit timeUnit) {
588+ this.connectionPool = new ConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit);
589+ return this;
590+ }
591+
553592 /**
554593 * Sets authtoken for the client
555594 *
@@ -582,7 +621,9 @@ private void validateClient(Contentstack contentstack) {
582621
583622 private OkHttpClient httpClient(Contentstack contentstack, Boolean retryOnFailure) {
584623 this.authInterceptor = contentstack.interceptor = new AuthInterceptor();
585- return new OkHttpClient.Builder().addInterceptor(this.authInterceptor)
624+ return new OkHttpClient.Builder()
625+ .connectionPool(this.connectionPool)
626+ .addInterceptor(this.authInterceptor)
586627 .addInterceptor(logger())
587628 .proxy(this.proxy)
588629 .connectTimeout(Duration.ofSeconds(this.timeout))
0 commit comments