Comments (4)
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.
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
- It isn't Clone like you mention
- 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 - 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.
@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.
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)
- Type erased sync Service HOT 8
- Etag middleware HOT 8
- Weighted balance::p2c HOT 3
- `BoxService` is unnecessarily `!Sync` HOT 1
- call_all should stop after poll_ready returns an error HOT 1
- Sharing Collections of Layers: BoxCloneLayer? HOT 1
- Support weights for request rate limiting HOT 1
- Can I ask one question? HOT 4
- Publish `0.5` release HOT 5
- `Retry<RetryPolicy, RateLimit<Client>>` does not work HOT 7
- Idea: Preventing Inappropriate Service Invocation HOT 1
- Returning a response from a tower layer HOT 2
- AsyncFilterLayer is missing Clone impl
- Have MakeBalance and MakeBalanceLayer example?
- `Reconnect::new()` Generic parameters are redundant HOT 2
- Adding option_layer causes trait bound unsastisfied HOT 1
- Publish release without pin-project (with pin-project-lite) HOT 4
- `tower::service_fn` docs don't say that you need the `util` flag HOT 3
- MQTT client adapter / framework HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tower.