breaker

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package breaker is a small circuit breaker for dependencies that fail persistently (MCP servers, model APIs): after a run of failures calls are refused for a cooldown that grows with each failed trial.

Index

Constants

View Source
const (
	InitialCooldown = 15 * time.Second
	MaxCooldown     = 5 * time.Minute
)

How long an open breaker refuses calls: InitialCooldown after the first trip, doubling on each failed trial up to MaxCooldown.

Variables

This section is empty.

Functions

This section is empty.

Types

type Breaker

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

Breaker opens after Threshold consecutive failures: calls are refused immediately for a cooldown that doubles on each failed trial (from InitialCooldown up to MaxCooldown). When the cooldown ends, one caller is let through as a trial; success closes the breaker, failure reopens it. It is safe for concurrent use.

func New

func New(threshold int) *Breaker

New returns a closed breaker that opens after threshold consecutive failures (minimum 1).

func (*Breaker) Abandon

func (b *Breaker) Abandon()

Abandon releases a call Allow let through without recording an outcome (it was cancelled), so a half-open breaker can admit another trial.

func (*Breaker) Allow

func (b *Breaker) Allow() (ok bool, retryIn time.Duration)

Allow reports whether a call may proceed; if not, retryIn says when the next trial is due.

func (*Breaker) Failure

func (b *Breaker) Failure() (streak int, opened bool, cooldown time.Duration)

Failure records a failed call. streak is the number of consecutive failures; opened is true when this failure opened (or reopened) the breaker, with the cooldown that now applies.

func (*Breaker) Failures

func (b *Breaker) Failures() int

Failures returns the current run of consecutive failures.

func (*Breaker) SetClock

func (b *Breaker) SetClock(now func() time.Time)

SetClock replaces the time source (tests).

func (*Breaker) Success

func (b *Breaker) Success() (recovered bool)

Success records a successful call; recovered is true when it closed an open breaker.

Jump to

Keyboard shortcuts

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