Documentation ¶
Overview ¶
Package hystrix is a Go implementation of Netflix's Hystrix logic for circuit breakers. It creates openers and closers for the circuit that behave the same as Netflix's Hystrix Java implementation. The easiest way to learn how to use this package is to look at the examples, especially ExampleFactory.
Index ¶
- func CloserFactory(config ConfigureCloser) func() circuit.OpenToClosed
- func OpenerFactory(config ConfigureOpener) func() circuit.ClosedToOpen
- type Closer
- func (s *Closer) Allow(_ context.Context, now time.Time) (shouldAllow bool)
- func (s *Closer) Closed(_ context.Context, now time.Time)
- func (s *Closer) Config() ConfigureCloser
- func (s *Closer) ErrBadRequest(_ context.Context, _ time.Time, _ time.Duration)
- func (s *Closer) ErrConcurrencyLimitReject(_ context.Context, _ time.Time)
- func (s *Closer) ErrFailure(_ context.Context, _ time.Time, _ time.Duration)
- func (s *Closer) ErrInterrupt(_ context.Context, _ time.Time, _ time.Duration)
- func (s *Closer) ErrShortCircuit(_ context.Context, _ time.Time)
- func (s *Closer) ErrTimeout(_ context.Context, _ time.Time, _ time.Duration)
- func (s *Closer) MarshalJSON() ([]byte, error)
- func (s *Closer) Opened(_ context.Context, now time.Time)
- func (s *Closer) SetConfigNotThreadSafe(config ConfigureCloser)
- func (s *Closer) SetConfigThreadSafe(config ConfigureCloser)
- func (s *Closer) ShouldClose(_ context.Context, _ time.Time) bool
- func (s *Closer) Success(_ context.Context, _ time.Time, _ time.Duration)
- type ConfigureCloser
- type ConfigureOpener
- type Factory
- type Opener
- func (e *Opener) Closed(_ context.Context, now time.Time)
- func (e *Opener) Config() ConfigureOpener
- func (e *Opener) ErrBadRequest(_ context.Context, _ time.Time, _ time.Duration)
- func (e *Opener) ErrConcurrencyLimitReject(_ context.Context, _ time.Time)
- func (e *Opener) ErrFailure(_ context.Context, now time.Time, _ time.Duration)
- func (e *Opener) ErrInterrupt(_ context.Context, _ time.Time, _ time.Duration)
- func (e *Opener) ErrShortCircuit(_ context.Context, _ time.Time)
- func (e *Opener) ErrTimeout(_ context.Context, now time.Time, _ time.Duration)
- func (e *Opener) MarshalJSON() ([]byte, error)
- func (e *Opener) Opened(_ context.Context, now time.Time)
- func (e *Opener) Prevent(_ context.Context, _ time.Time) (shouldAllow bool)
- func (e *Opener) SetConfigNotThreadSafe(props ConfigureOpener)
- func (e *Opener) SetConfigThreadSafe(props ConfigureOpener)
- func (e *Opener) ShouldOpen(_ context.Context, now time.Time) bool
- func (e *Opener) Success(_ context.Context, now time.Time, _ time.Duration)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloserFactory ¶
func CloserFactory(config ConfigureCloser) func() circuit.OpenToClosed
CloserFactory creates Closer closer
func OpenerFactory ¶
func OpenerFactory(config ConfigureOpener) func() circuit.ClosedToOpen
OpenerFactory creates a err % opener
Types ¶
type Closer ¶
type Closer struct {
// contains filtered or unexported fields
}
Closer is hystrix's default half-open logic: try again ever X ms
func (*Closer) Allow ¶
Allow checks for half open state. The circuit is currently closed. Check and return true if this request should be allowed. This will signal the circuit in a "half-open" state, allowing that one request. If any requests are allowed, the circuit moves into a half open state.
func (*Closer) Config ¶
func (s *Closer) Config() ConfigureCloser
Config returns the current configuration. Use SetConfigThreadSafe to modify the current configuration.
func (*Closer) ErrBadRequest ¶
ErrBadRequest is ignored
func (*Closer) ErrConcurrencyLimitReject ¶
ErrConcurrencyLimitReject is ignored
func (*Closer) ErrFailure ¶
ErrFailure resets the consecutive Successful count
func (*Closer) ErrInterrupt ¶
ErrInterrupt is ignored
func (*Closer) ErrShortCircuit ¶
ErrShortCircuit is ignored
func (*Closer) ErrTimeout ¶
ErrTimeout resets the consecutive Successful count
func (*Closer) MarshalJSON ¶
MarshalJSON returns closer information in a JSON format
func (*Closer) Opened ¶
Opened circuit. It should now check to see if it should ever allow various requests in an attempt to become closed
func (*Closer) SetConfigNotThreadSafe ¶
func (s *Closer) SetConfigNotThreadSafe(config ConfigureCloser)
SetConfigNotThreadSafe just calls SetConfigThreadSafe. It is not safe to call while the circuit is active.
func (*Closer) SetConfigThreadSafe ¶
func (s *Closer) SetConfigThreadSafe(config ConfigureCloser)
SetConfigThreadSafe resets the sleep duration during reopen attempts
Example ¶
Most configuration properties on [the Hystrix Configuration page](https://github.com/Netflix/Hystrix/wiki/Configuration) that say they are modifyable at runtime can be changed on the Circuit in a thread safe way. Most of the ones that cannot are related to stat collection.
This example shows how to update hystrix configuration at runtime.
// Start off using the defaults configuration := hystrix.Factory{} h := circuit.Manager{ // Tell the manager to use this configuration factory whenever it makes a new circuit DefaultCircuitProperties: []circuit.CommandPropertiesConstructor{configuration.Configure}, } c := h.MustCreateCircuit("hystrix-circuit") fmt.Println("The default sleep window", c.OpenToClose.(*hystrix.Closer).Config().SleepWindow) // This configuration update function is thread safe. We can modify this at runtime while the circuit is active c.OpenToClose.(*hystrix.Closer).SetConfigThreadSafe(hystrix.ConfigureCloser{ SleepWindow: time.Second * 3, }) fmt.Println("The new sleep window", c.OpenToClose.(*hystrix.Closer).Config().SleepWindow)
Output: The default sleep window 5s The new sleep window 3s
func (*Closer) ShouldClose ¶
ShouldClose is true if we have enough successful attempts in a row.
type ConfigureCloser ¶
type ConfigureCloser struct { // AfterFunc should simulate time.AfterFunc AfterFunc func(time.Duration, func()) *time.Timer `json:"-"` // SleepWindow is https://github.com/Netflix/Hystrix/wiki/Configuration#circuitbreakersleepwindowinmilliseconds SleepWindow time.Duration // HalfOpenAttempts is how many attempts to allow per SleepWindow HalfOpenAttempts int64 // RequiredConcurrentSuccessful is how may consecutive passing requests are required before the circuit is closed RequiredConcurrentSuccessful int64 }
ConfigureCloser configures values for Closer
func (*ConfigureCloser) Merge ¶
func (c *ConfigureCloser) Merge(other ConfigureCloser)
Merge this configuration with another
type ConfigureOpener ¶
type ConfigureOpener struct { // ErrorThresholdPercentage is https://github.com/Netflix/Hystrix/wiki/Configuration#circuitbreakererrorthresholdpercentage ErrorThresholdPercentage int64 // RequestVolumeThreshold is https://github.com/Netflix/Hystrix/wiki/Configuration#circuitbreakerrequestvolumethreshold RequestVolumeThreshold int64 // Now should simulate time.Now Now func() time.Time `json:"-"` // RollingDuration is https://github.com/Netflix/Hystrix/wiki/Configuration#metricsrollingstatstimeinmilliseconds RollingDuration time.Duration // NumBuckets is https://github.com/Netflix/Hystrix/wiki/Configuration#metricsrollingstatsnumbuckets NumBuckets int }
ConfigureOpener configures Opener
func (*ConfigureOpener) Merge ¶
func (c *ConfigureOpener) Merge(other ConfigureOpener)
Merge this configuration with another
type Factory ¶
type Factory struct { ConfigureCloser ConfigureCloser ConfigureOpener ConfigureOpener CreateConfigureCloser []func(circuitName string) ConfigureCloser CreateConfigureOpener []func(circuitName string) ConfigureOpener }
Factory aids making hystrix circuit logic
Example ¶
This example configures the circuit to use Hystrix open/close logic with the default Hystrix parameters
configuration := hystrix.Factory{ // Hystrix open logic is to open the circuit after an % of errors ConfigureOpener: hystrix.ConfigureOpener{ // We change the default to wait for 10 requests, not 20, before checking to close RequestVolumeThreshold: 10, // The default values match what hystrix does by default }, // Hystrix close logic is to sleep then check ConfigureCloser: hystrix.ConfigureCloser{ // The default values match what hystrix does by default }, } h := circuit.Manager{ // Tell the manager to use this configuration factory whenever it makes a new circuit DefaultCircuitProperties: []circuit.CommandPropertiesConstructor{configuration.Configure}, } // This circuit will inherit the configuration from the example c := h.MustCreateCircuit("hystrix-circuit") fmt.Println("This is a hystrix configured circuit", c.Name())
Output: This is a hystrix configured circuit hystrix-circuit
type Opener ¶
type Opener struct {
// contains filtered or unexported fields
}
Opener is ClosedToOpen that opens a circuit after a threshold and % error has been reached. It is the default hystrix implementation.
func (*Opener) Config ¶
func (e *Opener) Config() ConfigureOpener
Config returns the current configuration. To update configuration, please call SetConfigThreadSafe or SetConfigNotThreadSafe
func (*Opener) ErrBadRequest ¶
ErrBadRequest is ignored
func (*Opener) ErrConcurrencyLimitReject ¶
ErrConcurrencyLimitReject is ignored
func (*Opener) ErrFailure ¶
ErrFailure increases error count for the circuit
func (*Opener) ErrInterrupt ¶
ErrInterrupt is ignored
func (*Opener) ErrShortCircuit ¶
ErrShortCircuit is ignored
func (*Opener) ErrTimeout ¶
ErrTimeout increases error count for the circuit
func (*Opener) MarshalJSON ¶
MarshalJSON returns opener information in a JSON format
func (*Opener) SetConfigNotThreadSafe ¶
func (e *Opener) SetConfigNotThreadSafe(props ConfigureOpener)
SetConfigNotThreadSafe recreates the buckets. It is not safe to call while the circuit is active.
func (*Opener) SetConfigThreadSafe ¶
func (e *Opener) SetConfigThreadSafe(props ConfigureOpener)
SetConfigThreadSafe modifies error % and request volume threshold
func (*Opener) ShouldOpen ¶
ShouldOpen returns true if rolling count >= threshold and error % is high enough.