pub struct CircuitBreaker { /* private fields */ }Expand description
A CircuitBreaker throttler rejects retry attempts if the success rate is too low.
This struct implements the gRPC throttler algorithm. The throttler works by tracking the number of available “tokens” for a retry attempt. If this number goes below a threshold all retry attempts are throttled.
Retry failures decrement the number of tokens by a given cost. Completed
requests (successfully or not) increase the tokens by 1.
Note: the number of tokens may go below the throttling threshold as multiple concurrent requests may fail and decrease the token count.
Note: throttling only applies to retry attempts, the initial requests is never throttled. This may increases the token count even if all retry attempts are throttled.
§Examples
let tokens = 1000;
let min_tokens = 250;
let error_cost = 10;
let throttler = CircuitBreaker::new(tokens, min_tokens, error_cost)?;Implementations§
Source§impl CircuitBreaker
impl CircuitBreaker
Sourcepub fn new(tokens: u64, min_tokens: u64, error_cost: u64) -> Result<Self, Error>
pub fn new(tokens: u64, min_tokens: u64, error_cost: u64) -> Result<Self, Error>
Creates a new instance.
§Parameters
tokens- the initial number of tokens. This is decreased byerror_coston each retry failure, and increased by1when a request succeeds.min_tokens- stops accepting retry attempts when the number of tokens is at or below this value.error_cost- decrease the token count by this value on failed request attempts.
§Example
let throttler = CircuitBreaker::new(1000, 250, 10)?;Sourcepub fn clamp(tokens: u64, min_tokens: u64, error_cost: u64) -> Self
pub fn clamp(tokens: u64, min_tokens: u64, error_cost: u64) -> Self
Creates a new instance, adjusting min_tokens if needed.
§Parameters
tokens- the initial number of tokens. This is decreased byerror_coston each retry failure, and increased by1when a request succeeds.min_tokens- stops accepting retry attempts when the number of tokens is at or below this value. Clamped to be<= tokens.error_cost- decrease the token count by this value on failed request attempts.
§Example
let throttler = CircuitBreaker::clamp(1000, 250, 10);Trait Implementations§
Source§impl Clone for CircuitBreaker
impl Clone for CircuitBreaker
Source§fn clone(&self) -> CircuitBreaker
fn clone(&self) -> CircuitBreaker
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more