breaker

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package breaker is a circuit breaker persisted in a state file, so short lived processes (hooks, CLI invocations) share one view of it.

Policy (same as the ralph bash/python transports):

  • two consecutive failures open the breaker;
  • HTTP 401/422 (config errors) open it immediately via Open;
  • success resets the consecutive counter, but only while closed;
  • once open it stays open until Reset.

Every mutation holds an exclusive file lock across read-modify-write and replaces the state file by atomic rename, so concurrent processes never lose an update and readers never see a torn file. Persistence is best effort: I/O errors never fail the caller; an unreadable file reads closed.

Index

Constants

View Source
const (

	// FailureThreshold is the consecutive-failure count that opens the breaker.
	FailureThreshold = 2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Breaker

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

Breaker is a file-backed circuit breaker. It satisfies jev.Breaker.

func New

func New(stateDir string) *Breaker

New returns a Breaker whose files live in <stateDir>/jevkit.

func (*Breaker) IsOpen

func (b *Breaker) IsOpen() bool

IsOpen reports whether the breaker is open.

func (*Breaker) Load

func (b *Breaker) Load() State

Load returns the current state; a missing or corrupt file reads as closed.

func (*Breaker) Open

func (b *Breaker) Open(reason string)

Open force-opens the breaker. It is a no-op when already open.

func (*Breaker) Path

func (b *Breaker) Path() string

Path is the state file location.

func (*Breaker) RecordFailure

func (b *Breaker) RecordFailure(reason string)

RecordFailure counts a failure and opens after FailureThreshold in a row. It is silent once open.

func (*Breaker) RecordSuccess

func (b *Breaker) RecordSuccess()

RecordSuccess resets the failure counter. It does not close an open breaker.

func (*Breaker) Reset

func (b *Breaker) Reset()

Reset closes the breaker and clears the counter.

type State

type State struct {
	Open        bool      `json:"open"`
	Consecutive int       `json:"consecutive"`
	Reason      string    `json:"reason,omitempty"`
	OpenedAt    time.Time `json:"opened_at,omitzero"`
}

State is the persisted breaker record.

Jump to

Keyboard shortcuts

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