circuitbreaker

package module
v0.0.0-...-c1a0ab7 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2021 License: MIT Imports: 2 Imported by: 0

README

Circuit Breaker tool

Implementation of the Circuit Breaker pattern. Implemented using the sync/atomic package.

Circuit Breaker scheme

Features:

  • Use Interval to set the interval after which it will clear counters. Default store all counters of requests (successful, throtlled, etc.).
  • Use Timeout to set the interval after which it will switch to the HalfClosed state. Default 1 second.
  • Use Treshold to set threshold value for consecutive errors. Default 1.
  • Use MaxRequests to set value for max request number in state HalfClosed.

Example

cb := circuitbreaker.New(circuitbreaker.Settings{
    Timeout:     2 * time.Second,
    Threshold:   2,
    MaxRequests: 2,
})

for i := 0; i < 10; i++ {
    if !cb.Allow() {
        continue
    }
    if i % 2 == 0 {
        cb.RegisterError()
    } else {
        cb.RegisterOK()
    }
}

Documentation

Index

Constants

View Source
const (
	OPEN uint32 = iota
	CLOSED
	HALF_CLOSED
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CircuitBreaker

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

func New

func New(settings Settings) *CircuitBreaker

func (*CircuitBreaker) Allow

func (cb *CircuitBreaker) Allow() bool

Allow returns true if request allowed or false if not. Use it in your code.

func (*CircuitBreaker) Counters

func (cb *CircuitBreaker) Counters() Counters

Counters return Counters object with different counters.

func (*CircuitBreaker) RegisterError

func (cb *CircuitBreaker) RegisterError()

func (*CircuitBreaker) RegisterOK

func (cb *CircuitBreaker) RegisterOK()

type Counters

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

type Settings

type Settings struct {
	Interval    time.Duration // Interval after which all counters will be reset if state closed. Default disabled.
	Timeout     time.Duration // Timeout is time after which CircuitBreaker will enter the half-closed state. Default 1 second.
	Threshold   int           // Threshold is the value after which CircuitBreaker will switch to the Open state. Default 1.
	MaxRequests int           // MaxRequests is the number of requests that will be sent in the Half Open state. Default 1.
}

Settings use it for create new CircuitBreaker object.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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