@@ -33,8 +33,14 @@ public class DefaultClient implements Client {
3333 public static final String API_TIMEOUT_PROP_NAME = "io.getstream.chat.timeout" ;
3434 public static final String API_URL_PROP_NAME = "io.getstream.chat.url" ;
3535 public static final String X_STREAM_EXT_PROP_NAME = "io.getstream.chat.xStreamExt" ;
36+ public static final String CONNECTION_POOL_MAX_IDLE_CONNECTIONS_PROP_NAME =
37+ "io.getstream.chat.connectionPool.maxIdleConnections" ;
38+ public static final String CONNECTION_POOL_KEEP_ALIVE_DURATION_PROP_NAME =
39+ "io.getstream.chat.connectionPool.keepAliveDurationMs" ;
3640
3741 private static final String API_DEFAULT_URL = "https://chat.stream-io-api.com" ;
42+ private static final int DEFAULT_MAX_IDLE_CONNECTIONS = 5 ;
43+ private static final long DEFAULT_KEEP_ALIVE_DURATION_MS = 59_000L ;
3844 private static volatile DefaultClient defaultInstance ;
3945 @ NotNull private final String apiSecret ;
4046 @ NotNull private final String apiKey ;
@@ -98,7 +104,7 @@ public DefaultClient(
98104 private OkHttpClient buildOkHttpClient () {
99105 OkHttpClient .Builder httpClient =
100106 new OkHttpClient .Builder ()
101- .connectionPool (new ConnectionPool ( 5 , 59 , TimeUnit . SECONDS ))
107+ .connectionPool (buildConnectionPool ( extendedProperties ))
102108 .callTimeout (getStreamChatTimeout (extendedProperties ), TimeUnit .MILLISECONDS );
103109 httpClient .interceptors ().clear ();
104110
@@ -191,6 +197,24 @@ public void setTimeout(@NotNull Duration timeoutDuration) {
191197 this .serviceFactory = serviceFactoryBuilder .apply (retrofit );
192198 }
193199
200+ @ Override
201+ public void setConnectionPool (int maxIdleConnections , @ NotNull Duration keepAliveDuration ) {
202+ if (maxIdleConnections < 0 ) {
203+ throw new IllegalArgumentException ("maxIdleConnections must be >= 0" );
204+ }
205+ if (keepAliveDuration .isNegative ()) {
206+ throw new IllegalArgumentException ("keepAliveDuration must be >= 0" );
207+ }
208+
209+ extendedProperties .setProperty (
210+ CONNECTION_POOL_MAX_IDLE_CONNECTIONS_PROP_NAME , Integer .toString (maxIdleConnections ));
211+ extendedProperties .setProperty (
212+ CONNECTION_POOL_KEEP_ALIVE_DURATION_PROP_NAME ,
213+ Long .toString (keepAliveDuration .toMillis ()));
214+ this .retrofit = buildRetrofitClient (buildOkHttpClient ());
215+ this .serviceFactory = serviceFactoryBuilder .apply (retrofit );
216+ }
217+
194218 private static @ NotNull String jwtToken (String apiSecret ) {
195219 Key signingKey =
196220 new SecretKeySpec (
@@ -233,6 +257,24 @@ private static Properties extendProperties(Properties properties) {
233257 canformedProperties .put (API_TIMEOUT_PROP_NAME , envTimeout );
234258 }
235259
260+ var envConnectionPoolMaxIdleConnections =
261+ env .getOrDefault (
262+ "STREAM_CHAT_CONNECTION_POOL_MAX_IDLE_CONNECTIONS" ,
263+ System .getProperty ("STREAM_CHAT_CONNECTION_POOL_MAX_IDLE_CONNECTIONS" ));
264+ if (envConnectionPoolMaxIdleConnections != null ) {
265+ canformedProperties .put (
266+ CONNECTION_POOL_MAX_IDLE_CONNECTIONS_PROP_NAME , envConnectionPoolMaxIdleConnections );
267+ }
268+
269+ var envConnectionPoolKeepAliveDuration =
270+ env .getOrDefault (
271+ "STREAM_CHAT_CONNECTION_POOL_KEEP_ALIVE_DURATION_MS" ,
272+ System .getProperty ("STREAM_CHAT_CONNECTION_POOL_KEEP_ALIVE_DURATION_MS" ));
273+ if (envConnectionPoolKeepAliveDuration != null ) {
274+ canformedProperties .put (
275+ CONNECTION_POOL_KEEP_ALIVE_DURATION_PROP_NAME , envConnectionPoolKeepAliveDuration );
276+ }
277+
236278 var envApiUrl = env .getOrDefault ("STREAM_CHAT_URL" , System .getProperty ("STREAM_CHAT_URL" ));
237279 if (envApiUrl != null ) {
238280 canformedProperties .put (API_URL_PROP_NAME , envApiUrl );
@@ -255,6 +297,37 @@ private static long getStreamChatTimeout(@NotNull Properties properties) {
255297 return Long .parseLong (timeout .toString ());
256298 }
257299
300+ private static @ NotNull ConnectionPool buildConnectionPool (@ NotNull Properties properties ) {
301+ return new ConnectionPool (
302+ getConnectionPoolMaxIdleConnections (properties ),
303+ getConnectionPoolKeepAliveDurationMs (properties ),
304+ TimeUnit .MILLISECONDS );
305+ }
306+
307+ private static int getConnectionPoolMaxIdleConnections (@ NotNull Properties properties ) {
308+ var maxIdleConnections =
309+ properties .getOrDefault (
310+ CONNECTION_POOL_MAX_IDLE_CONNECTIONS_PROP_NAME , DEFAULT_MAX_IDLE_CONNECTIONS );
311+ int parsedMaxIdleConnections = Integer .parseInt (maxIdleConnections .toString ());
312+ if (parsedMaxIdleConnections < 0 ) {
313+ throw new IllegalArgumentException (
314+ CONNECTION_POOL_MAX_IDLE_CONNECTIONS_PROP_NAME + " must be >= 0" );
315+ }
316+ return parsedMaxIdleConnections ;
317+ }
318+
319+ private static long getConnectionPoolKeepAliveDurationMs (@ NotNull Properties properties ) {
320+ var keepAliveDuration =
321+ properties .getOrDefault (
322+ CONNECTION_POOL_KEEP_ALIVE_DURATION_PROP_NAME , DEFAULT_KEEP_ALIVE_DURATION_MS );
323+ long parsedKeepAliveDuration = Long .parseLong (keepAliveDuration .toString ());
324+ if (parsedKeepAliveDuration < 0 ) {
325+ throw new IllegalArgumentException (
326+ CONNECTION_POOL_KEEP_ALIVE_DURATION_PROP_NAME + " must be >= 0" );
327+ }
328+ return parsedKeepAliveDuration ;
329+ }
330+
258331 private static String getStreamChatBaseUrl (@ NotNull Properties properties ) {
259332 var url = properties .getOrDefault (API_URL_PROP_NAME , API_DEFAULT_URL );
260333 return url .toString ();
0 commit comments