go-pattern-examples/resiliency/06_circuit_breaker/breaker_options.go

67 lines
1.4 KiB
Go
Raw Normal View History

2020-05-10 17:03:24 +03:00
package circuit
import "time"
//Options for breaker
type Options struct {
2020-05-11 09:31:25 +03:00
Name string
Expiry time.Time
Interval, Timeout time.Duration
MaxRequests uint32
ReadyToTrip StateCheckerHandler
OnStateChanged StateChangedEventHandler
2020-05-10 17:03:24 +03:00
}
//SetName of breaker
func SetName(name string) Option {
return func(opts *Options) {
opts.Name = name
}
}
2020-05-11 13:01:25 +03:00
//Interval of breaker
func Interval(interval time.Duration) Option {
return func(opts *Options) {
opts.Interval = interval
}
}
//Timeout of breaker
func Timeout(timeout time.Duration) Option {
return func(opts *Options) {
opts.Timeout = timeout
}
}
// MaxRequests is the maximum number of requests allowed to pass through
// when the CircuitBreaker is half-open.
// If MaxRequests is 0, the CircuitBreaker allows only 1 request.
//MaxRequests of breaker
func MaxRequests(maxRequests uint32) Option {
return func(opts *Options) {
opts.MaxRequests = maxRequests
}
}
//Expiry of breaker
func Expiry(expiry time.Time) Option {
2020-05-10 17:03:24 +03:00
return func(opts *Options) {
opts.Expiry = expiry
}
}
2020-05-11 13:01:25 +03:00
//OnStateChanged set handle of ChangedHandle
func OnStateChanged(handler StateChangedEventHandler) Option {
2020-05-10 17:03:24 +03:00
return func(opts *Options) {
opts.OnStateChanged = handler
}
}
2020-05-11 13:01:25 +03:00
//ReadyToTrip check traffic state ,to see if request can go
func ReadyToTrip(readyToGo StateCheckerHandler) Option {
2020-05-10 17:03:24 +03:00
return func(opts *Options) {
opts.ReadyToTrip = readyToGo
}
}