GovernorConfigBuilder

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

pub fn const_default() -> 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>

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>

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>

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>

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

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>

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>

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>

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>

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>

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,

Set the key extractor this configuration should use. By default this is using the [PeerIpKeyExtractor].

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

Set 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
  • retry-after - Same value as x-ratelimit-after
  • 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 and retry-after are enabled, with use_headers will enable x-ratelimit-limit, x-ratelimit-whitelisted and x-ratelimit-remaining

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§

§

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

§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

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

§

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

Formats the value using the given formatter. Read more
§

impl Default for 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>
where K: PartialEq + KeyExtractor, M: PartialEq + RateLimitingMiddleware<QuantaInstant>,

§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

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

§

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>

§

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>
where K: UnwindSafe, M: UnwindSafe,

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

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

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

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.

§

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

§

fn from_ref(input: &T) -> T

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

impl<T> Instrument for T

§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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

Source§

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

Source§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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