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

revert: rate limit based on peer address #1351 #1426

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 changes: 0 additions & 123 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions common/src/models/error.rs
Expand Up @@ -60,7 +60,6 @@ pub enum ErrorKind {
NotReady,
ServiceUnavailable,
DeleteProjectFailed,
RateLimited(String),
}

impl From<ErrorKind> for ApiError {
Expand Down Expand Up @@ -127,12 +126,6 @@ impl From<ErrorKind> for ApiError {
ErrorKind::Forbidden => (StatusCode::FORBIDDEN, "Forbidden"),
ErrorKind::NotReady => (StatusCode::INTERNAL_SERVER_ERROR, "Service not ready"),
ErrorKind::DeleteProjectFailed => (StatusCode::INTERNAL_SERVER_ERROR, "Deleting project failed"),
ErrorKind::RateLimited(message) => {
return Self {
message,
status_code: StatusCode::TOO_MANY_REQUESTS.as_u16(),
}
},
};
Self {
message: error_message.to_string(),
Expand Down
3 changes: 0 additions & 3 deletions deployer/src/handlers/error.rs
Expand Up @@ -26,8 +26,6 @@ pub enum Error {
Internal(#[from] anyhow::Error),
#[error("Missing header: {0}")]
MissingHeader(String),
#[error("{0}. Retry the request in a few minutes")]
RateLimited(String),
}

impl Serialize for Error {
Expand All @@ -49,7 +47,6 @@ impl IntoResponse for Error {

let code = match self {
Error::NotFound(_) => StatusCode::NOT_FOUND,
Error::RateLimited(_) => StatusCode::TOO_MANY_REQUESTS,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};

Expand Down
34 changes: 6 additions & 28 deletions deployer/src/handlers/mod.rs
Expand Up @@ -33,7 +33,7 @@ use shuttle_common::{
claims::{Claim, Scope},
models::{
deployment::{DeploymentRequest, CREATE_SERVICE_BODY_LIMIT, GIT_STRINGS_MAX_LENGTH},
error::{axum::CustomErrorPath, ApiError, ErrorKind},
error::axum::CustomErrorPath,
project::ProjectName,
},
request_span, LogItem,
Expand Down Expand Up @@ -663,16 +663,8 @@ pub async fn get_logs(
.collect(),
)),
Err(error) => {
if error.code() == tonic::Code::Unavailable
&& error.metadata().get("x-ratelimit-limit").is_some()
{
Err(Error::RateLimited(
"your application is producing too many logs. Interactions with the shuttle logger service will be rate limited"
.to_string(),
))
} else {
Err(anyhow!("failed to retrieve logs for deployment").into())
}
error!(error = %error, "failed to retrieve logs for deployment");
Err(anyhow!("failed to retrieve logs for deployment").into())
Comment on lines +666 to +667
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this were to be reverted, it would be reverted to return a 404, but that would be incorrect, so I left this in.

}
}
}
Expand Down Expand Up @@ -723,24 +715,10 @@ async fn logs_websocket_handler(
"failed to get backlog of logs"
);

if error.code() == tonic::Code::Unavailable
&& error.metadata().get("x-ratelimit-limit").is_some()
{
let message = serde_json::to_string(
&ApiError::from(
ErrorKind::RateLimited(
"your application is producing too many logs. Interactions with the shuttle logger service will be rate limited"
.to_string()
)
))
.expect("to convert error to json");
let _ = s
.send(ws::Message::Text("failed to get logs".to_string()))
.await;

let _ = s.send(ws::Message::Text(message)).await;
} else {
let _ = s
.send(ws::Message::Text("failed to get logs".to_string()))
.await;
}
let _ = s.close().await;
return;
}
Expand Down
5 changes: 0 additions & 5 deletions logger/Cargo.toml
Expand Up @@ -12,7 +12,6 @@ shuttle-proto = { workspace = true, features = ["logger"] }
async-trait = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true }
http = { workspace = true }
prost-types = { workspace = true }
serde_json = { workspace = true }
sqlx = { workspace = true, features = [
Expand All @@ -25,8 +24,6 @@ thiserror = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread"] }
tokio-stream = { workspace = true }
tonic = { workspace = true }
tower = { workspace = true }
tower_governor = { version= "0.1.0", features = ["tracing"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["default"] }

Expand All @@ -38,5 +35,3 @@ serde_json = { workspace = true }
shuttle-common-tests = { workspace = true }
uuid = { workspace = true }
ctor = { workspace = true }
futures = { workspace = true }
tower = { workspace = true, features = ["util"] }
1 change: 0 additions & 1 deletion logger/src/lib.rs
Expand Up @@ -15,7 +15,6 @@ use tracing::{debug, error, field, Span};

pub mod args;
mod dal;
pub mod rate_limiting;

pub use dal::Postgres;

Expand Down
31 changes: 2 additions & 29 deletions logger/src/main.rs
Expand Up @@ -8,15 +8,9 @@ use shuttle_common::{
},
log::Backend,
};
use shuttle_logger::{
args::Args,
rate_limiting::{tonic_error, TonicPeerIpKeyExtractor, BURST_SIZE, REFRESH_INTERVAL},
Postgres, Service,
};
use shuttle_logger::{args::Args, Postgres, Service};
use shuttle_proto::logger::logger_server::LoggerServer;
use tonic::transport::Server;
use tower::ServiceBuilder;
use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer};
use tracing::trace;

#[tokio::main]
Expand All @@ -27,33 +21,12 @@ async fn main() {

trace!(args = ?args, "parsed args");

let governor_config = GovernorConfigBuilder::default()
// Regenerate capacity at a rate of 2 requests per second, meaning the maximum capacity
// for sustained traffic is 2 RPS per peer address.
.per_millisecond(REFRESH_INTERVAL)
// Allow bursts of up to 6 requests, when any burst capacity is used, it will regenerate
// one element at a time at the rate set above.
.burst_size(BURST_SIZE)
.use_headers()
.key_extractor(TonicPeerIpKeyExtractor)
.finish()
.unwrap();

let mut server_builder = Server::builder()
.http2_keepalive_interval(Some(Duration::from_secs(60)))
.layer(JwtAuthenticationLayer::new(AuthPublicKey::new(
args.auth_uri,
)))
.layer(ExtractPropagationLayer)
.layer(
ServiceBuilder::new()
// This middleware goes above `GovernorLayer` because it will receive errors returned by
// `GovernorLayer`.
.map_err(tonic_error)
.layer(GovernorLayer {
config: &governor_config,
}),
);
.layer(ExtractPropagationLayer);

let postgres = Postgres::new(&args.db_connection_uri).await;

Expand Down