hystrixgo

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2020 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CircuitBreaker

type CircuitBreaker struct {
	// circuit breaker command name for this instance
	CommandName string

	// config fields
	TimeOut                int
	MaxConcurrentRequests  int
	RequestVolumeThreshold int
	SleepWindow            int
	ErrorPercentThreshold  int

	// config logger
	Logger *data.ZapLog

	// config to disable circuit breaker temporarily
	DisableCircuitBreaker bool
	// contains filtered or unexported fields
}

CircuitBreaker defines one specific circuit breaker by command name

Config Properties:

  1. Timeout = how long to wait for command to complete, in milliseconds, default = 1000
  2. MaxConcurrentRequests = how many commands of the same type can run at the same time, default = 10
  3. RequestVolumeThreshold = minimum number of requests needed before a circuit can be tripped due to health, default = 20
  4. SleepWindow = how long to wait after a circuit opens before testing for recovery, in milliseconds, default = 5000
  5. ErrorPercentThreshold = causes circuits to open once the rolling measure of errors exceeds this percent of requests, default = 50
  6. Logger = indicates the logger that will be used in the Hystrix package, default = logs nothing

func (*CircuitBreaker) Do

func (c *CircuitBreaker) Do(run RunLogic, fallback FallbackLogic, dataIn interface{}) (interface{}, error)

Do will execute synchronous with circuit breaker

Parameters:

  1. run = required, defines either inline or external function to be executed, it is meant for a self contained function and accepts no parameter, returns error
  2. fallback = optional, defines either inline or external function to be executed as fallback when run fails, it is meat for a self contained function and accepts only error parameter, returns error, set to nil if fallback is not specified
  3. dataIn = optional, input parameter to run and fallback func, may be nil if not needed

func (*CircuitBreaker) DoC

func (c *CircuitBreaker) DoC(ctx context.Context, run RunLogic, fallback FallbackLogic, dataIn interface{}) (interface{}, error)

DoC will execute synchronous with circuit breaker in given context

Parameters:

  1. ctx = required, defines the context in which this method is to be run under
  2. run = required, defines either inline or external function to be executed, it is meant for a self contained function and accepts context.Context parameter, returns error
  3. fallback = optional, defines either inline or external function to be executed as fallback when run fails, it is meant for a self contained function and accepts context.Context and error parameters, returns error, set to nil if fallback is not specified
  4. dataIn = optional, input parameter to run and fallback func, may be nil if not needed

func (*CircuitBreaker) FlushAll

func (c *CircuitBreaker) FlushAll()

FlushAll will purge all circuits and metrics from memory

func (*CircuitBreaker) Go

func (c *CircuitBreaker) Go(run RunLogic,
	fallback FallbackLogic,
	dataIn interface{}) (interface{}, error)

Go will execute async with circuit breaker

Parameters:

  1. run = required, defines either inline or external function to be executed, it is meant for a self contained function and accepts no parameter, returns error
  2. fallback = optional, defines either inline or external function to be executed as fallback when run fails, it is meat for a self contained function and accepts only error parameter, returns error, set to nil if fallback is not specified
  3. dataIn = optional, input parameter to run and fallback func, may be nil if not needed

func (*CircuitBreaker) GoC

func (c *CircuitBreaker) GoC(ctx context.Context,
	run RunLogic,
	fallback FallbackLogic,
	dataIn interface{}) (interface{}, error)

GoC will execute async with circuit breaker in given context

Parameters:

  1. ctx = required, defines the context in which this method is to be run under
  2. run = required, defines either inline or external function to be executed, it is meant for a self contained function and accepts context.Context parameter, returns error
  3. fallback = optional, defines either inline or external function to be executed as fallback when run fails, it is meat for a self contained function and accepts context.Context and error parameters, returns error, set to nil if fallback is not specified
  4. dataIn = optional, input parameter to run and fallback func, may be nil if not needed

func (*CircuitBreaker) Init

func (c *CircuitBreaker) Init() error

Init will initialize the circuit break with the given command name, a command name represents a specific service or api method that has circuit breaker being applied

func (*CircuitBreaker) StartStatsdCollector

func (c *CircuitBreaker) StartStatsdCollector(appName string, statsdIp string, statsdPort ...int) error

StartStatsdCollector will register and initiate the hystrixgo package for metric collection into statsd, each action from hystrixgo will be tracked into metrics and pushed into statsd via udp

Parameters:

  1. appName = name of the app working with hystrixgo
  2. statsdIp = IP address of statsd service host
  3. statsdPort = Port of statsd service host

statsd and graphite is a service that needs to be running on a host, either localhost or another reachable host in linux, the easiest way to deploy statsd and graphite is via docker image:

see full docker install info at = https://github.com/graphite-project/docker-graphite-statsd

docker command to run statsd and graphite as a unit as follows:

docker run -d --name graphite --restart=always -p 80:80 -p 2003-2004:2003-2004 -p 2023-2024:2023-2024 -p 8125:8125/udp -p 8126:8126 graphiteapp/graphite-statsd

once docker image is running, to view graphite:

http://localhost/dashboard

func (*CircuitBreaker) StartStreamHttpServer

func (c *CircuitBreaker) StartStreamHttpServer(port ...int)

StartStreamHttpServer will start a simple HTTP server on local host with given port, this will launch in goroutine, and return immediately

This method call is on entire hystrixgo package, not just the current circuit breaker struct

To view stream data, launch browser, point to http://localhost:port

default port = 81

func (*CircuitBreaker) StopStreamHttpServer

func (c *CircuitBreaker) StopStreamHttpServer()

StopStreamHttpServer will stop the currently running stream server if already started

func (*CircuitBreaker) UpdateConfig

func (c *CircuitBreaker) UpdateConfig()

UpdateConfig will update the hystrixgo command config data to the current value in struct for a given command name

func (*CircuitBreaker) UpdateLogger

func (c *CircuitBreaker) UpdateLogger()

UpdateLogger will udpate the hystrixgo package wide logger, based on the Logger set in the struct field

type FallbackLogic

type FallbackLogic func(dataIn interface{}, errIn error, ctx ...context.Context) (dataOut interface{}, err error)

FallbackLogic declares func alias for internal Fallback logic handler

type RunLogic

type RunLogic func(dataIn interface{}, ctx ...context.Context) (dataOut interface{}, err error)

RunLogic declares func alias for internal Run logic handler

Jump to

Keyboard shortcuts

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