circuitbreaker

package module
v0.0.0-...-32933d9 Latest Latest
Warning

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

Go to latest
Published: May 12, 2022 License: MIT Imports: 6 Imported by: 0

README

circuit-breaker

This package provides circuit breaker. You can see the concept here.

Usage

import (
	"testing"
	"time"

	"github.com/wreulicke/circuitbreaker"
)

func TestExample(t *testing.T) {
	// initialize with default options
	c := circuitbreaker.New(
		circuitbreaker.WithResetTimeout(60*time.Second),
		circuitbreaker.WithFailureRate(0.5),
		circuitbreaker.WithNumberOfCallsInHalfState(5),
		circuitbreaker.WithIsIgnorable(circuitbreaker.DefaultIsIgnorable),
		circuitbreaker.WithIsSuccessful(circuitbreaker.DefaultIsSuccessful),
	)

	circuitbreaker.GuardBy(c, func() (string, error) {
		return "", nil
	})
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOpen = errors.New("circuit breaker is open")
)

Functions

func DefaultIsIgnorable

func DefaultIsIgnorable(err error) bool

func DefaultIsSuccessful

func DefaultIsSuccessful(err error) bool

func GuardBy

func GuardBy[T any](cb *CircuitBreaker, f func() (T, error)) (r T, err error)

func Ignore

func Ignore(err error) error

func Success

func Success(err error) error

func WithClock

func WithClock(clock clock.Clock) option

func WithFailureRate

func WithFailureRate(p float32) option

func WithHook

func WithHook(h Hook) option

func WithIsIgnorable

func WithIsIgnorable(f func(error) bool) option

func WithIsSuccessful

func WithIsSuccessful(f func(error) bool) option

func WithNumberOfCallsInHalfState

func WithNumberOfCallsInHalfState(n int32) option

func WithResetTimeout

func WithResetTimeout(d time.Duration) option

Types

type CircuitBreaker

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

func New

func New(opts ...option) *CircuitBreaker

type CircuitBreakerState

type CircuitBreakerState string
const (
	StateOpen     CircuitBreakerState = "open"
	StateClosed   CircuitBreakerState = "closed"
	StateHalfOpen CircuitBreakerState = "half-open"
)

type Hook

type Hook func(old, new CircuitBreakerState)

type IgnorableError

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

func (*IgnorableError) Error

func (e *IgnorableError) Error() string

func (*IgnorableError) Unwrap

func (e *IgnorableError) Unwrap() error

type SuccessfulError

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

func (*SuccessfulError) Error

func (e *SuccessfulError) Error() string

func (*SuccessfulError) Unwrap

func (e *SuccessfulError) Unwrap() error

Jump to

Keyboard shortcuts

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