Struct afire::extension::RateLimiter
source ยท pub struct RateLimiter { /* private fields */ }
Expand description
Limit the amount of requests handled by the server.
Implementationsยง
sourceยงimpl RateLimiter
impl RateLimiter
sourcepub fn new() -> RateLimiter
pub fn new() -> RateLimiter
Make a new RateLimiter.
Default limit is 10 and timeout is 60
Examples found in repository?
examples/basic/rate_limit.rs (line 29)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a handler for GET "/"
server.route(Method::GET, "/", |_req| {
Response::new().text("Hello World!").content(Content::TXT)
});
// For this example, we'll limit requests to 1 every 2 seconds
// Make a new Ratelimater
// Default Limit is 10
// Default Timeout is 60 sec
RateLimiter::new()
// Override the Limit to 1
.limit(1)
// Override the timeout to 2
.timeout(2)
// Override the Handler
.handler(Box::new(|_req| {
Some(
Response::new()
.status(Status::TooManyRequests)
.text("AHHHH!!! Too Many Requests")
.content(Content::TXT),
)
}))
// Attach to the server
.attach(&mut server);
// Now if you goto http://localhost:8080/ and reload a bunch of times,
// you'll see the rate limiter kicking in.
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn limit(self, limit: u64) -> RateLimiter
pub fn limit(self, limit: u64) -> RateLimiter
Set the request limit per timeout Attach the rate limiter to a server.
Example
// Import Lib
use afire::{Server, extension::RateLimiter, Middleware};
// Create a new server
let mut server = Server::<()>::new("localhost", 1234);
// Add a rate limiter
RateLimiter::new()
// Override limit to 100 requests
.limit(100)
// Attach it to the server
.attach(&mut server);
// Start Server
// This is blocking
server.start().unwrap();
Examples found in repository?
examples/basic/rate_limit.rs (line 31)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a handler for GET "/"
server.route(Method::GET, "/", |_req| {
Response::new().text("Hello World!").content(Content::TXT)
});
// For this example, we'll limit requests to 1 every 2 seconds
// Make a new Ratelimater
// Default Limit is 10
// Default Timeout is 60 sec
RateLimiter::new()
// Override the Limit to 1
.limit(1)
// Override the timeout to 2
.timeout(2)
// Override the Handler
.handler(Box::new(|_req| {
Some(
Response::new()
.status(Status::TooManyRequests)
.text("AHHHH!!! Too Many Requests")
.content(Content::TXT),
)
}))
// Attach to the server
.attach(&mut server);
// Now if you goto http://localhost:8080/ and reload a bunch of times,
// you'll see the rate limiter kicking in.
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn timeout(self, timeout: u64) -> RateLimiter
pub fn timeout(self, timeout: u64) -> RateLimiter
Set the Ratelimit refresh period
Example
// Import Lib
use afire::{Server, extension::RateLimiter, Middleware};
// Create a new server
let mut server = Server::<()>::new("localhost", 1234);
// Add a rate limiter
RateLimiter::new()
// Override timeout to 60 seconds
.timeout(60)
// Attach it to the server
.attach(&mut server);
// Start Server
// This is blocking
server.start().unwrap();
Examples found in repository?
examples/basic/rate_limit.rs (line 33)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a handler for GET "/"
server.route(Method::GET, "/", |_req| {
Response::new().text("Hello World!").content(Content::TXT)
});
// For this example, we'll limit requests to 1 every 2 seconds
// Make a new Ratelimater
// Default Limit is 10
// Default Timeout is 60 sec
RateLimiter::new()
// Override the Limit to 1
.limit(1)
// Override the timeout to 2
.timeout(2)
// Override the Handler
.handler(Box::new(|_req| {
Some(
Response::new()
.status(Status::TooManyRequests)
.text("AHHHH!!! Too Many Requests")
.content(Content::TXT),
)
}))
// Attach to the server
.attach(&mut server);
// Now if you goto http://localhost:8080/ and reload a bunch of times,
// you'll see the rate limiter kicking in.
// Start the server
// This will block the current thread
server.start().unwrap();
}
sourcepub fn handler(
self,
handler: Box<dyn Fn(&Request) -> Option<Response> + Send + Sync>
) -> RateLimiter
pub fn handler( self, handler: Box<dyn Fn(&Request) -> Option<Response> + Send + Sync> ) -> RateLimiter
Define a Custom Handler for when a client has exceeded the ratelimit. If the handler returns None, the request will be processed normally.
Example
// Import Lib
use afire::{Server, Response, extension::RateLimiter, Middleware};
// Create a new server
let mut server = Server::<()>::new("localhost", 1234);
// Add a rate limiter
RateLimiter::new()
// Override the handler for requests exceeding the limit
.handler(Box::new(|_req| Some(Response::new().text("much request"))))
// Attach it to the server
.attach(&mut server);
// Start Server
// This is blocking
server.start().unwrap();
Examples found in repository?
examples/basic/rate_limit.rs (lines 35-42)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a handler for GET "/"
server.route(Method::GET, "/", |_req| {
Response::new().text("Hello World!").content(Content::TXT)
});
// For this example, we'll limit requests to 1 every 2 seconds
// Make a new Ratelimater
// Default Limit is 10
// Default Timeout is 60 sec
RateLimiter::new()
// Override the Limit to 1
.limit(1)
// Override the timeout to 2
.timeout(2)
// Override the Handler
.handler(Box::new(|_req| {
Some(
Response::new()
.status(Status::TooManyRequests)
.text("AHHHH!!! Too Many Requests")
.content(Content::TXT),
)
}))
// Attach to the server
.attach(&mut server);
// Now if you goto http://localhost:8080/ and reload a bunch of times,
// you'll see the rate limiter kicking in.
// Start the server
// This will block the current thread
server.start().unwrap();
}
Trait Implementationsยง
sourceยงimpl Debug for RateLimiter
impl Debug for RateLimiter
sourceยงimpl Default for RateLimiter
impl Default for RateLimiter
sourceยงimpl Middleware for RateLimiter
impl Middleware for RateLimiter
sourceยงfn pre(&self, req: &mut Request) -> MiddleResult
fn pre(&self, req: &mut Request) -> MiddleResult
Middleware to run Before Routes
sourceยงfn end(&self, req: &Request, _res: &Response)
fn end(&self, req: &Request, _res: &Response)
Middleware ot run after the response has been handled
sourceยงfn pre_raw(&self, req: &mut Result<Request>) -> MiddleResult
fn pre_raw(&self, req: &mut Result<Request>) -> MiddleResult
Middleware to run before routes.
Because this is the
raw
version of Middleware::pre
, it is passed a Result
.
The default implementation calls Middleware::pre
if the Result
is Ok
.sourceยงfn post_raw(
&self,
req: Result<Rc<Request>>,
res: &mut Result<Response>
) -> MiddleResult
fn post_raw( &self, req: Result<Rc<Request>>, res: &mut Result<Response> ) -> MiddleResult
Middleware to run after routes.
Because this is the
raw
version of Middleware::post
, it is passed a Result
.
The default implementation calls Middleware::post
if the Result
is Ok
.sourceยงfn post(&self, _req: &Request, _res: &mut Response) -> MiddleResult
fn post(&self, _req: &Request, _res: &mut Response) -> MiddleResult
Middleware to run After Routes
Auto Trait Implementationsยง
impl !RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl !UnwindSafe for RateLimiter
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