sunng87 / diehard

Clojure resilience library for flexible retry, circuit breaker and rate limiter
Eclipse Public License 2.0
330 stars 27 forks source link

Add support for rate-based circuit-breaker #44

Closed mrichards42 closed 3 years ago

mrichards42 commented 3 years ago

Adds support for Failsafe's rate-based CircuitBreaker using withFailureRateThreshold.

Naming is a compromise between Failsafe ("rate") and existing names ("in-period"), but I realize "rate" vs "ratio" may end up being a source of confusion. I had assumed that the :failure-threshold-ratio... options used rate thresholding, but was surprised to see that they are actually count-based.

Based on my reading of the Failsafe docs and some testing, (.withFailureThreshold cb 50 500) (aka :failure-threshold-ratio [50 500]) would actually trip after 50 total failures as long as 500 executions have happened ever, not 50 out of the last 500 executions. That is, repeating 1000 successes and 10 failures 5 times (for a total of 50 failures and 5000 successes) would end up tripping the circuit, even though the failure ratio in this case is < 1% in total (50 out of 5050), and only 2% in the last 500 (10 out of 500).

sunng87 commented 3 years ago

Thanks!

sunng87 commented 3 years ago

By the way, it would be nice to add doc in core.clj to describe the this new option. Also a test for this behavior is also welcomed.