Does anyone have any strategies for setting per-re...
# http4k
a
Does anyone have any strategies for setting per-request timeouts? In the past, I've injected a
fastInternet
and
slowInternet
into my application, but I dislike this approach, and desire even greater flexibility. I know that resilience4j has the time limiter, but I'm unsure if it would gracefully cleanup the connections as well as the HTTP client implementation would.
At this point, I'm considering injecting a
Copy code
fun interface TimedRequestHandler: (Request, Duration) -> Response
and caching several client implementations with various timeouts internally. But I'm unsure about the potential overhead from having so many clients with their own connection pools sitting in memory forever.
j
I suspect you can do this in two parts. One with an http4k thing, setting a header that will be interpreted lower down in the stack in an implementation-specific handler to set the request timeout.
Assuming okhttp as client, something like this, but without all the annotations https://stackoverflow.com/questions/46845206/how-to-change-timeout-for-a-request-in-okhttp
a
Yes, a header could work, if a bit "magical" 🤔
I used a custom header with an okhttp interceptor as you suggested. Works well, thanks.
👍 1
j
is it a thing you could share? -e.g. as a cookbook example?
a
Most likely yes. Wonder what format would be most appropriate 🤔
j
not sure, maybe (if it is short) - just some .md in src/docs/guide/howto ?