Skip to content

429 status code for RateLimitLayer #1760

Answered by davidpdrsn
kimbauters asked this question in Q&A
Discussion options

You must be logged in to vote

RateLimit will send backpressure up the stack making things wait. To convert that "not ready" state into an error you need LoadShed.

Here is an example:

use std::time::Duration;

use axum::{
    error_handling::HandleErrorLayer,
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::get,
    BoxError, Router,
};
use tower::{
    buffer::BufferLayer,
    limit::RateLimitLayer,
    load_shed::{error::Overloaded, LoadShedLayer},
    ServiceBuilder,
};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async {})).layer(
        ServiceBuilder::new()
            .layer(HandleErrorLayer::new(handle_error))
            .layer(BufferLayer::new(1024))

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kimbauters
Comment options

Answer selected by kimbauters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants