circuitbreaker

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2020 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Circuit Breaker

The circuit breaker prevents the calling of a service when that service has failed in the past. How the circuit breaker is tripped depends on the mode of operation. There are four modes of operation: contiguous errors, errors within a time period, contiguous errors within a time period, and smart circuit breaker mode.

The available service settings are as follows:

Name Type Description
mode string The tripping mode: 'a' for contiguous errors, 'b' for errors within a time period, 'c' for contiguous errors within a time period, and 'd' for a probabilistic smart circuit breaker mode. Defaults to mode 'a'
threshold number The number of errors required for tripping. Defaults to 5 errors
period number Number of seconds in which errors have to occur for the circuit breaker to trip. Applies to modes 'b' and 'c'. Defaults to 60 seconds
timeout number Number of seconds that the circuit breaker will remain tripped. Applies to modes 'a', 'b', 'c'. Defaults to 60 seconds

The available input for the request are as follows:

Name Type Description
operation string An operation to perform: '' for protecting a service, 'counter' for processing errors, and 'reset' for processing non-errors. Defaults to ''

The available response outputs are as follows:

Name Type Description
tripped boolean The state of the circuit breaker

A sample service definition is:

{
  "name": "CircuitBreaker",
  "description": "Circuit breaker service",
  "ref": "github.com/project-flogo/microgateway/activity/circuitbreaker",
  "settings": {
    "mode": "a"
  }
}

An example series of step that invokes the above CircuitBreaker service:

{
  "service": "CircuitBreaker"
},
{
  "service": "PetStorePets",
  "input": {
    "method": "GET"
  },
  "halt": "($.PetStorePets.error != nil) && !error.isneterror($.PetStorePets.error)"
},
{
  "if": "$.PetStorePets.error != nil",
  "service": "CircuitBreaker",
  "input": {
    "operation": "counter"
  }
},
{
  "if": "$.PetStorePets.error == nil",
  "service": "CircuitBreaker",
  "input": {
    "operation": "reset"
  }
}

Utilizing the response values can be seen in a response handler:

{
  "if": "$.CircuitBreaker.outputs.tripped == true",
  "error": true,
  "output": {
    "code": 403,
    "data": {
      "error": "circuit breaker tripped"
    }
  }
}

Documentation

Index

Constants

View Source
const (
	// CircuitBreakerModeA triggers the circuit breaker when there are contiguous errors
	CircuitBreakerModeA = "a"
	// CircuitBreakerModeB triggers the circuit breaker when there are errors over time
	CircuitBreakerModeB = "b"
	// CircuitBreakerModeC triggers the circuit breaker when there are contiguous errors over time
	CircuitBreakerModeC = "c"
	// CircuitBreakerModeD is a probabilistic smart circuit breaker
	CircuitBreakerModeD = "d"
	// CircuitBreakerFailure is a failure
	CircuitBreakerFailure = -1.0
	// CircuitBreakerUnknown is an onknown status
	CircuitBreakerUnknown = 0.0
	// CircuitBreakerSuccess is a success
	CircuitBreakerSuccess = 1.0
)

Variables

View Source
var (
	// ErrorCircuitBreakerTripped happens when the circuit breaker has tripped
	ErrorCircuitBreakerTripped = errors.New("circuit breaker tripped")

	Now = time.Now
)

Functions

func New

Types

type Activity

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

func (*Activity) Eval

func (a *Activity) Eval(ctx activity.Context) (done bool, err error)

Eval executes the activity

func (*Activity) Metadata

func (a *Activity) Metadata() *activity.Metadata

Metadata return the metadata for the activity

type Context

type Context struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

CircuitBreakerContext is a circuit breaker context

func (*Context) AddRecord

func (c *Context) AddRecord(weight float64, now time.Time)

func (*Context) Probability

func (c *Context) Probability(now time.Time) float64

Probability computes the probability for mode d

func (*Context) Trip

func (c *Context) Trip(now time.Time, timeout time.Duration)

Trip trips the circuit breaker

type Input

type Input struct {
	Operation string `md:"operation,allowed(counter,reset)"`
}

func (*Input) FromMap

func (r *Input) FromMap(values map[string]interface{}) error

func (*Input) ToMap

func (r *Input) ToMap() map[string]interface{}

type Output

type Output struct {
	Tripped bool `md:"tripped"`
}

func (*Output) FromMap

func (o *Output) FromMap(values map[string]interface{}) error

func (*Output) ToMap

func (o *Output) ToMap() map[string]interface{}

type Record

type Record struct {
	Weight float64
	Stamp  time.Time
}

Record is a record of a request

type Settings

type Settings struct {
	Mode      string `md:"mode,allowed(a,b,c,d)"`
	Threshold int    `md:"threshold"`
	Period    int    `md:"period"`
	Timeout   int    `md:"timeout"`
}

Directories

Path Synopsis
api

Jump to

Keyboard shortcuts

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