Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated properties for netty connection pooling #4769

Merged
merged 1 commit into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
jansupol marked this conversation as resolved.
Show resolved Hide resolved
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.