circuitbreaker

package
v0.0.0-...-fd23dd1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2019 License: OSL-3.0 Imports: 3 Imported by: 0

README

Circuit Breaker

The circuitbreaker package wraps an implementation of the Circuit Breaker pattern for usage within flamingo.

The used implementation is sony/gobreaker.

It can be used to wrap all kind of requests that are likely to fail.

Basic usage

To use the circuit breaker, you will have to wrap your request into a BreakableFunction, which is a func() (interface{}, error).

Then just create an instance of CircuitBreaker an pass the function to the Execute function. Execute runs the given request if the CircuitBreaker accepts it. It returns an error instantly if the CircuitBreaker rejects the request. Otherwise, it returns the result of the request. If a panic occurs in the request, the CircuitBreaker handles it as an error and causes the same panic again.

Settings

To create an instance of CircuitBreaker you will have to pass a Settings object:

  • Name is the name of the CircuitBreaker.
  • MaxRequests is the maximum number of requests allowed to pass through when the CircuitBreaker is half-open. If MaxRequests is 0, CircuitBreaker allows only 1 request.
  • Interval is the cyclic period of the closed state for CircuitBreaker to clear the internal Counts. If Interval is 0, CircuitBreaker doesn't clear the internal Counts during the closed state.
  • Timeout is the period of the open state, after which the state of CircuitBreaker becomes half-open. If Timeout is 0, the timeout value of CircuitBreaker is set to 60 seconds.
  • OnStateChange is called whenever the state of CircuitBreaker changes.
  • ReadyToTrip is called with a copy of Counts whenever a request fails in the closed state. If ReadyToTrip returns true, CircuitBreaker will be placed into the open state. If ReadyToTrip is nil, readyToTrip from this package is used.

If you pass a flamingo.Logger into the constructor, the CircuitBreaker will log each state change. Pass nil if you do not want this.

Example

s := circuitbreaker.Settings{
  Name:          "Test",
  MaxRequests:   0,
  Interval:      10 * time.Second,
  Timeout:       5 * time.Second,
  OnStateChange: nil,
  ReadyToTrip:   nil,
}
b := circuitbreaker.NewCircuitBreaker(s, logger)

for i := 0; i < 100; i++ {
  time.Sleep(time.Second)
  fmt.Println("Get Request", i)

  body, err := b.Execute(
    func()(interface{}, error) {
      resp, err := http.Get("http://localhost:8081")
      if err != nil {
        return nil, err
      }
  
      defer resp.Body.Close()
      body, err := ioutil.ReadAll(resp.Body)
      if err != nil {
        return nil, err
      }
  
      return body, nil
    },
  )

  if err != nil {
    fmt.Println("CB ERR:", err)
  } else {
    fmt.Println("CB BODY:", string(body.([]byte)))
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BreakableFunction

type BreakableFunction func() (interface{}, error)

BreakableFunction is a request which will be performed inside the CircuitBreaker

type CircuitBreaker

type CircuitBreaker interface {
	Execute(function BreakableFunction) (interface{}, error)
}

CircuitBreaker represents a state machine to prevent sending requests that are likely to fail.

func NewCircuitBreaker

func NewCircuitBreaker(settings Settings, logger flamingo.Logger) CircuitBreaker

NewCircuitBreaker returns a new CircuitBreaker configured with the given Settings.

type Settings

type Settings struct {
	Name          string
	MaxRequests   uint32
	Interval      time.Duration
	Timeout       time.Duration
	OnStateChange StateChangeFunction
	ReadyToTrip   func(counts gobreaker.Counts) bool
}

Settings is used to pass settings to the CircuitBreaker

Name is the name of the CircuitBreaker.

MaxRequests is the maximum number of requests allowed to pass through when the CircuitBreaker is half-open. If MaxRequests is 0, the CircuitBreaker allows only 1 request.

Interval is the cyclic period of the closed state for the CircuitBreaker to clear the internal Counts. If Interval is 0, the CircuitBreaker doesn't clear internal Counts during the closed state.

Timeout is the period of the open state, after which the state of the CircuitBreaker becomes half-open. If Timeout is 0, the timeout value of the CircuitBreaker is set to 60 seconds.

OnStateChange is called whenever the state of the CircuitBreaker changes.

ReadyToTrip is called with a copy of Counts whenever a request fails in the closed state. If ReadyToTrip returns true, the CircuitBreaker will be placed into the open state. If ReadyToTrip is nil, readyToTrip from this package is used.

type State

type State interface {
	String() string
}

State represents the current state of the CircuitBreaker

type StateChangeFunction

type StateChangeFunction func(cb CircuitBreaker, from, to State)

StateChangeFunction is called when the CircuitBreaker changes its state

Jump to

Keyboard shortcuts

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