class CircuitBreaker
- CircuitBreaker
- Reference
- Object
Overview
Simple Implementation of the circuit breaker pattern in Crystal.
Given a certain error threshold, timeframe and timeout window, a breaker can be used to monitor criticial command executions. Circuit breakers are usually used to prevent unnecessary requests if a server ressource et al becomes unavailable. This protects the server from additional load and allows it to recover and relieves the client from requests that are doomed to fail.
Wrap API calls inside a breaker, if the error rate in a given time frame surpasses a certain threshold, all subsequent calls will fail for a given duration.
Defined in:
circuit_breaker.crConstructors
-
.new(threshold error_threshold : Int32, timewindow timeframe, reenable_after duration : Int32, handled_errors = [] of Exception, ignored_errors = [] of Exception)
creates a CircuitBreaker instance with a specified error threshold, timeframe, breaker duration and optionally a number of ignored or handled errors
Instance Method Summary
-
#run(&)
get's passed a block to watch for errors every error thrown inside your block counts towards the error rate once the threshold is surpassed, it starts throwing
CircuitOpenException
s you can catch these rrors and implement some fallback behaviourbegin breaker.run do my_rest_call() end rescue exc : CircuitOpenException log "happens to the best of us..." 42 end
Constructor Detail
creates a CircuitBreaker instance with a specified error threshold, timeframe, breaker duration and optionally a number of ignored or handled errors
breaker = CircuitBreaker.new(
threshold: 5, # % of errors before you want to trip the circuit
timewindow: 60, # in s: anything older will be ignored in error_rate
reenable_after: 300 # after x seconds, the breaker will allow executions again
)
Instance Method Detail
get's passed a block to watch for errors
every error thrown inside your block counts towards the error rate
once the threshold is surpassed, it starts throwing CircuitOpenException
s
you can catch these rrors and implement some fallback behaviour
begin
breaker.run do
my_rest_call()
end
rescue exc : CircuitOpenException
log "happens to the best of us..."
42
end