pkgsite: golang.org/x/pkgsite/internal/breaker Index | Files

package breaker

import "golang.org/x/pkgsite/internal/breaker"

Package breaker implements the circuit breaker pattern. see https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn589784(v=pandp.10).

This package uses different terminologies for the state of the circuit breaker for better readability. Since it is unintuitive for users unfamiliar with circuits that a "closed" state means to "let the requests pass through" and "open" state means "don't let anything through", we use the colors red, yellow, and green instead of open, half-open, and closed, respectively.

When the API is stable, this package may be factored out to an external location.

Index

Package Files

breaker.go

type Breaker Uses

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

Breaker represents a circuit breaker.

In the green state, the breaker remains green until it encounters a time window of length GreenInterval where there are more than FailsToRed failures and a failureRatio of more than FailureThreshold, in which case the state becomes red.

In the red state, the breaker halts all requests and waits for a timeout period before shifting to the yellow state.

In the yellow state, the breaker allows the first SuccsToGreen requests. If any of these fail, the state reverts to red. Otherwise, the state becomes green again.

The timeout period is initially set to MinTimeout when the breaker shifts from green to yellow. By default, the timeout period is doubled each time the breaker fails to shift from the yellow state to the green state and is capped at MaxTimeout.

func New Uses

func New(config Config) (*Breaker, error)

New creates a Breaker with the given configuration.

func (*Breaker) Allow Uses

func (b *Breaker) Allow() bool

Allow reports whether an event may happen at time now. If Allow returns true, the user must then call Record to register whether the event succeeded.

func (*Breaker) Record Uses

func (b *Breaker) Record(success bool)

Record registers the success or failure of an event with the circuit breaker. Use this function after a call to Allow returned true.

func (*Breaker) State Uses

func (b *Breaker) State() State

State returns the state of the breaker.

type Config Uses

type Config struct {
    // FailsToRed is the number of failures to exceed before the breaker shifts
    // from green to red state.
    FailsToRed int
    // FailureThreshold is the failure ratio to exceed before the breaker
    // shifts from green to red state.
    FailureThreshold float64
    // GreenInterval is the length of the interval with which the breaker
    // checks for conditions to move from green to red state.
    GreenInterval time.Duration
    // MinTimeout is the minimum timeout period that the breaker stays in the
    // red state before moving to the yellow state.
    MinTimeout time.Duration
    // MaxTimeout is the maxmimum timeout period that the breaker stays in the
    // red state before moving to the yellow state.
    MaxTimeout time.Duration
    // SuccsToGreen is the number of successes required to shift from the
    // yellow state to the green state.
    SuccsToGreen int
}

Config holds the configuration values for a breaker.

type State Uses

type State int

State represents the current state the breaker is in.

const (
    // Red state means that requests should not be allowed.
    Red State = iota
    // Yellow state means that certain requests may proceed with caution.
    Yellow
    // Green state means that requests are allowed to pass.
    Green
)

The breaker can have three states: Red, Yellow, or Green.

func (State) String Uses

func (s State) String() string

String returns the string version of the state.

Package breaker imports 3 packages (graph) and is imported by 3 packages. Updated 2020-08-05. Refresh now. Tools for package owners.