diff --git a/ballerina/http_client_connection_pool.bal b/ballerina/http_client_connection_pool.bal index c2598bca7f..06e3636fa4 100644 --- a/ballerina/http_client_connection_pool.bal +++ b/ballerina/http_client_connection_pool.bal @@ -27,11 +27,15 @@ configurable int maxActiveStreamsPerConnection = 100; # + maxIdleConnections - Maximum number of idle connections allowed per pool. # + waitTime - Maximum amount of time (in seconds), the client should wait for an idle connection before it sends an error when the pool is exhausted # + maxActiveStreamsPerConnection - Maximum active streams per connection. This only applies to HTTP/2. Default value is 100 +# + minEvictableIdleTime - Minimum evictable time for an idle connection in seconds. Default value is 5 minutes +# + timeBetweenEvictionRuns - Time between eviction runs in seconds. Default value is 30 seconds public type PoolConfiguration record {| int maxActiveConnections = maxActiveConnections; int maxIdleConnections = maxIdleConnections; decimal waitTime = waitTime; int maxActiveStreamsPerConnection = maxActiveStreamsPerConnection; + decimal minEvictableIdleTime = 300; + decimal timeBetweenEvictionRuns = 30; |}; //This is a hack to get the global map initialized, without involving locking. class ConnectionManager { diff --git a/changelog.md b/changelog.md index 897f530327..5254d809db 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,11 @@ This file contains all the notable changes done to the Ballerina HTTP package th The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added +- [Expose HTTP connection eviction configurations in the client level](https://github.com/ballerina-platform/ballerina-library/issues/5951) + ## [2.10.5] - 2023-12-06 ### Fixed diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java index dc5f4fc2f6..286e621115 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java @@ -506,6 +506,10 @@ public final class HttpConstants { public static final BString CONNECTION_POOLING_WAIT_TIME = StringUtils.fromString("waitTime"); public static final BString CONNECTION_POOLING_MAX_ACTIVE_STREAMS_PER_CONNECTION = StringUtils.fromString( "maxActiveStreamsPerConnection"); + public static final BString CONNECTION_POOLING_EVICTABLE_IDLE_TIME = StringUtils.fromString( + "minEvictableIdleTime"); + public static final BString CONNECTION_POOLING_TIME_BETWEEN_EVICTION_RUNS = StringUtils.fromString( + "timeBetweenEvictionRuns"); public static final String HTTP_CLIENT_CONNECTION_POOL = "PoolConfiguration"; public static final String CONNECTION_MANAGER = "ConnectionManager"; public static final int POOL_CONFIG_INDEX = 1; diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java index e470d0ab71..6d8b96e59e 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java @@ -1352,6 +1352,15 @@ public static void populatePoolingConfig(BMap poolRecord, PoolConfiguration pool maxActiveStreamsPerConnection == -1 ? Integer.MAX_VALUE : validateConfig( maxActiveStreamsPerConnection, HttpConstants.CONNECTION_POOLING_MAX_ACTIVE_STREAMS_PER_CONNECTION.getValue())); + + double minEvictableIdleTime = + ((BDecimal) poolRecord.get(HttpConstants.CONNECTION_POOLING_EVICTABLE_IDLE_TIME)).floatValue(); + poolConfiguration.setMinEvictableIdleTime(minEvictableIdleTime < 0 ? 0 : (long) minEvictableIdleTime * 1000); + + double timeBetweenEvictionRuns = + ((BDecimal) poolRecord.get(HttpConstants.CONNECTION_POOLING_TIME_BETWEEN_EVICTION_RUNS)).floatValue(); + poolConfiguration.setTimeBetweenEvictionRuns( + timeBetweenEvictionRuns < 0 ? 0 : (long) timeBetweenEvictionRuns * 1000); } private static int validateConfig(long value, String configName) {