Circuit Breaker
@CircuitBreaker protects a route when repeated failures indicate that its dependency is unavailable.
import 'reflect-metadata';
import {CircuitBreaker} from '@heronjs/common';
import {Rest, Get} from '@heronjs/express';
@Rest('/availability')
export class AvailabilityController {
@Get({uri: '/'})
@CircuitBreaker<AvailabilityController>({threshold: 3, cooldown: 60000, fallback: 'unavailable'})
check() {
return {available: true};
}
unavailable() {
return {available: false};
}
}Register the controller in a module and configure a default cache store. Without a cache manager the HTTP circuit breaker is inactive. threshold sets the failure limit, cooldown is measured in milliseconds, and fallback names a method on the same controller. The HTTP adapter invokes the fallback without arguments; give it a compatible response shape and no required parameters.
The circuit opens after the failure threshold. While open, calls use the fallback; after the cooldown the adapter permits attempts in the half-open state. Successful responses close the circuit, while a failed half-open response reopens it. State is stored in the configured cache. An open circuit responds with HTTP 503 using the serialized fallback result.