Skip to main content

Balance

Struct Balance 

Source
pub struct Balance<D: Discover, Req> { /* private fields */ }
Expand description

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)) where n 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.

Implementations§

Source§

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>>,

Source

pub fn new(discover: D, rng: SmallRng) -> Self

Initializes a P2C load balancer from the provided randomization source.

Source

pub fn from_entropy(discover: D) -> Self

Initializes a P2C load balancer from the OS’s entropy source.

Source

pub fn len(&self) -> usize

Returns the number of endpoints currently tracked by the balancer.

Trait Implementations§

Source§

impl<D, Req> Debug for Balance<D, Req>
where D: Debug + Discover, D::Key: Debug, D::Service: Debug, Req: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>>,

Source§

type Response = <<D as Discover>::Service as Service<Req>>::Response

Responses given by the service.
Source§

type Error = Box<dyn Error + Sync + Send>

Errors produced by the service.
Source§

type Future = MapErr<<<D as Discover>::Service as Service<Req>>::Future, fn(<<D as Discover>::Service as Service<Req>>::Error) -> Box<dyn Error + Sync + Send>>

The future response value.
Source§

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>

Returns Poll::Ready(Ok(())) when the service is able to process requests. Read more
Source§

fn call(&mut self, request: Req) -> Self::Future

Process the request and return the response asynchronously. Read more

Auto Trait Implementations§

§

impl<D, Req> !Freeze for Balance<D, Req>

§

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,

§

impl<D, Req> Sync for Balance<D, Req>
where 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,

§

impl<D, Req> UnsafeUnpin for Balance<D, Req>
where D: UnsafeUnpin,

§

impl<D, Req> !UnwindSafe for Balance<D, Req>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<M, S, Target, Request> MakeService<Target, Request> for M
where M: Service<Target, Response = S>, S: Service<Request>,

Source§

type Response = <S as Service<Request>>::Response

Responses given by the service
Source§

type Error = <S as Service<Request>>::Error

Errors produced by the service
Source§

type Service = S

The Service value created by this factory
Source§

type MakeError = <M as Service<Target>>::Error

Errors produced while building a service.
Source§

type Future = <M as Service<Target>>::Future

The future of the Service instance.
Source§

fn poll_ready( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>

Returns Ready when the factory is able to create more services. Read more
Source§

fn make_service( &mut self, target: Target, ) -> <M as MakeService<Target, Request>>::Future

Create and return a new service value asynchronously.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V