breakerpool

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: Apache-2.0, Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package breakerpool provides a generic circuit breaker pool with priority-based failover.

Index

Constants

View Source
const DefaultPriority = 100

DefaultPriority is the default priority for entries without an explicit priority.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllUnavailableError

type AllUnavailableError struct {
	LastError error
}

AllUnavailableError indicates all endpoints are unavailable.

func (*AllUnavailableError) Error

func (e *AllUnavailableError) Error() string

func (*AllUnavailableError) Unwrap

func (e *AllUnavailableError) Unwrap() error

type Entry

type Entry[S any] struct {
	// State is passed to the Execute callback. This is the only required field.
	State S

	// Priority determines failover order. Higher values are tried first.
	// If zero, DefaultPriority (100) is used.
	Priority int

	// Settings overrides the pool's default gobreaker settings for this entry.
	// If nil, the pool's default settings are used.
	Settings *gobreaker.Settings
}

Entry associates state with optional per-entry circuit breaker settings.

type Pool

type Pool[T, S any] struct {
	// contains filtered or unexported fields
}

Pool manages circuit breakers with priority-based failover. When executing operations, it tries entries in priority order (highest first), using round-robin within the same priority tier. If a breaker is open or trips, the pool tries the next available entry.

func New

func New[T, S any](entries []Entry[S], defaults gobreaker.Settings) *Pool[T, S]

New creates a new Pool with the given entries and default circuit breaker settings. For each entry:

  • Uses entry.Settings if non-nil, otherwise uses defaults
  • Uses entry.Priority if non-zero, otherwise uses DefaultPriority

func (*Pool[T, S]) AllUnavailable

func (p *Pool[T, S]) AllUnavailable() bool

AllUnavailable returns true if all circuit breakers are in the open state.

func (*Pool[T, S]) Execute

func (p *Pool[T, S]) Execute(fn func(S) (T, error)) (T, error)

Execute runs the given function against entries in priority order. The function receives the entry's State and should perform the operation. If the operation fails with an error that trips the circuit breaker, Execute tries the next available entry. Returns AllUnavailableError if all entries are unavailable.

func (*Pool[T, S]) Len

func (p *Pool[T, S]) Len() int

Len returns the number of entries in the pool.

Jump to

Keyboard shortcuts

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