Documentation ¶
Overview ¶
A circuit breaker
Circuitry is a circuit breaker similar to Hystrix:
w, _ := NewWindow(10, 10*time.Second) circuit := circuitry.NewBreaker(15, 100, time.Minute, w) if circuit.Allow() { err := DangerousStuff() circuit.Error(err) }
Or via a Command:
type Namer struct { Name string } func (n *Namer) Run() (interface{}, error) { rand.Seed(time.Now().UnixNano()) if rand.Intn(4) >= 2 { return nil, fmt.Errorf("can't assign name: %s", n.Name) } return fmt.Sprintf("Your name is %s.", n.Name), nil } func (n *Namer) Fallback() interface{} { return fmt.Sprintf("Hello, %s.", n.Name) } func main() { cmd := &Namer{"Hal"} w, _ := circuitry.NewWindow(10, 10*time.Second) circuit := circuitry.NewBreaker(40, 4, time.Minute, w) value := circuitry.Execute(cmd, circuit) fmt.Println(value) }
Index ¶
- Variables
- func Execute(c Command, b *CircuitBreaker) interface{}
- type CircuitBreaker
- func (b *CircuitBreaker) Allow() bool
- func (b *CircuitBreaker) Close()
- func (b *CircuitBreaker) Error(err error)
- func (b *CircuitBreaker) ForceClose()
- func (b *CircuitBreaker) ForceOpen()
- func (b *CircuitBreaker) IsClosed() bool
- func (b *CircuitBreaker) IsForced() bool
- func (b *CircuitBreaker) IsOpen() bool
- func (b *CircuitBreaker) MarkFailure()
- func (b *CircuitBreaker) MarkShortCircuited()
- func (b *CircuitBreaker) MarkSuccess()
- func (b *CircuitBreaker) Open()
- type Command
- type Window
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrBucketSize = errors.New("bucket duration and size must divide equally")
Functions ¶
func Execute ¶
func Execute(c Command, b *CircuitBreaker) interface{}
Execute the given command against the given circuit breaker.
Example ¶
b := NewBreaker(40, 0, time.Minute, window()) v := Execute(&alwaysSucceed{}, b) fmt.Println(v)
Output:
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker represents a circuit breaker.
func NewBreaker ¶
func NewBreaker(errors, volume int64, timeout time.Duration, w *Window) *CircuitBreaker
Create a new circuit breaker.
Example ¶
w, _ := NewWindow(10, 10*time.Second) circuit := NewBreaker(40, 4, time.Minute, w) if circuit.Allow() { circuit.Error(fmt.Errorf("forced error: %s", "example")) }
Output:
func (*CircuitBreaker) Allow ¶
func (b *CircuitBreaker) Allow() bool
Allow requests to proceed or not.
func (*CircuitBreaker) Error ¶
func (b *CircuitBreaker) Error(err error)
Pass error to the to the circuit breaker.
func (*CircuitBreaker) IsClosed ¶
func (b *CircuitBreaker) IsClosed() bool
Reports if the circuit is closed.
func (*CircuitBreaker) IsForced ¶
func (b *CircuitBreaker) IsForced() bool
Reports if the circuit is forced.
func (*CircuitBreaker) IsOpen ¶
func (b *CircuitBreaker) IsOpen() bool
Reports if the circuit is open.
func (*CircuitBreaker) MarkShortCircuited ¶
func (b *CircuitBreaker) MarkShortCircuited()
Record a rejection.
func (*CircuitBreaker) MarkSuccess ¶
func (b *CircuitBreaker) MarkSuccess()
Record a successful operation.
type Command ¶
type Command interface { Run() (interface{}, error) Fallback() interface{} }
Command represents a piece of code you want to run and a fallback value.
type Window ¶
type Window struct {
// contains filtered or unexported fields
}
func NewWindow ¶
Create a new window of duration d containing n buckets.
Example ¶
window, _ := NewWindow(10, 10*time.Second) window.Reset()
Output:
func (*Window) ShortCircuited ¶
Return the total of short-circuit.
Click to show internal directories.
Click to hide internal directories.