GithubHelp home page GithubHelp logo

Comments (4)

davidpdrsn avatar davidpdrsn commented on June 28, 2024 1

You can do this:

use axum::{
    error_handling::HandleErrorLayer,
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::get,
    BoxError, Router,
};
use std::time::Duration;
use tower::{buffer::BufferLayer, limit::RateLimitLayer, ServiceBuilder};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async {})).layer(
        ServiceBuilder::new()
            .layer(HandleErrorLayer::new(handle_middleware_error))
            .layer(BufferLayer::new(1024))
            .layer(RateLimitLayer::new(10, Duration::from_secs(1))),
    );

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn handle_middleware_error(err: BoxError) -> Response {
    StatusCode::INTERNAL_SERVER_ERROR.into_response()
}

Also I was wrong about tower's rate limit changing the error type. It just relies on poll_ready.

from tower.

davidpdrsn avatar davidpdrsn commented on June 28, 2024

That is pretty much what Buffer does.

Specifically in terms of rate limiting I don't think tower's option is a good fit for http servers because

  1. It isn't Clone like you mention
  2. It changes the error type, whereas axum requires Infallible so you always send a response. Instead you have to do that mapping manually using axum's error handling stuff
  3. It does't provide any feedback to the client through response headers.

I think a more HTTP specific rate limiting could be added to tower-http.

from tower.

teamwork-alockhart avatar teamwork-alockhart commented on June 28, 2024

@davidpdrsn
Thank you for the quick response.
I see your point here - it's not very ergonomic in combination with Axum's API.
I saw this previous issue opened in Axum in which you answered that BufferLayer could be used as a workaround, but I wasn't able to figure that out unfortunately.
To be honest, I'm a little bit out of my depth, but if there is something I can do to contribute to legitimate solution, I would like to help.

from tower.

teamwork-alockhart avatar teamwork-alockhart commented on June 28, 2024

Thanks for showing me how to do this! I'll have to read a bit more on how this works, but I think I understand.
I guess I'll close for now - it seems like this is an easy enough workaround that a solution like the one you mentioned above wouldn't be in high demand.

from tower.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.