-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53bd46b
commit 3fdc116
Showing
6 changed files
with
103 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,24 @@ | ||
## Running the dragonfly backed sliding window rate limiter | ||
## How to run the 2 servers plus a rate limiter: | ||
|
||
### Running the dragonfly backed sliding window rate limiter | ||
|
||
```bash | ||
docker-compose up -d | ||
``` | ||
|
||
### Run first server | ||
|
||
```bash | ||
cargo run --bin server_1 | ||
``` | ||
|
||
### Run second server | ||
|
||
``` | ||
cargo run --bin server_2 | ||
``` | ||
|
||
|
||
- Using postman or Insomnia, call this endpoint until it returns 429 (Automate the requests sent (60 to be exact)) `localhost:8080/limited` | ||
|
||
- Then call this endpoint `localhost:8081/limited` and you will still get `429` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use actix_web::{web, App, HttpServer}; | ||
use anyhow::Context; | ||
use ratelimiter::{ | ||
limited, rate_limiters::sliding_window_counter::distributed::DistributedSlidingWindowCounter, | ||
AppStateWithIpRateLimiter, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
let rate_limiter = DistributedSlidingWindowCounter::new().await?; | ||
HttpServer::new(move || { | ||
let rate_limiter = AppStateWithIpRateLimiter::new(rate_limiter.clone()); | ||
App::new() | ||
.app_data(web::Data::new(rate_limiter)) | ||
.service(limited) | ||
}) | ||
.bind(("127.0.0.1", 8080)) | ||
.context("Failed to bind to port")? | ||
.run() | ||
.await | ||
.context("Failed to run the server") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use actix_web::{web, App, HttpServer}; | ||
use anyhow::Context; | ||
use ratelimiter::{ | ||
limited, rate_limiters::sliding_window_counter::distributed::DistributedSlidingWindowCounter, | ||
AppStateWithIpRateLimiter, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
let rate_limiter = DistributedSlidingWindowCounter::new().await?; | ||
HttpServer::new(move || { | ||
let rate_limiter = AppStateWithIpRateLimiter::new(rate_limiter.clone()); | ||
App::new() | ||
.app_data(web::Data::new(rate_limiter)) | ||
.service(limited) | ||
}) | ||
.bind(("127.0.0.1", 8081)) | ||
.context("Failed to bind to port")? | ||
.run() | ||
.await | ||
.context("Failed to run the server") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,34 @@ | ||
use actix_web::{get, web, HttpResponse, Responder}; | ||
use rate_limiters::sliding_window_counter::distributed::DistributedSlidingWindowCounter; | ||
use tokio::sync::Mutex; | ||
|
||
pub mod rate_limiters; | ||
|
||
#[get("/limited")] | ||
async fn limited(data: web::Data<AppStateWithIpRateLimiter>) -> impl Responder { | ||
let limiter = &mut data.limiter.lock().await; | ||
let consumed_result = limiter.consume_token().await; | ||
let consumed = match consumed_result { | ||
Ok(consumed) => consumed, | ||
Err(err) => { | ||
return HttpResponse::InternalServerError() | ||
.body(format!("Internal Server Error: {err:?}")) | ||
} | ||
}; | ||
if consumed { | ||
return HttpResponse::Ok().body("Limited, but ok for now, don't over use me!"); | ||
} | ||
HttpResponse::TooManyRequests().body("Rate limit exceeded, try again later") | ||
} | ||
|
||
pub struct AppStateWithIpRateLimiter { | ||
limiter: Mutex<DistributedSlidingWindowCounter>, | ||
} | ||
|
||
impl AppStateWithIpRateLimiter { | ||
pub fn new(limiter: DistributedSlidingWindowCounter) -> Self { | ||
Self { | ||
limiter: Mutex::new(limiter), | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters