RateLimiter

Struct RateLimiter 

Source
pub struct RateLimiter<T>
where T: Handler<ActorMessage> + Send + Sync + 'static, T::Context: ToEnvelope<T, ActorMessage>,
{ /* private fields */ }
Expand description

Type that implements the ratelimit middleware.

This accepts interval which specifies the window size, max_requests which specifies the maximum number of requests in that window, and store which is essentially a data store used to store client access information. Entry is removed from the store after interval.

§Example

use actix_ratelimit::{MemoryStore, MemoryStoreActor};
use actix_ratelimit::RateLimiter;

#[actix_rt::main]
async fn main() {
    let store = MemoryStore::new();
    let ratelimiter = RateLimiter::new(
                            MemoryStoreActor::from(store.clone()).start())
                        .with_interval(Duration::from_secs(60))
                        .with_max_requests(100);
}

Implementations§

Source§

impl<T> RateLimiter<T>
where T: Handler<ActorMessage> + Send + Sync + 'static, <T as Actor>::Context: ToEnvelope<T, ActorMessage>,

Source

pub fn new(store: Addr<T>) -> Self

Creates a new instance of RateLimiter with the provided address of StoreActor.

Source

pub fn with_interval(self, interval: Duration) -> Self

Specify the interval. The counter for a client is reset after this interval

Source

pub fn with_max_requests(self, max_requests: usize) -> Self

Specify the maximum number of requests allowed in the given interval.

Source

pub fn with_identifier<F: Fn(&ServiceRequest) -> Result<String, ARError> + 'static>( self, identifier: F, ) -> Self

Function to get the identifier for the client request

Trait Implementations§

Source§

impl<T, S, B> Transform<S> for RateLimiter<T>
where T: Handler<ActorMessage> + Send + Sync + 'static, T::Context: ToEnvelope<T, ActorMessage>, S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static, S::Future: 'static, B: 'static,

Source§

type Request = ServiceRequest

Requests handled by the service.
Source§

type Response = ServiceResponse<B>

Responses given by the service.
Source§

type Error = <S as Service>::Error

Errors produced by the service.
Source§

type InitError = ()

Errors produced while building a transform service.
Source§

type Transform = RateLimitMiddleware<S, T>

The TransformService value created by this factory
Source§

type Future = Ready<Result<<RateLimiter<T> as Transform<S>>::Transform, <RateLimiter<T> as Transform<S>>::InitError>>

The future response value.
Source§

fn new_transform(&self, service: S) -> Self::Future

Creates and returns a new Transform component, asynchronously
Source§

fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
where Self: Sized, F: Fn(Self::InitError) -> E + Clone,

Map this transforms’s factory error to a different error, returning a new transform service factory.

Auto Trait Implementations§

§

impl<T> Freeze for RateLimiter<T>

§

impl<T> !RefUnwindSafe for RateLimiter<T>

§

impl<T> !Send for RateLimiter<T>

§

impl<T> !Sync for RateLimiter<T>

§

impl<T> Unpin for RateLimiter<T>

§

impl<T> !UnwindSafe for RateLimiter<T>

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
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<T> ErasedDestructor for T
where T: 'static,