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>
ALoadBalancerthat 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 classRoundRobinLoadBalancer.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 CompletablecloseAsync()Used to close/shutdown a resource.CompletablecloseAsyncGracefully()Used to close/shutdown a resource, similar toAsyncCloseable.closeAsync(), but attempts to cleanup state before abruptly closing.Publisher<java.lang.Object>eventStream()APublisherof events provided by thisLoadBalancer.static <ResolvedAddress,C extends LoadBalancedConnection>
RoundRobinLoadBalancer.RoundRobinLoadBalancerFactory<ResolvedAddress,C>newRoundRobinFactory()Create aLoadBalancerFactorythat creates instances ofRoundRobinLoadBalancer.CompletableonClose()Returns aCompletablethat is notified once theListenableAsyncCloseablewas 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 aLoadBalancerFactorythat creates instances ofRoundRobinLoadBalancer.- Type Parameters:
ResolvedAddress- The resolved address type.C- The type of connection.- Returns:
- a
LoadBalancerFactorythat creates instances ofRoundRobinLoadBalancer.
-
selectConnection
public Single<C> selectConnection(java.util.function.Predicate<C> selector)
Description copied from interface:LoadBalancerSelect the most appropriate connection for a request. Returned connection may be used concurrently for other requests.- Specified by:
selectConnectionin interfaceLoadBalancer<ResolvedAddress>- Parameters:
selector- AFunctionthat evaluates a connection for selection. This selector should returnnullif the connection MUST not be selected. This selector is guaranteed to be called for any connection that is returned from this method.- Returns:
- a
Singlethat completes with the most appropriate connection to use.
-
eventStream
public Publisher<java.lang.Object> eventStream()
Description copied from interface:LoadBalancerAPublisherof events provided by thisLoadBalancer. This maybe used to broadcast internal state of thisLoadBalancerto provide hints/visibility for external usage.- Specified by:
eventStreamin interfaceLoadBalancer<ResolvedAddress>- Returns:
- A
Publisherof events provided by thisLoadBalancer.
-
onClose
public Completable onClose()
Description copied from interface:ListenableAsyncCloseableReturns aCompletablethat is notified once theListenableAsyncCloseablewas closed.- Specified by:
onClosein interfaceListenableAsyncCloseable- Returns:
- the
Completablethat is notified on close.
-
closeAsync
public Completable closeAsync()
Description copied from interface:AsyncCloseableUsed to close/shutdown a resource.- Specified by:
closeAsyncin interfaceAsyncCloseable- Returns:
- A
Completablethat is notified once the close is complete.
-
closeAsyncGracefully
public Completable closeAsyncGracefully()
Description copied from interface:AsyncCloseableUsed 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:
closeAsyncGracefullyin interfaceAsyncCloseable- Returns:
- A
Completablethat is notified once the close is complete.
-
-