Skip to content

Commit

Permalink
Updated properties for netty connection pooling
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol authored and senivam committed Apr 16, 2021
1 parent 06b1a6c commit fecc6f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -29,11 +29,19 @@ public class NettyClientProperties {
/**
* <p>
* This property determines the maximum number of idle connections that will be simultaneously kept alive
* in total, rather than per destination. The default is 60.
* in total, rather than per destination. The default is 60. Specify 0 to disable.
* </p>
*/
public static final String MAX_CONNECTIONS_TOTAL = "jersey.config.client.maxTotalConnections";

/**
* <p>
* This property determines the number of seconds the idle connections are kept in the pool before pruned.
* The default is 60. Specify 0 to disable.
* </p>
*/
public static final String IDLE_CONNECTION_PRUNE_TIMEOUT = "jersey.config.client.idleConnectionPruneTimeout";

/**
* <p>
* This property determines the maximum number of idle connections that will be simultaneously kept alive, per destination.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -94,9 +94,12 @@ class NettyConnector implements Connector {
// http.maxConnections (default: 5)
private static final int DEFAULT_MAX_POOL_SIZE = 5;
private static final int MAX_POOL_SIZE = Integer.getInteger("http.maxConnections", DEFAULT_MAX_POOL_SIZE);
private static final int MAX_POOL_IDLE = 60;
private static final int DEFAULT_MAX_POOL_IDLE = 60; // seconds
private static final int DEFAULT_MAX_POOL_SIZE_TOTAL = 60; // connections


private final Integer maxPoolSize; // either from system property, or from Jersey config, or default
private final Integer maxPoolSizeTotal; //either from Jersey config, or default
private final Integer maxPoolIdle; // either from Jersey config, or default

private static final String INACTIVE_POOLED_CONNECTION_HANDLER = "inactive_pooled_connection_handler";
Expand All @@ -119,20 +122,22 @@ class NettyConnector implements Connector {

this.client = client;

final Object maxPoolIdleProperty = properties.get(NettyClientProperties.MAX_CONNECTIONS_TOTAL);
final Object maxPoolSizeTotalProperty = properties.get(NettyClientProperties.MAX_CONNECTIONS_TOTAL);
final Object maxPoolIdleProperty = properties.get(NettyClientProperties.IDLE_CONNECTION_PRUNE_TIMEOUT);
final Object maxPoolSizeProperty = properties.get(NettyClientProperties.MAX_CONNECTIONS);

maxPoolIdle = maxPoolIdleProperty != null ? (Integer) maxPoolIdleProperty : MAX_POOL_IDLE;
maxPoolSizeTotal = maxPoolSizeTotalProperty != null ? (Integer) maxPoolSizeTotalProperty : DEFAULT_MAX_POOL_SIZE_TOTAL;
maxPoolIdle = maxPoolIdleProperty != null ? (Integer) maxPoolIdleProperty : DEFAULT_MAX_POOL_IDLE;
maxPoolSize = maxPoolSizeProperty != null
? (Integer) maxPoolSizeProperty
: (HTTP_KEEPALIVE ? MAX_POOL_SIZE : DEFAULT_MAX_POOL_SIZE);

if (maxPoolIdle == null || maxPoolIdle < 0) {
throw new ProcessingException(LocalizationMessages.WRONG_MAX_POOL_IDLE(maxPoolIdle));
if (maxPoolSizeTotal < 0) {
throw new ProcessingException(LocalizationMessages.WRONG_MAX_POOL_TOTAL(maxPoolSizeTotal));
}

if (maxPoolSize == null || maxPoolSize < 0) {
throw new ProcessingException(LocalizationMessages.WRONG_MAX_POOL_SIZE(maxPoolIdle));
if (maxPoolSize < 0) {
throw new ProcessingException(LocalizationMessages.WRONG_MAX_POOL_SIZE(maxPoolSize));
}
}

Expand Down Expand Up @@ -270,7 +275,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
connections.put(key, conns1);
} else {
synchronized (conns1) {
if (conns1.size() < maxPoolSize) {
if ((maxPoolSizeTotal == 0 || connections.size() < maxPoolSizeTotal) && conns1.size() < maxPoolSize) {
conns1.add(ch);
} else { // else do not add the Channel to the idle pool
added = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,5 +17,6 @@
wrong.proxy.uri.type=The proxy URI ("{0}") property MUST be an instance of String or URI.
wrong.read.timeout=Unexpected ("{0}") READ_TIMEOUT.
wrong.max.pool.size=Unexpected ("{0}") maximum number of connections per destination.
wrong.max.pool.idle=Unexpected ("{0}") maximum number of connections total.
wrong.max.pool.total=Unexpected ("{0}") maximum number of connections total.
wrong.max.pool.idle=Unexpected ("{0}") maximum number of idle seconds.

0 comments on commit fecc6f0

Please sign in to comment.