Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #5217 - Review RoundRobinConnectionPool
Introduced IndexedConnectionPool and RandomConnectionPool. Clarified semantic of RoundRobinConnectionPool. Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
- Loading branch information
Showing
6 changed files
with
184 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
jetty-client/src/main/java/org/eclipse/jetty/client/IndexedConnectionPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. | ||
// ------------------------------------------------------------------------ | ||
// All rights reserved. This program and the accompanying materials | ||
// are made available under the terms of the Eclipse Public License v1.0 | ||
// and Apache License v2.0 which accompanies this distribution. | ||
// | ||
// The Eclipse Public License is available at | ||
// http://www.eclipse.org/legal/epl-v10.html | ||
// | ||
// The Apache License v2.0 is available at | ||
// http://www.opensource.org/licenses/apache2.0.php | ||
// | ||
// You may elect to redistribute this code under either of these licenses. | ||
// ======================================================================== | ||
// | ||
|
||
package org.eclipse.jetty.client; | ||
|
||
import org.eclipse.jetty.client.api.Connection; | ||
import org.eclipse.jetty.util.Callback; | ||
import org.eclipse.jetty.util.Pool; | ||
import org.eclipse.jetty.util.annotation.ManagedObject; | ||
import org.eclipse.jetty.util.log.Log; | ||
import org.eclipse.jetty.util.log.Logger; | ||
|
||
/** | ||
* <p>A {@link MultiplexConnectionPool} that picks connections at a particular | ||
* index between {@code 0} and {@link #getMaxConnectionCount()}.</p> | ||
* <p>The algorithm that decides the index value is decided by subclasses.</p> | ||
* <p>To acquire a connection, this class obtains the index value and attempts | ||
* to activate the pool entry at that index. | ||
* If this activation fails, another attempt to activate an alternative pool | ||
* entry is performed, to avoid stalling connection acquisition if there is | ||
* an available entry at a different index.</p> | ||
*/ | ||
@ManagedObject | ||
public abstract class IndexedConnectionPool extends MultiplexConnectionPool | ||
{ | ||
private static final Logger LOG = Log.getLogger(IndexedConnectionPool.class); | ||
|
||
private final Pool<Connection> pool; | ||
|
||
public IndexedConnectionPool(HttpDestination destination, int maxConnections, boolean cache, Callback requester, int maxMultiplex) | ||
{ | ||
super(destination, maxConnections, cache, requester, maxMultiplex); | ||
pool = destination.getBean(Pool.class); | ||
} | ||
|
||
/** | ||
* <p>Must return an index between 0 (inclusive) and {@code maxConnections} (exclusive) | ||
* used to attempt to acquire the connection at that index in the pool.</p> | ||
* | ||
* @param maxConnections the upper bound of the index (exclusive) | ||
* @return an index between 0 (inclusive) and {@code maxConnections} (exclusive) | ||
*/ | ||
protected abstract int getIndex(int maxConnections); | ||
|
||
@Override | ||
protected Connection activate() | ||
{ | ||
int index = getIndex(getMaxConnectionCount()); | ||
Pool<Connection>.Entry entry = pool.acquireAt(index); | ||
if (LOG.isDebugEnabled()) | ||
LOG.debug("activating at index={} entry={}", index, entry); | ||
if (entry == null) | ||
{ | ||
entry = pool.acquire(); | ||
if (LOG.isDebugEnabled()) | ||
LOG.debug("activating alternative entry={}", entry); | ||
} | ||
if (entry == null) | ||
return null; | ||
Connection connection = entry.getPooled(); | ||
acquired(connection); | ||
return connection; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
jetty-client/src/main/java/org/eclipse/jetty/client/RandomConnectionPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. | ||
// ------------------------------------------------------------------------ | ||
// All rights reserved. This program and the accompanying materials | ||
// are made available under the terms of the Eclipse Public License v1.0 | ||
// and Apache License v2.0 which accompanies this distribution. | ||
// | ||
// The Eclipse Public License is available at | ||
// http://www.eclipse.org/legal/epl-v10.html | ||
// | ||
// The Apache License v2.0 is available at | ||
// http://www.opensource.org/licenses/apache2.0.php | ||
// | ||
// You may elect to redistribute this code under either of these licenses. | ||
// ======================================================================== | ||
// | ||
|
||
package org.eclipse.jetty.client; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import org.eclipse.jetty.util.Callback; | ||
import org.eclipse.jetty.util.annotation.ManagedObject; | ||
|
||
/** | ||
* <p>An indexed {@link ConnectionPool} that provides connections | ||
* randomly among the ones that are available.</p> | ||
*/ | ||
@ManagedObject | ||
public class RandomConnectionPool extends IndexedConnectionPool | ||
{ | ||
public RandomConnectionPool(HttpDestination destination, int maxConnections, boolean cache, Callback requester, int maxMultiplex) | ||
{ | ||
super(destination, maxConnections, cache, requester, maxMultiplex); | ||
} | ||
|
||
@Override | ||
protected int getIndex(int maxConnections) | ||
{ | ||
return ThreadLocalRandom.current().nextInt(maxConnections); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters