cr-fusebox
cr-fusebox
A declarative circuit breaker shard for Crystal. Wrap instance methods with failure thresholds, sliding windows, cooldown recovery, fallbacks, and lifecycle hooks.
Installation
Add it to your shard.yml:
dependencies:
cr-fusebox:
github: Gwash3189/cr-fusebox
version: ~> 0.2.0
Then run:
shards install
And require it:
require "cr-fusebox"
Usage
Include Fusebox in your class or struct and annotate instance methods with @[Fusebox::Breaker]:
class Payments
include Fusebox
@[Fusebox::Breaker(
failures: 5,
within: 10.seconds,
cooldown: 30.seconds,
fallback: :charge_fallback,
on_open: :circuit_opened,
on_half_open: :circuit_half_opened,
on_close: :circuit_closed,
)]
def charge(amount : Int32) : String
# call the downstream payment service
end
def charge_fallback(error : Exception) : String
"cached"
end
def circuit_opened(error : Exception)
# alert, log, or emit a metric
end
def circuit_half_opened
# a probe is about to run
end
def circuit_closed
# the downstream service recovered
end
end
You can also use a bare @[Fusebox::Breaker] annotation to apply the default configuration (failures: 5, cooldown: 30.seconds, default key).
Note:
include Fuseboxis required on the enclosing type. Without it,@[Fusebox::Breaker]annotations are not processed.
Options
| Option | Default | Description |
|---|---|---|
failures |
5 |
Number of failures required to open the circuit. Must be at least 1. |
within |
nil |
Optional Time::Span sliding window. When omitted, failures must be consecutive. |
cooldown |
30.seconds |
How long the circuit stays open before allowing a half-open probe. |
fallback |
none | Method called when the work fails or the circuit is open. |
on_open |
none | Called when the circuit transitions to open. |
on_half_open |
none | Called when a half-open probe starts. |
on_close |
none | Called when a successful probe closes the circuit. |
on_failure |
none | Called for every failed protected call. |
on_reject |
none | Called when a call is rejected because the circuit is open. |
key |
"ClassName#method_name" |
Circuit registry key. |
Circuit states
- Closed — calls pass through. Failures are tracked within
within, or consecutively whenwithinis omitted. - Open — calls fail fast with
Fusebox::CircuitOpenErrorwithout invoking the wrapped method. If afallbackis configured, its value is returned instead. - Half-open — after
cooldownelapses, exactly one probe call is allowed. A successful probe closes the circuit. A failed probe immediately reopens it. Concurrent calls while the probe is in flight are rejected.
Handlers and fallbacks
Error-oriented handlers (on_open, on_failure, on_reject) may be defined with zero arguments or with one Exception argument:
def circuit_opened(error : Exception)
end
# or
def circuit_opened
end
on_half_open and on_close may be defined with zero arguments or with the circuit key:
def circuit_closed(key : String)
end
# or
def circuit_closed
end
A fallback may receive the error or take no arguments:
def charge_fallback(error : Exception)
"cached"
end
# or
def charge_fallback
"cached"
end
Handler exceptions are rescued so lifecycle hooks cannot break the protected call path. Fallback exceptions are not rescued.
Sharing and custom keys
By default, one circuit is shared by every instance of a class for a given method, keyed by "ClassName#method_name":
Fusebox.state("Payments#charge")
Use key: when a different scope is needed:
@[Fusebox::Breaker(key: "stripe-api", failures: 5)]
def charge(amount : Int32) : String
# ...
end
# Per-tenant circuits are also possible because key is evaluated per call:
@[Fusebox::Breaker(key: "tenant-#{@tenant_id}", failures: 5)]
def charge(amount : Int32) : String
# ...
end
Inspecting and resetting circuits
state = Fusebox.state("Payments#charge")
state.closed?
state.open?
state.half_open?
Fusebox.reset("Payments#charge")
Fusebox.reset_all
Supported method shapes
The wrapper preserves the original method signature and forwards calls through previous_def, including:
- overloaded methods (with independent per-overload
@[Fusebox::Breaker]options) - default and named arguments
- splat and double-splat arguments (including typed double splats)
- generic free variables (
forall T) - explicit return types
yield-based blocks- typed captured blocks
- private and protected methods
- method names such as
valid?
Limitations
- Only instance methods can be wrapped (annotating class methods or
initializeis rejected at compile time). - The enclosing type must
include Fuseboxfor@[Fusebox::Breaker]annotations to take effect. - Duplicate
@[Fusebox::Breaker]annotations on the same method are rejected at compile time. - Abstract methods are rejected at compile time.
- A captured block parameter must have a type restriction. Use an anonymous
&block oryieldwhen the block is not captured. breakandnextinside a typed captured block follow Crystal's normal captured-block rules. Preferyield-based methods when those semantics matter.
Development
shards install
crystal spec
crystal tool format --check
To generate a coverage report on Linux, install kcov and run:
./bin/coverage
License
MIT
cr-fusebox
- 0
- 0
- 0
- 0
- 0
- about 1 hour ago
- November 3, 2026
MIT License
Sun, 29 Nov 2026 05:25:27 GMT