ResolvedAddress
- The resolved address type.C
- The type of connection.public final class RoundRobinLoadBalancer<ResolvedAddress,C extends LoadBalancedConnection> extends java.lang.Object implements LoadBalancer<C>
LoadBalancer
that uses a round robin strategy for selecting addresses. It has the following behaviour:
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.Constructor and Description |
---|
RoundRobinLoadBalancer(Publisher<? extends ServiceDiscovererEvent<ResolvedAddress>> eventPublisher,
ConnectionFactory<ResolvedAddress,? extends C> connectionFactory,
java.util.Comparator<ResolvedAddress> comparator)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
Completable |
closeAsync()
Used to close/shutdown a resource.
|
Completable |
closeAsyncGracefully()
Used to close/shutdown a resource, similar to
AsyncCloseable.closeAsync() , but attempts to cleanup state before
abruptly closing. |
Publisher<java.lang.Object> |
eventStream()
A
Publisher of events provided by this LoadBalancer . |
static <ResolvedAddress,C extends LoadBalancedConnection> |
newRoundRobinFactory()
Create a
LoadBalancerFactory that creates instances of RoundRobinLoadBalancer . |
Completable |
onClose()
Returns a
Completable that is notified once the ListenableAsyncCloseable was closed. |
Single<C> |
selectConnection(java.util.function.Predicate<C> selector)
Select the most appropriate connection for a request.
|
public RoundRobinLoadBalancer(Publisher<? extends ServiceDiscovererEvent<ResolvedAddress>> eventPublisher, ConnectionFactory<ResolvedAddress,? extends C> connectionFactory, java.util.Comparator<ResolvedAddress> comparator)
eventPublisher
- provides a stream of addresses to connect to.connectionFactory
- a function which creates new connections.comparator
- used to compare addresses for lookup/iteration during the connection attempt phase.public static <ResolvedAddress,C extends LoadBalancedConnection> LoadBalancerFactory<ResolvedAddress,C> newRoundRobinFactory()
LoadBalancerFactory
that creates instances of RoundRobinLoadBalancer
.ResolvedAddress
- The resolved address type.C
- The type of connection.LoadBalancerFactory
that creates instances of RoundRobinLoadBalancer
.public Single<C> selectConnection(java.util.function.Predicate<C> selector)
LoadBalancer
selectConnection
in interface LoadBalancer<C extends LoadBalancedConnection>
selector
- A Function
that evaluates a connection for selection.
This selector should return null
if the connection MUST not be selected.
This selector is guaranteed to be called for any connection that is returned from this method.Single
that completes with the most appropriate connection to use.public Publisher<java.lang.Object> eventStream()
LoadBalancer
Publisher
of events provided by this LoadBalancer
. This maybe used to broadcast internal state
of this LoadBalancer
to provide hints/visibility for external usage.eventStream
in interface LoadBalancer<C extends LoadBalancedConnection>
Publisher
of events provided by this LoadBalancer
.public Completable onClose()
ListenableAsyncCloseable
Completable
that is notified once the ListenableAsyncCloseable
was closed.onClose
in interface ListenableAsyncCloseable
Completable
that is notified on close.public Completable closeAsync()
AsyncCloseable
closeAsync
in interface AsyncCloseable
CompletableSource
that is notified once the close is complete.public Completable closeAsyncGracefully()
AsyncCloseable
AsyncCloseable.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 to AsyncCloseable.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()
.
closeAsyncGracefully
in interface AsyncCloseable
Completable
that is notified once the close is complete.