Open Sagebati opened 1 month ago
Would Steer work? https://docs.rs/tower/latest/tower/steer/index.html
Steer seems to have everything I wanted, but I struggle passing it as a layer
let mut rate_limiter = Steer::new(
vec![BoxLayer::new(user_rate_limiter), BoxLayer::new(global_rate_limiter)],
|req: Request, services| {
let header = req.headers().typed_get::<Authorization<Bearer>>();
match header
.as_ref()
.and_then(|header| BearerToken::from_str(header.token()).ok())
{
Some(_) => 0, // If some it means that we have a valid token so we authorize more request
None => 1, // If not we limit the number of request per second
}
},
);
base.layer(middleware!())
.layer(rate_limiter.as_service()) //
Got
.layer(rate_limiter)
| ----- ^^^^^^^^^^^^ the trait `tower::Layer<Route>` is not implemented for `Steer<BoxLayer<_, http::Request<_>, _,
Ah, because steer doesn't have a layer impl.
So there is no way to do it, with Steer ? I misinformed, from the beginning I was trying to combine layers and not services. I still struggle to differentiate them
I saw that there was a draft of the "same" idea https://github.com/tower-rs/tower/pull/562. Is this something that has some value for a feature request ?
So the I have a case where I want to have two behaviors if a token is set or not. I didn't want to write my service, so I tried to use the service contaminators (optional, filter, either, etc..)
But what I wanted to do I not handled (I think) example.
I have a Predicate on the request and I want to Run either A or B depending of the predicate
I'm working on an implementation that could live in the
mod util
Draft: