Struct GovernorConfigBuilder
pub struct GovernorConfigBuilder<K, M>where
K: KeyExtractor,
M: RateLimitingMiddleware<QuantaInstant>,{ /* private fields */ }Expand description
Helper struct for building a configuration for the governor middleware.
§Example
Create a configuration with a quota of ten requests per IP address that replenishes one element every minute.
use tower_governor::governor::GovernorConfigBuilder;
let config = GovernorConfigBuilder::default()
.per_second(60)
.burst_size(10)
.finish()
.unwrap();with x-ratelimit headers
use tower_governor::governor::GovernorConfigBuilder;
let config = GovernorConfigBuilder::default()
.per_second(60)
.burst_size(10)
.use_headers() // Add this
.finish()
.unwrap();Implementations§
§impl<M> GovernorConfigBuilder<PeerIpKeyExtractor, M>where
M: RateLimitingMiddleware<QuantaInstant>,
Sets the default Governor Config and defines all the different configuration functions
This one is used when the default PeerIpKeyExtractor is used
impl<M> GovernorConfigBuilder<PeerIpKeyExtractor, M>where
M: RateLimitingMiddleware<QuantaInstant>,
Sets the default Governor Config and defines all the different configuration functions This one is used when the default PeerIpKeyExtractor is used
pub fn const_default() -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
pub fn const_period(
self,
duration: Duration,
) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
pub fn const_period( self, duration: Duration, ) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
Set the interval after which one element of the quota is replenished.
The interval must not be zero.
pub fn const_per_second(
self,
seconds: u64,
) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
pub fn const_per_second( self, seconds: u64, ) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
Set the interval after which one element of the quota is replenished in seconds.
The interval must not be zero.
pub fn const_per_millisecond(
self,
milliseconds: u64,
) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
pub fn const_per_millisecond( self, milliseconds: u64, ) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
Set the interval after which one element of the quota is replenished in milliseconds.
The interval must not be zero.
pub fn const_per_nanosecond(
self,
nanoseconds: u64,
) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
pub fn const_per_nanosecond( self, nanoseconds: u64, ) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
Set the interval after which one element of the quota is replenished in nanoseconds.
The interval must not be zero.
pub fn const_burst_size(
self,
burst_size: u32,
) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
pub fn const_burst_size( self, burst_size: u32, ) -> GovernorConfigBuilder<PeerIpKeyExtractor, M>
Set quota size that defines how many requests can occur before the governor middleware starts blocking requests from an IP address and clients have to wait until the elements of the quota are replenished.
The burst_size must not be zero.
§impl<K, M> GovernorConfigBuilder<K, M>where
K: KeyExtractor,
M: RateLimitingMiddleware<QuantaInstant>,
Sets configuration options when any Key Extractor is provided
impl<K, M> GovernorConfigBuilder<K, M>where
K: KeyExtractor,
M: RateLimitingMiddleware<QuantaInstant>,
Sets configuration options when any Key Extractor is provided
pub fn period(&mut self, duration: Duration) -> &mut GovernorConfigBuilder<K, M>
pub fn period(&mut self, duration: Duration) -> &mut GovernorConfigBuilder<K, M>
Set the interval after which one element of the quota is replenished.
The interval must not be zero.
pub fn per_second(&mut self, seconds: u64) -> &mut GovernorConfigBuilder<K, M>
pub fn per_second(&mut self, seconds: u64) -> &mut GovernorConfigBuilder<K, M>
Set the interval after which one element of the quota is replenished in seconds.
The interval must not be zero.
pub fn per_millisecond(
&mut self,
milliseconds: u64,
) -> &mut GovernorConfigBuilder<K, M>
pub fn per_millisecond( &mut self, milliseconds: u64, ) -> &mut GovernorConfigBuilder<K, M>
Set the interval after which one element of the quota is replenished in milliseconds.
The interval must not be zero.
pub fn per_nanosecond(
&mut self,
nanoseconds: u64,
) -> &mut GovernorConfigBuilder<K, M>
pub fn per_nanosecond( &mut self, nanoseconds: u64, ) -> &mut GovernorConfigBuilder<K, M>
Set the interval after which one element of the quota is replenished in nanoseconds.
The interval must not be zero.
pub fn burst_size(
&mut self,
burst_size: u32,
) -> &mut GovernorConfigBuilder<K, M>
pub fn burst_size( &mut self, burst_size: u32, ) -> &mut GovernorConfigBuilder<K, M>
Set quota size that defines how many requests can occur before the governor middleware starts blocking requests from an IP address and clients have to wait until the elements of the quota are replenished.
The burst_size must not be zero.
pub fn methods(
&mut self,
methods: Vec<Method>,
) -> &mut GovernorConfigBuilder<K, M>
pub fn methods( &mut self, methods: Vec<Method>, ) -> &mut GovernorConfigBuilder<K, M>
Set the HTTP methods this configuration should apply to. By default this is all methods.
pub fn key_extractor<K2>(
&mut self,
key_extractor: K2,
) -> GovernorConfigBuilder<K2, M>where
K2: KeyExtractor,
pub fn key_extractor<K2>(
&mut self,
key_extractor: K2,
) -> GovernorConfigBuilder<K2, M>where
K2: KeyExtractor,
Set the key extractor this configuration should use. By default this is using the [PeerIpKeyExtractor].
pub fn use_headers(
&mut self,
) -> GovernorConfigBuilder<K, StateInformationMiddleware>
pub fn use_headers( &mut self, ) -> GovernorConfigBuilder<K, StateInformationMiddleware>
Set ratelimit headers to response, the headers is
x-ratelimit-limit- Request limitx-ratelimit-remaining- The number of requests left for the time windowx-ratelimit-after- Number of seconds in which the API will become available after its rate limit has been exceededretry-after- Same value asx-ratelimit-afterx-ratelimit-whitelisted- If the request method not in methods, this header will be add it, usemethodsto add methods
By default x-ratelimit-after and retry-after are enabled, with use_headers will enable x-ratelimit-limit, x-ratelimit-whitelisted and x-ratelimit-remaining
Trait Implementations§
§impl<K, M> Clone for GovernorConfigBuilder<K, M>
impl<K, M> Clone for GovernorConfigBuilder<K, M>
§fn clone(&self) -> GovernorConfigBuilder<K, M>
fn clone(&self) -> GovernorConfigBuilder<K, M>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl<K, M> Debug for GovernorConfigBuilder<K, M>
impl<K, M> Debug for GovernorConfigBuilder<K, M>
§impl Default for GovernorConfigBuilder<PeerIpKeyExtractor, NoOpMiddleware>
impl Default for GovernorConfigBuilder<PeerIpKeyExtractor, NoOpMiddleware>
§fn default() -> GovernorConfigBuilder<PeerIpKeyExtractor, NoOpMiddleware>
fn default() -> GovernorConfigBuilder<PeerIpKeyExtractor, NoOpMiddleware>
The default configuration which is suitable for most services. Allows burst with up to eight requests and replenishes one element after 500ms, based on peer IP. The values can be modified by calling other methods on this struct.
§impl<K, M> PartialEq for GovernorConfigBuilder<K, M>
impl<K, M> PartialEq for GovernorConfigBuilder<K, M>
impl<K, M> Eq for GovernorConfigBuilder<K, M>
impl<K, M> StructuralPartialEq for GovernorConfigBuilder<K, M>where
K: KeyExtractor,
M: RateLimitingMiddleware<QuantaInstant>,
Auto Trait Implementations§
impl<K, M> Freeze for GovernorConfigBuilder<K, M>where
K: Freeze,
impl<K, M> RefUnwindSafe for GovernorConfigBuilder<K, M>where
K: RefUnwindSafe,
M: RefUnwindSafe,
impl<K, M> Send for GovernorConfigBuilder<K, M>
impl<K, M> Sync for GovernorConfigBuilder<K, M>
impl<K, M> Unpin for GovernorConfigBuilder<K, M>
impl<K, M> UnwindSafe for GovernorConfigBuilder<K, M>where
K: UnwindSafe,
M: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].