Documentation
¶
Index ¶
- type CircuitBreaker
- func (c *CircuitBreaker) Do(run RunLogic, fallback FallbackLogic, dataIn interface{}) (interface{}, error)
- func (c *CircuitBreaker) DoC(ctx context.Context, run RunLogic, fallback FallbackLogic, dataIn interface{}) (interface{}, error)
- func (c *CircuitBreaker) FlushAll()
- func (c *CircuitBreaker) Go(run RunLogic, fallback FallbackLogic, dataIn interface{}) (interface{}, error)
- func (c *CircuitBreaker) GoC(ctx context.Context, run RunLogic, fallback FallbackLogic, dataIn interface{}) (interface{}, error)
- func (c *CircuitBreaker) Init() error
- func (c *CircuitBreaker) StartStatsdCollector(appName string, statsdIp string, statsdPort ...int) error
- func (c *CircuitBreaker) StartStreamHttpServer(port ...int) error
- func (c *CircuitBreaker) StopStreamHttpServer()
- func (c *CircuitBreaker) UpdateConfig()
- func (c *CircuitBreaker) UpdateLogger()
- type FallbackLogic
- type RunLogic
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.
IMPORTANT — Global-state constraints of afex/hystrix-go:
- Logger (SetLogger) is PROCESS-GLOBAL: the last Init()/UpdateLogger() call wins for ALL circuits in the process, not just this instance. (BL-2)
- FlushAll() purges ALL circuits in the process, not just this instance's command. There is no per-circuit flush in the upstream library. (BL-3)
- These are fundamental limitations of the package-level design in afex/hystrix-go. A per-instance solution requires replacing the library (P2-5).
Config Properties:
- Timeout = how long to wait for command to complete, in milliseconds, default = 1000
- MaxConcurrentRequests = how many commands of the same type can run at the same time, default = 10
- RequestVolumeThreshold = minimum number of requests needed before a circuit can be tripped due to health, default = 20
- SleepWindow = how long to wait after a circuit opens before testing for recovery, in milliseconds, default = 5000
- ErrorPercentThreshold = causes circuits to open once the rolling measure of errors exceeds this percent of requests, default = 50
- Logger = indicates the logger that will be used in the Hystrix package, default = logs nothing (GLOBAL — see above)
func (*CircuitBreaker) Do ¶
func (c *CircuitBreaker) Do(run RunLogic, fallback FallbackLogic, dataIn interface{}) (interface{}, error)
Do will execute synchronous with circuit breaker
Parameters:
- 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
- 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
- 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:
- ctx = required, defines the context in which this method is to be run under
- 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
- 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
- dataIn = optional, input parameter to run and fallback func, may be nil if not needed
func (*CircuitBreaker) FlushAll ¶
func (c *CircuitBreaker) FlushAll()
FlushAll purges ALL circuits and metrics from memory across the ENTIRE PROCESS, not just the circuits owned by this CircuitBreaker instance. This is a limitation of afex/hystrix-go's package-level circuit registry — there is no exported API to flush or remove a single named circuit. Callers should be aware that invoking this method on any CircuitBreaker instance will reset every circuit breaker in the process. A per-instance flush requires replacing hystrix-go (see P2-5).
func (*CircuitBreaker) Go ¶
func (c *CircuitBreaker) Go(run RunLogic, fallback FallbackLogic, dataIn interface{}) (interface{}, error)
Go will execute async with circuit breaker
Parameters:
- 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
- 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
- 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:
- ctx = required, defines the context in which this method is to be run under
- 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
- 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
- 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:
- appName = name of the app working with hystrixgo
- statsdIp = IP address of statsd service host
- 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) error
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 updates the PROCESS-GLOBAL hystrix-go logger based on the Logger field of this struct. WARNING: hystrix.SetLogger is package-level — the last caller wins for ALL circuits, not just this instance (see BL-2 / P2-5).