hystrix

package
v4.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: Apache-2.0 Imports: 6 Imported by: 1

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

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

func (s *Closer) Allow(_ context.Context, now time.Time) (shouldAllow bool)

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) Closed

func (s *Closer) Closed(_ context.Context, now time.Time)

Closed circuit. It can turn off now.

func (*Closer) Config

func (s *Closer) Config() ConfigureCloser

Config returns the current configuration. Use SetConfigThreadSafe to modify the current configuration.

func (*Closer) ErrBadRequest

func (s *Closer) ErrBadRequest(_ context.Context, _ time.Time, _ time.Duration)

ErrBadRequest is ignored

func (*Closer) ErrConcurrencyLimitReject

func (s *Closer) ErrConcurrencyLimitReject(_ context.Context, _ time.Time)

ErrConcurrencyLimitReject is ignored

func (*Closer) ErrFailure

func (s *Closer) ErrFailure(_ context.Context, _ time.Time, _ time.Duration)

ErrFailure resets the consecutive Successful count

func (*Closer) ErrInterrupt

func (s *Closer) ErrInterrupt(_ context.Context, _ time.Time, _ time.Duration)

ErrInterrupt is ignored

func (*Closer) ErrShortCircuit

func (s *Closer) ErrShortCircuit(_ context.Context, _ time.Time)

ErrShortCircuit is ignored

func (*Closer) ErrTimeout

func (s *Closer) ErrTimeout(_ context.Context, _ time.Time, _ time.Duration)

ErrTimeout resets the consecutive Successful count

func (*Closer) MarshalJSON

func (s *Closer) MarshalJSON() ([]byte, error)

MarshalJSON returns closer information in a JSON format

func (*Closer) Opened

func (s *Closer) Opened(_ context.Context, now time.Time)

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

func (s *Closer) ShouldClose(_ context.Context, _ time.Time) bool

ShouldClose is true if we have enough successful attempts in a row.

func (*Closer) Success

func (s *Closer) Success(_ context.Context, _ time.Time, _ time.Duration)

Success any time runFunc was called and appeared healthy

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

func (*Factory) Configure

func (c *Factory) Configure(circuitName string) circuit.Config

Configure creates a circuit configuration constructor that uses hystrix open/close logic

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) Closed

func (e *Opener) Closed(_ context.Context, now time.Time)

Closed resets the error and attempt count

func (*Opener) Config

func (e *Opener) Config() ConfigureOpener

Config returns the current configuration. To update configuration, please call SetConfigThreadSafe or SetConfigNotThreadSafe

func (*Opener) ErrBadRequest

func (e *Opener) ErrBadRequest(_ context.Context, _ time.Time, _ time.Duration)

ErrBadRequest is ignored

func (*Opener) ErrConcurrencyLimitReject

func (e *Opener) ErrConcurrencyLimitReject(_ context.Context, _ time.Time)

ErrConcurrencyLimitReject is ignored

func (*Opener) ErrFailure

func (e *Opener) ErrFailure(_ context.Context, now time.Time, _ time.Duration)

ErrFailure increases error count for the circuit

func (*Opener) ErrInterrupt

func (e *Opener) ErrInterrupt(_ context.Context, _ time.Time, _ time.Duration)

ErrInterrupt is ignored

func (*Opener) ErrShortCircuit

func (e *Opener) ErrShortCircuit(_ context.Context, _ time.Time)

ErrShortCircuit is ignored

func (*Opener) ErrTimeout

func (e *Opener) ErrTimeout(_ context.Context, now time.Time, _ time.Duration)

ErrTimeout increases error count for the circuit

func (*Opener) MarshalJSON

func (e *Opener) MarshalJSON() ([]byte, error)

MarshalJSON returns opener information in a JSON format

func (*Opener) Opened

func (e *Opener) Opened(_ context.Context, now time.Time)

Opened resets the error and attempt count

func (*Opener) Prevent

func (e *Opener) Prevent(_ context.Context, _ time.Time) (shouldAllow bool)

Prevent never returns true

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

func (e *Opener) ShouldOpen(_ context.Context, now time.Time) bool

ShouldOpen returns true if rolling count >= threshold and error % is high enough.

func (*Opener) Success

func (e *Opener) Success(_ context.Context, now time.Time, _ time.Duration)

Success increases the number of correct attempts

Jump to

Keyboard shortcuts

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