circuit

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package circuit implements the Circuit Breaker pattern for http client. It will wrap a http request and monitor for failures and/or time outs. When a threshold of failures or time outs has been reached, future requests will not run. During this state, the breaker will allow limited number of http requests to run and, if they are successful, will start performing all http requests again.

Index

Constants

View Source
const (
	DefaultGoBreakerName        = "GoBreaker"
	DefaultGoBreakerMaxRequests = uint32(50)
	DefaultGoBreakerInterval    = 2 * time.Minute
	DefaultGoBreakerTimeout     = 1 * time.Minute
)

Variables

This section is empty.

Functions

func DefaultReadyToTrip

func DefaultReadyToTrip(counts gobreaker.Counts) bool

Types

type Breaker

type Breaker interface {
	Execute(req func() error) error
}

Breaker defines contract for circuit breakers to implement

type ClientOption

type ClientOption func(*HTTPClient)

func WithCircuitBreaker

func WithCircuitBreaker(cb Breaker) ClientOption

func WithHTTPRequestDoer

func WithHTTPRequestDoer(client HTTPRequestDoer) ClientOption

type GoBreaker

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

func NewGoBreaker

func NewGoBreaker(opts ...GoBreakerOption) *GoBreaker

func (*GoBreaker) Execute

func (gb *GoBreaker) Execute(req func() error) error

type GoBreakerOnStateChangeFunc

type GoBreakerOnStateChangeFunc func(name string, from gobreaker.State, to gobreaker.State)

type GoBreakerOption

type GoBreakerOption func(*GoBreaker)

func WithGoBreakerInterval

func WithGoBreakerInterval(interval time.Duration) GoBreakerOption

WithGoBreakerInterval sets the cyclic period of the closed state for CircuitBreaker to clear the internal Counts, described in gobreaker documentation. If Interval is 0, CircuitBreaker doesn't clear the internal Counts during the closed state. Default value is 2 minutes.

func WithGoBreakerMaxRequests

func WithGoBreakerMaxRequests(maxRequests uint32) GoBreakerOption

WithGoBreakerMaxRequests sets the maximum number of requests allowed to pass through when the CircuitBreaker is half-open. If MaxRequests is 0, CircuitBreaker allows only 1 request. Default value is 50 requests.

func WithGoBreakerName

func WithGoBreakerName(name string) GoBreakerOption

WithGoBreakerName sets the name of the CircuitBreaker. Default value is "GoBreaker".

func WithGoBreakerOnStateChange

func WithGoBreakerOnStateChange(onStateChange GoBreakerOnStateChangeFunc) GoBreakerOption

WithGoBreakerOnStateChange sets the function that is called whenever the state of CircuitBreaker changes.

func WithGoBreakerReadyToTrip

func WithGoBreakerReadyToTrip(readyToTrip GoBreakerReadyToTripFunc) GoBreakerOption

WithGoBreakerReadyToTrip sets the function that is called with a copy of Counts whenever a request fails in the closed state. If ReadyToTrip returns true, CircuitBreaker will be placed into the open state. If ReadyToTrip is nil, default ReadyToTrip is used. Default ReadyToTrip returns true when number of requests more than 100 and the percent of failures is more than 50 percents.

func WithGoBreakerTimeout

func WithGoBreakerTimeout(timeout time.Duration) GoBreakerOption

WithGoBreakerTimeout sets the period of the open state, after which the state of CircuitBreaker becomes half-open. If Timeout is 0, the timeout value of CircuitBreaker is set to 60 seconds. Default value is 1 minute.

type GoBreakerReadyToTripFunc

type GoBreakerReadyToTripFunc func(counts gobreaker.Counts) bool

type HTTPClient

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

func NewHTTPClient

func NewHTTPClient(opts ...ClientOption) *HTTPClient

func (*HTTPClient) Do

func (hc *HTTPClient) Do(req *http.Request) (response *http.Response, err error)

type HTTPRequestDoer

type HTTPRequestDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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