[−][src]Struct tower_balance::p2c::Balance
Distributes requests across inner services using the Power of Two Choices.
As described in the Finagle Guide:
The algorithm randomly picks two services from the set of ready endpoints and selects the least loaded of the two. By repeatedly using this strategy, we can expect a manageable upper bound on the maximum load of any server.
The maximum load variance between any two servers is bound by
ln(ln(n))
wheren
is the number of servers in the cluster.
Note that Balance
requires that the Discover
you use is Unpin
in order to implement
Service
. This is because it needs to be accessed from Service::poll_ready
, which takes
&mut self
. You can achieve this easily by wrapping your Discover
in Box::pin
before you
construct the Balance
instance. For more details, see #319.
Methods
impl<D, Req> Balance<D, Req> where
D: Discover,
D::Service: Service<Req>,
<D::Service as Service<Req>>::Error: Into<Box<dyn Error + Send + Sync>>,
[src]
D: Discover,
D::Service: Service<Req>,
<D::Service as Service<Req>>::Error: Into<Box<dyn Error + Send + Sync>>,
pub fn new(discover: D, rng: SmallRng) -> Self
[src]
Initializes a P2C load balancer from the provided randomization source.
pub fn from_entropy(discover: D) -> Self
[src]
Initializes a P2C load balancer from the OS's entropy source.
pub fn len(&self) -> usize
[src]
Returns the number of endpoints currently tracked by the balancer.
Trait Implementations
impl<D: Discover, Req> Debug for Balance<D, Req> where
D: Debug,
D::Key: Debug,
D::Service: Debug,
Req: Debug,
[src]
D: Debug,
D::Key: Debug,
D::Service: Debug,
Req: Debug,
impl<D, Req> Service<Req> for Balance<D, Req> where
D: Discover + Unpin,
D::Key: Clone,
D::Error: Into<Box<dyn Error + Send + Sync>>,
D::Service: Service<Req> + Load,
<D::Service as Load>::Metric: Debug,
<D::Service as Service<Req>>::Error: Into<Box<dyn Error + Send + Sync>>,
[src]
D: Discover + Unpin,
D::Key: Clone,
D::Error: Into<Box<dyn Error + Send + Sync>>,
D::Service: Service<Req> + Load,
<D::Service as Load>::Metric: Debug,
<D::Service as Service<Req>>::Error: Into<Box<dyn Error + Send + Sync>>,
type Response = <D::Service as Service<Req>>::Response
Responses given by the service.
type Error = Box<dyn Error + Send + Sync>
Errors produced by the service.
type Future = MapErr<<D::Service as Service<Req>>::Future, fn(_: <D::Service as Service<Req>>::Error) -> Box<dyn Error + Send + Sync>>
The future response value.
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>>
[src]
fn call(&mut self, request: Req) -> Self::Future
[src]
Auto Trait Implementations
impl<D, Req> !RefUnwindSafe for Balance<D, Req>
impl<D, Req> Send for Balance<D, Req> where
D: Send,
Req: Send,
<D as Discover>::Key: Send,
<D as Discover>::Service: Send,
D: Send,
Req: Send,
<D as Discover>::Key: Send,
<D as Discover>::Service: Send,
impl<D, Req> Sync for Balance<D, Req> where
D: Sync,
Req: Sync,
<D as Discover>::Key: Sync,
<D as Discover>::Service: Sync,
D: Sync,
Req: Sync,
<D as Discover>::Key: Sync,
<D as Discover>::Service: Sync,
impl<D, Req> Unpin for Balance<D, Req> where
D: Unpin,
Req: Unpin,
<D as Discover>::Key: Eq + Hash,
D: Unpin,
Req: Unpin,
<D as Discover>::Key: Eq + Hash,
impl<D, Req> !UnwindSafe for Balance<D, Req>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<C, Target> MakeConnection<Target> for C where
C: Service<Target>,
<C as Service<Target>>::Response: AsyncRead,
<C as Service<Target>>::Response: AsyncWrite,
[src]
C: Service<Target>,
<C as Service<Target>>::Response: AsyncRead,
<C as Service<Target>>::Response: AsyncWrite,
type Connection = <C as Service<Target>>::Response
The transport provided by this service
type Error = <C as Service<Target>>::Error
Errors produced by the connecting service
type Future = <C as Service<Target>>::Future
The future that eventually produces the transport
fn poll_ready(
&mut self,
cx: &mut Context
) -> Poll<Result<(), <C as MakeConnection<Target>>::Error>>
[src]
&mut self,
cx: &mut Context
) -> Poll<Result<(), <C as MakeConnection<Target>>::Error>>
fn make_connection(
&mut self,
target: Target
) -> <C as MakeConnection<Target>>::Future
[src]
&mut self,
target: Target
) -> <C as MakeConnection<Target>>::Future
impl<M, S, Target, Request> MakeService<Target, Request> for M where
M: Service<Target, Response = S>,
S: Service<Request>,
[src]
M: Service<Target, Response = S>,
S: Service<Request>,
type Response = <S as Service<Request>>::Response
Responses given by the service
type Error = <S as Service<Request>>::Error
Errors produced by the service
type Service = S
The Service
value created by this factory
type MakeError = <M as Service<Target>>::Error
Errors produced while building a service.
type Future = <M as Service<Target>>::Future
The future of the Service
instance.
fn poll_ready(
&mut self,
cx: &mut Context
) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
[src]
&mut self,
cx: &mut Context
) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
fn make_service(
&mut self,
target: Target
) -> <M as MakeService<Target, Request>>::Future
[src]
&mut self,
target: Target
) -> <M as MakeService<Target, Request>>::Future
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
[src]
V: MultiLane<T>,