[][src]Struct actix_ratelimit::middleware::RateLimiter

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

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);
}

Methods

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

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

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

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

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

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

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

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

Function to get the identifier for the client request

Trait Implementations

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 = AWError> + 'static,
    S::Future: 'static,
    B: 'static, 
[src]

type Request = ServiceRequest

Requests handled by the service.

type Response = ServiceResponse<B>

Responses given by the service.

type Error = S::Error

Errors produced by the service.

type InitError = ()

Errors produced while building a transform service.

type Transform = RateLimitMiddleware<S, T>

The TransformService value created by this factory

type Future = Ready<Result<Self::Transform, Self::InitError>>

The future response value.

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

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