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.
Implementations§
Source§impl CircuitBreaker
impl CircuitBreaker
Sourcepub fn new(tokens: u64, min_tokens: u64, error_cost: u64) -> Result<Self>
pub fn new(tokens: u64, min_tokens: u64, error_cost: u64) -> Result<Self>
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
fn configure_throttler() -> Result<http_client::ClientConfig> {
let throttler = CircuitBreaker::new(1000, 250, 10)?;
Ok(http_client::ClientConfig::default().set_retry_throttler(throttler))
}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
fn configure_throttler() -> http_client::ClientConfig {
let throttler = CircuitBreaker::clamp(1000, 250, 10);
http_client::ClientConfig::default().set_retry_throttler(throttler)
}Trait Implementations§
Source§impl Clone for CircuitBreaker
impl Clone for CircuitBreaker
Source§fn clone(&self) -> CircuitBreaker
fn clone(&self) -> CircuitBreaker
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CircuitBreaker
impl Debug for CircuitBreaker
Source§impl Default for CircuitBreaker
impl Default for CircuitBreaker
Source§impl RetryThrottler for CircuitBreaker
impl RetryThrottler for CircuitBreaker
Source§fn throttle_retry_attempt(&self) -> bool
fn throttle_retry_attempt(&self) -> bool
true
if the request should be throttled. Read more