circuitbreaker

package
v0.0.0-...-396d841 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	TimeoutInterval:     10 * time.Second,
	MaxFailures:         5,
	OpenToHalfOpenWait:  30 * time.Second,
	HalfOpenMaxSuccess:  5,
	HalfOpenMaxFailures: 3,
	RetryIntervals:      []time.Duration{1 * time.Second, 2 * time.Second, 3 * time.Second, 5 * time.Second, 8 * time.Second},
}

Functions

This section is empty.

Types

type CircuitBreaker

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

func NewCircuitBreaker

func NewCircuitBreaker(config Config, name string, rdb RedisClient) *CircuitBreaker

func (*CircuitBreaker) DoRequest

func (cb *CircuitBreaker) DoRequest(req *http.Request) *CircuitBreakerResponse

func (*CircuitBreaker) SetFallbackFunc

func (cb *CircuitBreaker) SetFallbackFunc(f func(*http.Request) *CircuitBreakerResponse)

type CircuitBreakerResponse

type CircuitBreakerResponse struct {
	HttpStatus   int          `json:"http-status"`
	ResponseType ResponseType `json:"response-type"`
	Data         interface{}  `json:"data,omitempty"`
	Error        *ErrorDetail `json:"error,omitempty"`
	Raw          interface{}  `json:"raw,omitempty"`
}

type Config

type Config struct {
	TimeoutInterval     time.Duration
	MaxFailures         int
	OpenToHalfOpenWait  time.Duration
	HalfOpenMaxSuccess  int
	HalfOpenMaxFailures int
	RetryIntervals      []time.Duration
}

type ErrorDetail

type ErrorDetail struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
	Raw     error  `json:"-"`
}

type RedisClient

type RedisClient interface {
	Get(ctx context.Context, key string) *redis.StringCmd
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
}

type ResponseType

type ResponseType string
const (
	Success  ResponseType = "success"
	Fallback ResponseType = "fallback"
	Error    ResponseType = "error"
)

type State

type State string
const (
	Closed   State = "CLOSED"
	Open     State = "OPEN"
	HalfOpen State = "HALF OPEN"
)

Jump to

Keyboard shortcuts

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