Package io.servicetalk.loadbalancer
Class RoundRobinLoadBalancer<ResolvedAddress,C extends LoadBalancedConnection>
- java.lang.Object
-
- io.servicetalk.loadbalancer.RoundRobinLoadBalancer<ResolvedAddress,C>
-
- Type Parameters:
ResolvedAddress
- The resolved address type.C
- The type of connection.
- All Implemented Interfaces:
LoadBalancer<C>
,AsyncCloseable
,ListenableAsyncCloseable
public final class RoundRobinLoadBalancer<ResolvedAddress,C extends LoadBalancedConnection> extends java.lang.Object implements LoadBalancer<C>
ALoadBalancer
that uses a round robin strategy for selecting addresses. It has the following behaviour:- Round robining is done at address level.
- Connections are created lazily, without any concurrency control on their creation. This can lead to over-provisioning connections when dealing with a requests surge.
- Existing connections are reused unless a selector passed to
selectConnection(Predicate)
suggests otherwise. This can lead to situations where connections will be used to their maximum capacity (for example in the context of pipelining) before new connections are created. - Closed connections are automatically pruned.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RoundRobinLoadBalancer.RoundRobinLoadBalancerFactory<ResolvedAddress,C extends LoadBalancedConnection>
-
Constructor Summary
Constructors Constructor Description RoundRobinLoadBalancer(Publisher<? extends ServiceDiscovererEvent<ResolvedAddress>> eventPublisher, ConnectionFactory<ResolvedAddress,? extends C> connectionFactory)
Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Completable
closeAsync()
Used to close/shutdown a resource.Completable
closeAsyncGracefully()
Used to close/shutdown a resource, similar toAsyncCloseable.closeAsync()
, but attempts to cleanup state before abruptly closing.Publisher<java.lang.Object>
eventStream()
APublisher
of events provided by thisLoadBalancer
.static <ResolvedAddress,C extends LoadBalancedConnection>
RoundRobinLoadBalancer.RoundRobinLoadBalancerFactory<ResolvedAddress,C>newRoundRobinFactory()
Create aLoadBalancerFactory
that creates instances ofRoundRobinLoadBalancer
.Completable
onClose()
Returns aCompletable
that is notified once theListenableAsyncCloseable
was closed.Single<C>
selectConnection(java.util.function.Predicate<C> selector)
Select the most appropriate connection for a request.
-
-
-
Constructor Detail
-
RoundRobinLoadBalancer
public RoundRobinLoadBalancer(Publisher<? extends ServiceDiscovererEvent<ResolvedAddress>> eventPublisher, ConnectionFactory<ResolvedAddress,? extends C> connectionFactory)
Creates a new instance.- Parameters:
eventPublisher
- provides a stream of addresses to connect to.connectionFactory
- a function which creates new connections.
-
-
Method Detail
-
newRoundRobinFactory
public static <ResolvedAddress,C extends LoadBalancedConnection> RoundRobinLoadBalancer.RoundRobinLoadBalancerFactory<ResolvedAddress,C> newRoundRobinFactory()
Create aLoadBalancerFactory
that creates instances ofRoundRobinLoadBalancer
.- Type Parameters:
ResolvedAddress
- The resolved address type.C
- The type of connection.- Returns:
- a
LoadBalancerFactory
that creates instances ofRoundRobinLoadBalancer
.
-
selectConnection
public Single<C> selectConnection(java.util.function.Predicate<C> selector)
Description copied from interface:LoadBalancer
Select the most appropriate connection for a request. Returned connection may be used concurrently for other requests.- Specified by:
selectConnection
in interfaceLoadBalancer<ResolvedAddress>
- Parameters:
selector
- AFunction
that evaluates a connection for selection. This selector should returnnull
if the connection MUST not be selected. This selector is guaranteed to be called for any connection that is returned from this method.- Returns:
- a
Single
that completes with the most appropriate connection to use.
-
eventStream
public Publisher<java.lang.Object> eventStream()
Description copied from interface:LoadBalancer
APublisher
of events provided by thisLoadBalancer
. This maybe used to broadcast internal state of thisLoadBalancer
to provide hints/visibility for external usage.- Specified by:
eventStream
in interfaceLoadBalancer<ResolvedAddress>
- Returns:
- A
Publisher
of events provided by thisLoadBalancer
.
-
onClose
public Completable onClose()
Description copied from interface:ListenableAsyncCloseable
Returns aCompletable
that is notified once theListenableAsyncCloseable
was closed.- Specified by:
onClose
in interfaceListenableAsyncCloseable
- Returns:
- the
Completable
that is notified on close.
-
closeAsync
public Completable closeAsync()
Description copied from interface:AsyncCloseable
Used to close/shutdown a resource.- Specified by:
closeAsync
in interfaceAsyncCloseable
- Returns:
- A
Completable
that is notified once the close is complete.
-
closeAsyncGracefully
public Completable closeAsyncGracefully()
Description copied from interface:AsyncCloseable
Used to close/shutdown a resource, similar toAsyncCloseable.closeAsync()
, but attempts to cleanup state before abruptly closing. This provides a hint that implementations can use to stop accepting new work and finish in flight work. This method is implemented on a "best effort" basis and may be equivalent toAsyncCloseable.closeAsync()
.Note: Implementations may or may not apply a timeout for this operation to complete, if a caller does not want to wait indefinitely, and are unsure if the implementation applies a timeout, it is advisable to apply a timeout and force a call to
AsyncCloseable.closeAsync()
.- Specified by:
closeAsyncGracefully
in interfaceAsyncCloseable
- Returns:
- A
Completable
that is notified once the close is complete.
-
-