breaker

package
v0.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package breaker provides a per-tier circuit breaker with a sliding-window failure counter, hand-rolled with no external dependencies.

States:

CLOSED     — normal operation; Allow() returns true.
OPEN       — tripped; Allow() returns false until cooldown elapses.
HALF-OPEN  — cooldown elapsed; exactly ONE probe is allowed through; a
             successful Record closes the breaker, a failed one re-opens it.

The sliding window tracks the last windowSize outcomes (bool ring buffer). The breaker trips to OPEN when failures within that window reach failureThreshold.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Breaker

type Breaker struct {
	// contains filtered or unexported fields
}

Breaker is a single-tier circuit breaker.

func New

func New(failureThreshold, windowSize int, cooldown time.Duration) *Breaker

New creates a Breaker.

  • failureThreshold: number of failures in the sliding window that trips OPEN.
  • windowSize: size of the outcome ring buffer.
  • cooldown: how long the breaker stays OPEN before transitioning to HALF-OPEN.

func (*Breaker) Allow

func (b *Breaker) Allow() bool

Allow returns true if a call may proceed. While OPEN it returns false; in HALF-OPEN exactly one probe is allowed.

func (*Breaker) Record

func (b *Breaker) Record(success bool)

Record registers the outcome of a call. success=false counts as a trip-eligible infra failure.

func (*Breaker) State

func (b *Breaker) State() string

State returns "closed", "open", or "half-open".

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group manages a set of Breakers keyed by tier name.

func NewGroup

func NewGroup(failureThreshold, windowSize int, cooldown time.Duration) *Group

NewGroup creates a Group; all per-tier Breakers share the same configuration.

func (*Group) Allow

func (g *Group) Allow(tier string) bool

Allow delegates to the per-tier Breaker.

func (*Group) Record

func (g *Group) Record(tier string, success bool)

Record delegates to the per-tier Breaker.

func (*Group) State

func (g *Group) State(tier string) string

State delegates to the per-tier Breaker.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL