tower-rs / tower

async fn(Request) -> Result<Response, Error>
https://docs.rs/tower
MIT License
3.56k stars 281 forks source link

[Feature Request] `TimeoutLayer` supports per-request values #798

Open cglong opened 4 weeks ago

cglong commented 4 weeks ago

Right now, TimeoutLayer accepts a Duration parameter which specifies how long to wait before aborting a response. I'd like to customize this parameter on a per-request basis.

I'm envisioning this should be doable with a trait with one method; that method takes in a request and returns the Duration. We should be able to implement this trait for Duration itself so this won't be a breaking change.

GlenDC commented 4 weeks ago

I like the idea, but to be clear, this is by definition a non-breaking change. E.g. in case someone was passing a type T as .into() it will no longer compile given the compiler wouldn't know to what concrete type you want to into it.

seanmonstar commented 4 weeks ago

Another thought, if it's cheap for you to create or clone that portion of your service, you could inspect the request, and then clone your inner service and wrap it in a new Timeout layer and the basically oneshot.

cglong commented 4 weeks ago

@GlenDC, I assume that's a typo and you mean it is a breaking change. If so, thank you, that's a good callout!

@seanmonstar, yeah that should be possible for our scenario! Thank you for the workaround 😄