When your product depends on a third-party odds feed that goes down twice a month, you need a circuit breaker β not just a retry loop. Retries without a breaker turn a 30-second outage into a 5-minute request queue that cascades into your own API.
Three states
A circuit breaker sits between your service and the upstream:
CLOSED β failures exceed threshold β OPEN OPEN β cool-down expires β HALF-OPEN HALF-OPEN β probe succeeds β CLOSED HALF-OPEN β probe fails β OPEN
Enter fullscreen mode
Exit fullscreen mode
class Breaker { private failures = 0 ; private state : 'closed' | 'open' | 'half' = 'closed' ; private openedAt = 0 ;
async call < T > ( fn : ( ) => Promise < T > ) : Promise < T > { if ( this . state === 'open' ) { if ( Date . now ( ) - this . openedAt < 30_000 ) throw new Error ( 'circuit open' ) ; this . state = 'half' ; } try { const result = await fn ( ) ; this . failures = 0 ; this . state = 'closed' ; return result ; } catch ( e ) { if ( ++ this . failures >= 5 ) { this .β¦