dakrone / clj-http

An idiomatic clojure http client wrapping the apache client. Officially supported version.
http://clojars.org/clj-http
MIT License
1.77k stars 408 forks source link

Retry when service is (tempoarily) unavailable #643

Open jacobemcken opened 6 months ago

jacobemcken commented 6 months ago

Retries work well with IOExceptions (like timeouts). :+1:

I had assumed that retries would also be applied, when a service is reported (temporarily) unavailable like 502, 503 & 504

When reading about the Apache HttpClient, it seems a ServiceUnavailableRetryStrategy exists for the cases mentioned above.

Is it on purpose this is left out of clj-http?

jacobemcken commented 6 months ago

When receiving a 503 I would expect the code to automatically retry:

(client/request {:url "https://httpbin.org/status/503"
                 :method :get
                 :retry-handler (fn [ex try-count http-context]
                                  (println "Got:" ex)
                                  (if (> try-count 4) false true))})

I read about the ServiceUnavailableRetryStrategy class on a blog: https://kevcodez.de/posts/2020-09-27-resilient-apache-http-client/

dakrone commented 6 months ago

I don't think we should retry on 5XX responses by default, but it would be fine to expose the ServiceUnavailableRetryStrategy in some way also, if someone wanted to use it.

agorgl commented 4 months ago

On the same ship, working with some APIs that sporadically show some 503 responses for a moment, would be great to expose the ServiceUnavailableRetryStrategy in order to allow an :unavailable-handler or something to use this functionality