breaker

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2015 License: MIT Imports: 4 Imported by: 0

README

circuit-breaker

Build Status GoDoc Code of Conduct

The circuit-breaker resiliency pattern for golang.

Creating a breaker takes three parameters:

  • error threshold (for opening the breaker)
  • success threshold (for closing the breaker)
  • timeout (how long to keep the breaker open)
b := breaker.New(3, 1, 5*time.Second)

for {
	result := b.Run(func() error {
		// communicate with some external service and
		// return an error if the communication failed
		return nil
	})

	switch result {
	case nil:
		// success!
	case breaker.ErrBreakerOpen:
		// our function wasn't run because the breaker was open
	default:
		// some other error
	}
}

Documentation

Overview

Package breaker implements the circuit-breaker resiliency pattern for Go.

Index

Examples

Constants

This section is empty.

Variables

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

ErrBreakerOpen is the error returned from Run() when the function is not executed because the breaker is currently open.

Functions

This section is empty.

Types

type Breaker

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

Breaker implements the circuit-breaker resiliency pattern

Example
breaker := New(3, 1, 5*time.Second)

for {
	result := breaker.Run(func() error {
		// communicate with some external service and
		// return an error if the communication failed
		return nil
	})

	switch result {
	case nil:
		// success!
	case ErrBreakerOpen:
		// our function wasn't run because the breaker was open
	default:
		// some other error
	}
}
Output:

func New

func New(errorThreshold, successThreshold int, timeout time.Duration) *Breaker

New constructs a new circuit-breaker that starts closed. From closed, the breaker opens if "errorThreshold" errors are seen without an error-free period of at least "timeout". From open, the breaker half-closes after "timeout". From half-open, the breaker closes after "successThreshold" consecutive successes, or opens on a single error.

func (*Breaker) Go

func (b *Breaker) Go(work func() error) error

Go will either return ErrBreakerOpen immediately if the circuit-breaker is already open, or it will run the given function in a separate goroutine. If the function is run, Go will return nil immediately, and will *not* return the return value of the function. It is safe to call Go concurrently on the same Breaker.

func (*Breaker) Run

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

Run will either return ErrBreakerOpen immediately if the circuit-breaker is already open, or it will run the given function and pass along its return value. It is safe to call Run concurrently on the same Breaker.

Jump to

Keyboard shortcuts

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