Lua implementation of the Circuit Breaker pattern.
local moonbreaker = require "moonbreaker"
-- Create Circuit Breaker instance with some settings.
local breaker = moonbreaker {
limit = 10,
timeout = 60,
}
-- We have some error-prone service function...
local function service(threshold)
if math.random() > threshold then
return nil, "error"
end
return true
end
-- ...and we can decorate it with our breaker instance.
service = breaker(service)
-- Now we’re ready to call it!
local ok, err = service(0.5)
See spec/moonbreaker_spec.lua
for details.
Setting | Type | Default |
---|---|---|
|
|
|
|
|
|
|
|
|
Transition Conditions |
||
|
|
|
|
|
|
Interaction With Service Function |
||
|
||
|
||
|
||
|
||
Other |
||
|
||
|
|
local breaker = moonbreaker {
limit = 5,
timeout = 60,
interval = 2 * 60,
should_open = moonbreaker.on.total_failures_rate {
rate = 0.7,
min_samples = 10,
},
success = moonbreaker.success.always,
report_error = moonbreaker.error_reporting.error,
notify = function (state)
print("next state: " .. state)
end,
}
local states = moonbreaker.states
state
is one of:
-
states.closed
-
states.opened
-
states.half_open
local errors = moonbreaker.errors
error
is one of:
-
errors.open: string
-
errors.too_many_requests: string
Counters
is a table with the following properties:
-
requests: number
-
total_successes: number
-
total_failures: number
-
consecutive_successes: number
-
consecutive_failures: number
Settings
is a table with the properties listed in the
settings table.
Copyright © 2020 Arthur Khashaev. See license for details.