Struct tower_governor::governor::GovernorConfigBuilder
source · pub struct GovernorConfigBuilder<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§
source§impl<K: KeyExtractor, M: RateLimitingMiddleware<QuantaInstant>> GovernorConfigBuilder<K, M>
impl<K: KeyExtractor, M: RateLimitingMiddleware<QuantaInstant>> GovernorConfigBuilder<K, M>
sourcepub fn error_handler<F>(&mut self, func: F) -> &mut Self
pub fn error_handler<F>(&mut self, func: F) -> &mut Self
Set handler function for handling GovernorError
§Example
GovernorConfigBuilder::default()
.error_handler(|mut error| {
// match against GovernorError and produce customized Response type.
match error {
_ => Response::new("some error".into())
}
});
source§impl<M: RateLimitingMiddleware<QuantaInstant>> GovernorConfigBuilder<PeerIpKeyExtractor, M>
impl<M: RateLimitingMiddleware<QuantaInstant>> GovernorConfigBuilder<PeerIpKeyExtractor, M>
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() -> Self
sourcepub fn const_period(self, duration: Duration) -> Self
pub fn const_period(self, duration: Duration) -> Self
Set the interval after which one element of the quota is replenished.
The interval must not be zero.
sourcepub fn const_per_second(self, seconds: u64) -> Self
pub fn const_per_second(self, seconds: u64) -> Self
Set the interval after which one element of the quota is replenished in seconds.
The interval must not be zero.
sourcepub fn const_per_millisecond(self, milliseconds: u64) -> Self
pub fn const_per_millisecond(self, milliseconds: u64) -> Self
Set the interval after which one element of the quota is replenished in milliseconds.
The interval must not be zero.
sourcepub fn const_per_nanosecond(self, nanoseconds: u64) -> Self
pub fn const_per_nanosecond(self, nanoseconds: u64) -> Self
Set the interval after which one element of the quota is replenished in nanoseconds.
The interval must not be zero.
sourcepub fn const_burst_size(self, burst_size: u32) -> Self
pub fn const_burst_size(self, burst_size: u32) -> Self
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.
source§impl<K: KeyExtractor, M: RateLimitingMiddleware<QuantaInstant>> GovernorConfigBuilder<K, M>
impl<K: KeyExtractor, M: RateLimitingMiddleware<QuantaInstant>> GovernorConfigBuilder<K, M>
Sets configuration options when any Key Extractor is provided
sourcepub fn period(&mut self, duration: Duration) -> &mut Self
pub fn period(&mut self, duration: Duration) -> &mut Self
Set the interval after which one element of the quota is replenished.
The interval must not be zero.
sourcepub fn per_second(&mut self, seconds: u64) -> &mut Self
pub fn per_second(&mut self, seconds: u64) -> &mut Self
Set the interval after which one element of the quota is replenished in seconds.
The interval must not be zero.
sourcepub fn per_millisecond(&mut self, milliseconds: u64) -> &mut Self
pub fn per_millisecond(&mut self, milliseconds: u64) -> &mut Self
Set the interval after which one element of the quota is replenished in milliseconds.
The interval must not be zero.
sourcepub fn per_nanosecond(&mut self, nanoseconds: u64) -> &mut Self
pub fn per_nanosecond(&mut self, nanoseconds: u64) -> &mut Self
Set the interval after which one element of the quota is replenished in nanoseconds.
The interval must not be zero.
sourcepub fn burst_size(&mut self, burst_size: u32) -> &mut Self
pub fn burst_size(&mut self, burst_size: u32) -> &mut Self
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.
sourcepub fn methods(&mut self, methods: Vec<Method>) -> &mut Self
pub fn methods(&mut self, methods: Vec<Method>) -> &mut Self
Set the HTTP methods this configuration should apply to. By default this is all methods.
sourcepub fn key_extractor<K2: KeyExtractor>(
&mut self,
key_extractor: K2
) -> GovernorConfigBuilder<K2, M>
pub fn key_extractor<K2: KeyExtractor>( &mut self, key_extractor: K2 ) -> GovernorConfigBuilder<K2, M>
Set the key extractor this configuration should use. By default this is using the PeerIpKeyExtractor.
sourcepub fn use_headers(
&mut self
) -> GovernorConfigBuilder<K, StateInformationMiddleware>
pub fn use_headers( &mut self ) -> GovernorConfigBuilder<K, StateInformationMiddleware>
Set x-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 exceededx-ratelimit-whitelisted
- If the request method not in methods, this header will be add it, usemethods
to add methods
By default x-ratelimit-after
is enabled, with use_headers
will enable x-ratelimit-limit
, x-ratelimit-whitelisted
and x-ratelimit-remaining
sourcepub fn finish(&mut self) -> Option<GovernorConfig<K, M>>
pub fn finish(&mut self) -> Option<GovernorConfig<K, M>>
Finish building the configuration and return the configuration for the middleware.
Returns None
if either burst size or period interval are zero.
Trait Implementations§
source§impl<K: Clone + KeyExtractor, M: Clone + RateLimitingMiddleware<QuantaInstant>> Clone for GovernorConfigBuilder<K, M>
impl<K: Clone + KeyExtractor, M: Clone + RateLimitingMiddleware<QuantaInstant>> Clone for GovernorConfigBuilder<K, M>
source§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 moresource§impl<K: Debug + KeyExtractor, M: Debug + RateLimitingMiddleware<QuantaInstant>> Debug for GovernorConfigBuilder<K, M>
impl<K: Debug + KeyExtractor, M: Debug + RateLimitingMiddleware<QuantaInstant>> Debug for GovernorConfigBuilder<K, M>
source§impl<K: PartialEq + KeyExtractor, M: PartialEq + RateLimitingMiddleware<QuantaInstant>> PartialEq for GovernorConfigBuilder<K, M>
impl<K: PartialEq + KeyExtractor, M: PartialEq + RateLimitingMiddleware<QuantaInstant>> PartialEq for GovernorConfigBuilder<K, M>
source§fn eq(&self, other: &GovernorConfigBuilder<K, M>) -> bool
fn eq(&self, other: &GovernorConfigBuilder<K, M>) -> bool
self
and other
values to be equal, and is used
by ==
.impl<K: Eq + KeyExtractor, M: Eq + RateLimitingMiddleware<QuantaInstant>> Eq for GovernorConfigBuilder<K, M>
impl<K: KeyExtractor, M: RateLimitingMiddleware<QuantaInstant>> StructuralPartialEq for GovernorConfigBuilder<K, M>
Auto Trait Implementations§
impl<K, M> !RefUnwindSafe for GovernorConfigBuilder<K, M>
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>
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
§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<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.