pub struct RateLimiter<T>{ /* 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>,
impl<T> RateLimiter<T>where
T: Handler<ActorMessage> + Send + Sync + 'static,
<T as Actor>::Context: ToEnvelope<T, ActorMessage>,
Sourcepub fn new(store: Addr<T>) -> Self
pub fn new(store: Addr<T>) -> Self
Creates a new instance of RateLimiter with the provided address of StoreActor.
Sourcepub fn with_interval(self, interval: Duration) -> Self
pub fn with_interval(self, interval: Duration) -> Self
Specify the interval. The counter for a client is reset after this interval
Sourcepub fn with_max_requests(self, max_requests: usize) -> Self
pub fn with_max_requests(self, max_requests: usize) -> Self
Specify the maximum number of requests allowed in the given interval.
Sourcepub fn with_identifier<F: Fn(&ServiceRequest) -> Result<String, ARError> + 'static>(
self,
identifier: F,
) -> Self
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,
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
type Request = ServiceRequest
Requests handled by the service.
Source§type Response = ServiceResponse<B>
type Response = ServiceResponse<B>
Responses given by the service.
Source§type Transform = RateLimitMiddleware<S, T>
type Transform = RateLimitMiddleware<S, T>
The
TransformService value created by this factorySource§type Future = Ready<Result<<RateLimiter<T> as Transform<S>>::Transform, <RateLimiter<T> as Transform<S>>::InitError>>
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
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>
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
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> 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
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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