From 84eb0f18f3eeb6865854f6679bd4f4e9ded10064 Mon Sep 17 00:00:00 2001 From: dilanSachi Date: Wed, 17 Jan 2024 11:56:50 +0530 Subject: [PATCH 1/7] Expose evictable idle time for a connection --- ballerina/http_client_connection_pool.bal | 2 ++ .../main/java/io/ballerina/stdlib/http/api/HttpConstants.java | 2 ++ .../src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/ballerina/http_client_connection_pool.bal b/ballerina/http_client_connection_pool.bal index c2598bca7f..e66f3e3abb 100644 --- a/ballerina/http_client_connection_pool.bal +++ b/ballerina/http_client_connection_pool.bal @@ -27,11 +27,13 @@ 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 +# + evictableIdleTime - Evictable time for an idle connection in seconds. Default value is 5 minutes public type PoolConfiguration record {| int maxActiveConnections = maxActiveConnections; int maxIdleConnections = maxIdleConnections; decimal waitTime = waitTime; int maxActiveStreamsPerConnection = maxActiveStreamsPerConnection; + int evictableIdleTime = 5 * 60; |}; //This is a hack to get the global map initialized, without involving locking. class ConnectionManager { 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..bfb0d957fd 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,8 @@ 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( + "evictableIdleTime"); 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..cb77ce21b0 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,10 @@ public static void populatePoolingConfig(BMap poolRecord, PoolConfiguration pool maxActiveStreamsPerConnection == -1 ? Integer.MAX_VALUE : validateConfig( maxActiveStreamsPerConnection, HttpConstants.CONNECTION_POOLING_MAX_ACTIVE_STREAMS_PER_CONNECTION.getValue())); + + long minEvictableIdleTime = + poolRecord.getIntValue(HttpConstants.CONNECTION_POOLING_EVICTABLE_IDLE_TIME); + poolConfiguration.setMinEvictableIdleTime(minEvictableIdleTime); } private static int validateConfig(long value, String configName) { From 2eaac3cfeed546f52ba510fdb52f9dccbeecbc9c Mon Sep 17 00:00:00 2001 From: dilanSachi Date: Wed, 17 Jan 2024 12:00:05 +0530 Subject: [PATCH 2/7] Update changelog.md --- changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/changelog.md b/changelog.md index 897f530327..4192136ecb 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 `evictableIdleTime` for a HTTP connection](https://github.com/ballerina-platform/ballerina-library/issues/5951) + ## [2.10.5] - 2023-12-06 ### Fixed From 1b88b37adec5c89f232aa6b229cbfaa07613f4e1 Mon Sep 17 00:00:00 2001 From: dilanSachi Date: Wed, 17 Jan 2024 14:03:49 +0530 Subject: [PATCH 3/7] Address review comments --- ballerina/http_client_connection_pool.bal | 4 ++-- .../java/io/ballerina/stdlib/http/api/HttpConstants.java | 2 +- .../main/java/io/ballerina/stdlib/http/api/HttpUtil.java | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ballerina/http_client_connection_pool.bal b/ballerina/http_client_connection_pool.bal index e66f3e3abb..9ea62a6677 100644 --- a/ballerina/http_client_connection_pool.bal +++ b/ballerina/http_client_connection_pool.bal @@ -27,13 +27,13 @@ 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 -# + evictableIdleTime - Evictable time for an idle connection in seconds. Default value is 5 minutes +# + minEvictableIdleTime - Minimum evictable time for an idle connection in seconds. Default value is 5 minutes public type PoolConfiguration record {| int maxActiveConnections = maxActiveConnections; int maxIdleConnections = maxIdleConnections; decimal waitTime = waitTime; int maxActiveStreamsPerConnection = maxActiveStreamsPerConnection; - int evictableIdleTime = 5 * 60; + decimal minEvictableIdleTime = 5 * 60; |}; //This is a hack to get the global map initialized, without involving locking. class ConnectionManager { 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 bfb0d957fd..85af969dd3 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 @@ -507,7 +507,7 @@ public final class HttpConstants { 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( - "evictableIdleTime"); + "minEvictableIdleTime"); 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 cb77ce21b0..427a0ec012 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 @@ -1353,9 +1353,9 @@ public static void populatePoolingConfig(BMap poolRecord, PoolConfiguration pool maxActiveStreamsPerConnection, HttpConstants.CONNECTION_POOLING_MAX_ACTIVE_STREAMS_PER_CONNECTION.getValue())); - long minEvictableIdleTime = - poolRecord.getIntValue(HttpConstants.CONNECTION_POOLING_EVICTABLE_IDLE_TIME); - poolConfiguration.setMinEvictableIdleTime(minEvictableIdleTime); + float minEvictableIdleTime = + poolRecord.getFloatValue(HttpConstants.CONNECTION_POOLING_EVICTABLE_IDLE_TIME).floatValue(); + poolConfiguration.setMinEvictableIdleTime(minEvictableIdleTime < 0 ? 0 : (long) minEvictableIdleTime * 1000); } private static int validateConfig(long value, String configName) { From 0355a19db76685ccfe328d4c78bf194f7993264c Mon Sep 17 00:00:00 2001 From: dilanSachi Date: Wed, 17 Jan 2024 14:08:11 +0530 Subject: [PATCH 4/7] Update changelog.md --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 4192136ecb..5d822f2324 100644 --- a/changelog.md +++ b/changelog.md @@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased ### Added -- [Expose `evictableIdleTime` for a HTTP connection](https://github.com/ballerina-platform/ballerina-library/issues/5951) +- [Expose `minEvictableIdleTime` for a HTTP connection](https://github.com/ballerina-platform/ballerina-library/issues/5951) ## [2.10.5] - 2023-12-06 From 8afe852dc38c125061b1acc8215eb24cf8f17dd6 Mon Sep 17 00:00:00 2001 From: dilanSachi Date: Wed, 17 Jan 2024 14:26:19 +0530 Subject: [PATCH 5/7] Update pool record --- ballerina/http_client_connection_pool.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ballerina/http_client_connection_pool.bal b/ballerina/http_client_connection_pool.bal index 9ea62a6677..c0fffa44ec 100644 --- a/ballerina/http_client_connection_pool.bal +++ b/ballerina/http_client_connection_pool.bal @@ -33,7 +33,7 @@ public type PoolConfiguration record {| int maxIdleConnections = maxIdleConnections; decimal waitTime = waitTime; int maxActiveStreamsPerConnection = maxActiveStreamsPerConnection; - decimal minEvictableIdleTime = 5 * 60; + decimal minEvictableIdleTime = 300; |}; //This is a hack to get the global map initialized, without involving locking. class ConnectionManager { From f982a15851d4f07e1af058a1391050a37c697c9d Mon Sep 17 00:00:00 2001 From: dilanSachi Date: Wed, 17 Jan 2024 16:02:57 +0530 Subject: [PATCH 6/7] Expose time between eviction runs --- ballerina/http_client_connection_pool.bal | 2 ++ .../java/io/ballerina/stdlib/http/api/HttpConstants.java | 2 ++ .../main/java/io/ballerina/stdlib/http/api/HttpUtil.java | 9 +++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ballerina/http_client_connection_pool.bal b/ballerina/http_client_connection_pool.bal index c0fffa44ec..06e3636fa4 100644 --- a/ballerina/http_client_connection_pool.bal +++ b/ballerina/http_client_connection_pool.bal @@ -28,12 +28,14 @@ configurable int maxActiveStreamsPerConnection = 100; # + 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/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java index 85af969dd3..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 @@ -508,6 +508,8 @@ public final class HttpConstants { "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 427a0ec012..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 @@ -1353,9 +1353,14 @@ public static void populatePoolingConfig(BMap poolRecord, PoolConfiguration pool maxActiveStreamsPerConnection, HttpConstants.CONNECTION_POOLING_MAX_ACTIVE_STREAMS_PER_CONNECTION.getValue())); - float minEvictableIdleTime = - poolRecord.getFloatValue(HttpConstants.CONNECTION_POOLING_EVICTABLE_IDLE_TIME).floatValue(); + 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) { From 269c0e8f0c922b70659dee3f080873a20ae9cc73 Mon Sep 17 00:00:00 2001 From: dilanSachi Date: Wed, 17 Jan 2024 16:03:41 +0530 Subject: [PATCH 7/7] Update changelog.md --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 5d822f2324..5254d809db 100644 --- a/changelog.md +++ b/changelog.md @@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased ### Added -- [Expose `minEvictableIdleTime` for a HTTP connection](https://github.com/ballerina-platform/ballerina-library/issues/5951) +- [Expose HTTP connection eviction configurations in the client level](https://github.com/ballerina-platform/ballerina-library/issues/5951) ## [2.10.5] - 2023-12-06