breaker

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package breaker implements the Circuit Breaker design pattern.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrOpenState is the error returned when the Breaker is open.
	ErrOpenState = errors.New("breaker: circuit breaker is open")

	// ErrTooManyRequests is the error returned when too many test requests are
	// made when the Breaker is half-open.
	ErrTooManyRequests = errors.New("breaker: too many requests")
)

Functions

This section is empty.

Types

type Breaker

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

Breaker is a circuit breaker.

Example
package main

import (
	"time"

	"github.com/hamba/pkg/breaker"
)

func main() {
	b := breaker.NewBreaker(breaker.ThresholdFuse(1), breaker.WithSleep(100*time.Millisecond))

	err := b.Run(func() error {
		// Your code protected by the circuit breaker...

		return nil // Return any errors
	})
	if err != nil {
		// Handle the error
	}

}

func NewBreaker

func NewBreaker(f Fuse, opts ...OptFunc) *Breaker

NewBreaker creates a new Breaker.

func (*Breaker) Run

func (b *Breaker) Run(req func() error) error

Run runs the given request if the Breaker allows it.

Run returns an error immediately if the Breaker rejects the request, otherwise it returns the result of the request.

func (*Breaker) State

func (b *Breaker) State() State

State returns the state of the Breaker.

type Counter

type Counter struct {
	// Requests is the total number of requests made.
	Requests uint64
	// Successes is the total number of successes returned.
	Successes uint64
	// Failures is the total number of failures returned.
	Failures uint64
	// ConsecutiveSuccesses is the number of consecutive successes returned.
	ConsecutiveSuccesses uint64
	// ConsecutiveFailures is the number of consecutive failures returned.
	ConsecutiveFailures uint64
}

Counter holds the number number of requests, successes and failures of a breaker.

Counter is reset from time to time, and totals should not be used as a full totals.

type Fuse

type Fuse interface {
	// Trip determines if the Breaker should be tripped.
	Trip(Counter) bool
}

Fuse represents a Breaker fuse used to trip the breaker.

type FuseFunc

type FuseFunc func(Counter) bool

FuseFunc is an adapter allowing to use a function as a Fuse.

func ConsecutiveFuse

func ConsecutiveFuse(count uint64) FuseFunc

ConsecutiveFuse trips the Breaker when the consecutive number of failures exceeds the given count.

func RateFuse

func RateFuse(rate uint64) FuseFunc

RateFuse trips the Breaker when the percentage of failures exceeds the given rate.

rate should be between 0 and 100.

func ThresholdFuse

func ThresholdFuse(count uint64) FuseFunc

ThresholdFuse trips the Breaker when the total number of failures exceeds the given count.

func (FuseFunc) Trip

func (f FuseFunc) Trip(c Counter) bool

Trip determines if the Breaker should be tripped.

type OptFunc

type OptFunc func(b *Breaker)

OptFunc represents a configuration function for Breaker.

func WithSleep

func WithSleep(d time.Duration) OptFunc

WithSleep sets the time the Breaker stays open for.

func WithTestRequests

func WithTestRequests(c uint64) OptFunc

WithTestRequests sets the number of test requests allowed when the Breaker is half-open.

type State

type State int8

State represents a Breakers state.

const (
	StateClosed State = iota
	StateHalfOpen
	StateOpen
)

State constants for the Breaker.

Jump to

Keyboard shortcuts

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