Skip to content

Latest commit

 

History

History
232 lines (142 loc) · 5.73 KB

breaker.md

File metadata and controls

232 lines (142 loc) · 5.73 KB

breaker

import "github.com/andy2046/gopie/pkg/breaker"

Package breaker implements Circuit Breaker pattern.

breaker.go

var (
    // ErrTooManyRequests is returned when the state is half open
    // and the requests count is more the maxRequests.
    ErrTooManyRequests = errors.New("too many requests, requests count is more the maxRequests in half open state")
    // ErrOpenState is returned when the state is open.
    ErrOpenState = errors.New("circuit breaker is open")
)
var DefaultSettings = Settings{
    Name:        "CircuitBreaker",
    MaxRequests: 1,
    Interval:    0,
    Timeout:     60 * time.Second,
    ShouldTrip: func(counts Counts) bool {
        return counts.ConsecutiveFailures > 5
    },
    OnStateChange: nil,
}

DefaultSettings is the default CircuitBreaker Settings.

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

CircuitBreaker prevent an application repeatedly trying to execute an operation that is likely to fail.

func New(options ...Option) *CircuitBreaker

New returns a new CircuitBreaker with options applied.

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(request func() (interface{}, error)) (interface{}, error)

Execute runs the given request if the CircuitBreaker accepts it. Execute returns an error instantly if the CircuitBreaker rejects the request. Otherwise, Execute returns the result of the request. If a panic occurs in the request, the CircuitBreaker handles it as an error and causes the same panic again.

func (*CircuitBreaker) Name

func (cb *CircuitBreaker) Name() string

Name returns the name of the CircuitBreaker.

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() State

State returns the current state of the CircuitBreaker.

type Counts struct {
    Requests             uint64
    TotalSuccesses       uint64
    TotalFailures        uint64
    ConsecutiveSuccesses uint64
    ConsecutiveFailures  uint64
}

Counts holds the numbers of requests and their successes/failures. CircuitBreaker clears the internal Counts either on the change of the state or at the closed-state intervals. Counts ignores the results of the requests sent before clearing.

type Option = func(*Settings) error

Option applies settings to CircuitBreaker Settings.

type Settings struct {
    // Name is the name of the CircuitBreaker.
    Name string
    // MaxRequests is the maximum number of requests allowed to pass through
    // when the CircuitBreaker is half-open.
    // If MaxRequests is 0, the CircuitBreaker allows only 1 request.
    MaxRequests uint64
    // Interval is the cyclic period of the closed state
    // for the CircuitBreaker to clear the internal Counts.
    // If Interval is 0, the CircuitBreaker doesn't clear internal Counts during the closed state.
    Interval time.Duration
    // Timeout is the period of the open state,
    // after which the state of the CircuitBreaker becomes half-open.
    // If Timeout is 0, the timeout for the CircuitBreaker is 60 seconds.
    Timeout time.Duration
    // ShouldTrip is called with a copy of Counts whenever a request fails in the closed state.
    // If ShouldTrip returns true, the CircuitBreaker will be placed into the open state.
    // If ShouldTrip is nil, default ShouldTrip is used.
    // Default ShouldTrip returns true when the number of consecutive failures is more than 5.
    ShouldTrip func(counts Counts) bool
    // OnStateChange is called whenever the state of the CircuitBreaker changes.
    OnStateChange func(name string, from, to State)
}

Settings represents settings for CircuitBreaker.

type State int

State is the type representing a state of CircuitBreaker.

const (
    // StateClosed represents Closed State.
    StateClosed State = iota
    // StateHalfOpen represents HalfOpen State.
    StateHalfOpen
    // StateOpen represents Open State.
    StateOpen
)

func (State) String

func (s State) String() string

String implements stringer interface.


Generated by godoc2md