Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rate-limiting to faucet #5981

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/sui-faucet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sui::client_commands::WalletContext;
use sui_config::{sui_config_dir, SUI_CLIENT_CONFIG};
use sui_faucet::{Faucet, FaucetRequest, FaucetResponse, SimpleFaucet};
use sui_metrics::spawn_monitored_task;
use tower::ServiceBuilder;
use tower::{limit::RateLimitLayer, ServiceBuilder};
use tower_http::cors::{Any, CorsLayer};
use tracing::{info, warn};
use uuid::Uuid;
Expand Down Expand Up @@ -52,6 +52,9 @@ struct FaucetConfig {

#[clap(long, default_value_t = 120)]
timeout_in_seconds: u64,

#[clap(long, default_value_t = 10)]
max_request_per_second: u64,
}

struct AppState<F = SimpleFaucet> {
Expand Down Expand Up @@ -84,6 +87,7 @@ async fn main() -> Result<(), anyhow::Error> {
port,
request_buffer_size,
timeout_in_seconds,
max_request_per_second,
..
} = config;

Expand Down Expand Up @@ -112,6 +116,10 @@ async fn main() -> Result<(), anyhow::Error> {
.layer(HandleErrorLayer::new(handle_error))
.layer(cors)
.buffer(request_buffer_size)
.layer(RateLimitLayer::new(
max_request_per_second,
Duration::from_secs(1),
))
.concurrency_limit(max_concurrency)
.timeout(Duration::from_secs(timeout_in_seconds))
.layer(Extension(app_state))
Expand Down