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>

source

pub fn error_handler<F>(&mut self, func: F) -> &mut Self
where F: Fn(GovernorError) -> Response<Body> + Send + Sync + 'static,

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>

Sets the default Governor Config and defines all the different configuration functions This one is used when the default PeerIpKeyExtractor is used

source

pub fn const_default() -> Self

source

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.

source

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.

source

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.

source

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.

source

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>

Sets configuration options when any Key Extractor is provided

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn use_headers( &mut self ) -> GovernorConfigBuilder<K, StateInformationMiddleware>

Set x-ratelimit headers to response, the headers is

  • x-ratelimit-limit - Request limit
  • x-ratelimit-remaining - The number of requests left for the time window
  • x-ratelimit-after - Number of seconds in which the API will become available after its rate limit has been exceeded
  • x-ratelimit-whitelisted - If the request method not in methods, this header will be add it, use methods 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

source

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>

source§

fn clone(&self) -> GovernorConfigBuilder<K, M>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: Debug + KeyExtractor, M: Debug + RateLimitingMiddleware<QuantaInstant>> Debug for GovernorConfigBuilder<K, M>

source§

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

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

impl Default for GovernorConfigBuilder<PeerIpKeyExtractor, NoOpMiddleware>

source§

fn default() -> Self

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.

source§

impl<K: PartialEq + KeyExtractor, M: PartialEq + RateLimitingMiddleware<QuantaInstant>> PartialEq for GovernorConfigBuilder<K, M>

source§

fn eq(&self, other: &GovernorConfigBuilder<K, M>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K: Eq + KeyExtractor, M: Eq + RateLimitingMiddleware<QuantaInstant>> Eq for GovernorConfigBuilder<K, M>

source§

impl<K: KeyExtractor, M: RateLimitingMiddleware<QuantaInstant>> StructuralPartialEq for GovernorConfigBuilder<K, M>

Auto Trait Implementations§

§

impl<K, M> Freeze for GovernorConfigBuilder<K, M>
where K: Freeze,

§

impl<K, M> !RefUnwindSafe for GovernorConfigBuilder<K, M>

§

impl<K, M> Send for GovernorConfigBuilder<K, M>
where K: Send, M: Send,

§

impl<K, M> Sync for GovernorConfigBuilder<K, M>
where K: Sync, M: Sync,

§

impl<K, M> Unpin for GovernorConfigBuilder<K, M>
where K: Unpin, M: Unpin,

§

impl<K, M> !UnwindSafe for GovernorConfigBuilder<K, M>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromRef<T> for T
where T: Clone,

source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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

§

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

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,