rubyist/circuitbreaker — pull request #7 by rubyist (2014-07-24 13:37:28+00:00)
Title: Build different kinds of circuit breakers

This is an attempt at refactoring the structure of circuit breakers to allow building and composing different kinds of circuit breakers.

It starts with a base `CircuitBreaker` which provides only the ability to `Trip()` and `Reset()` and ask if it is `Tripped()`.

On top of that is a `ResettingBreaker` which can be used to build circuit breakers that attempt to automatically reset. You won't use a ResettingBreaker directly, rather to build other kinds of breakers.

From that a `ThresholdBreaker` is provided which is the first really useful kind of circuit breaker in the package. ThresholdBreaker is a ResettingBreaker that records failures and trips when the failure count hits the threshold. Clients can either use `Call()` as before, or they can record failures manually with `Fail()` then check `Tripped()` to do whatever they want with that state. If doing it manually this way, clients will want to check `State()` for `halfopen` if they want to autoreset. `Call()` will do this automatically.

Finally, a `TimeoutBreaker` is provided which is a ThresholdBreaker that can only use `Call()`. If the call takes longer than the given timeout it will trigger a failure. If the failure threshold is met, the breaker will trip.

From this base, other kinds of breakers should be able to be easily built. One example might be a breaker that trips on error frequency, rather than count (see #6).