Documentation ¶
Index ¶
- Variables
- func Do(name string, req func() error) error
- func DoWithAcceptable(name string, req func() error, acceptable Acceptable) error
- func DoWithFallback(name string, req func() error, fallback func(err error) error) error
- func DoWithFallbackAcceptable(name string, req func() error, fallback func(err error) error, ...) error
- func MinInt(a, b int) int
- func NoBreakerFor(name string)
- func Now() time.Duration
- func Pid() int
- func ProcessName() string
- func Rand() string
- func Randn(n int) string
- func Since(d time.Duration) time.Duration
- type Acceptable
- type AtomicDuration
- type BatchError
- type Breaker
- type Bucket
- type Option
- type Proba
- type Promise
- type ResourceManager
- type RollingWindow
- type RollingWindowOption
- type SingleFlight
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func DoWithAcceptable ¶ added in v3.4.4
func DoWithAcceptable(name string, req func() error, acceptable Acceptable) error
DoWithAcceptable calls Breaker.DoWithAcceptable on the Breaker with given name.
func DoWithFallback ¶ added in v3.4.4
DoWithFallback calls Breaker.DoWithFallback on the Breaker with given name.
func DoWithFallbackAcceptable ¶ added in v3.4.4
func DoWithFallbackAcceptable(name string, req func() error, fallback func(err error) error, acceptable Acceptable) error
DoWithFallbackAcceptable calls Breaker.DoWithFallbackAcceptable on the Breaker with given name.
func NoBreakerFor ¶ added in v3.4.4
func NoBreakerFor(name string)
NoBreakerFor disables the circuit breaker for the given name.
func ProcessName ¶ added in v3.4.4
func ProcessName() string
ProcessName returns the processname, same as the command name.
Types ¶
type Acceptable ¶ added in v3.4.4
type AtomicDuration ¶ added in v3.4.4
type AtomicDuration int64
An AtomicDuration is an implementation of atomic duration.
func ForAtomicDuration ¶ added in v3.4.4
func ForAtomicDuration(val time.Duration) *AtomicDuration
ForAtomicDuration returns an AtomicDuration with given value.
func NewAtomicDuration ¶ added in v3.4.4
func NewAtomicDuration() *AtomicDuration
NewAtomicDuration returns an AtomicDuration.
func (*AtomicDuration) CompareAndSwap ¶ added in v3.4.4
func (d *AtomicDuration) CompareAndSwap(old, val time.Duration) bool
CompareAndSwap compares current value with old, if equals, set the value to val.
func (*AtomicDuration) Load ¶ added in v3.4.4
func (d *AtomicDuration) Load() time.Duration
Load loads the current duration.
func (*AtomicDuration) Set ¶ added in v3.4.4
func (d *AtomicDuration) Set(val time.Duration)
Set sets the value to val.
type BatchError ¶
type BatchError struct {
// contains filtered or unexported fields
}
A BatchError is an error that can hold multiple errors.
func (*BatchError) Add ¶
func (be *BatchError) Add(errs ...error)
Add adds errs to be, nil errors are ignored.
func (*BatchError) Err ¶
func (be *BatchError) Err() error
Err returns an error that represents all errors.
type Breaker ¶ added in v3.4.4
type Breaker interface { // Name returns the name of the Breaker. Name() string // Allow checks if the request is allowed. // If allowed, a promise will be returned, the caller needs to call promise.Accept() // on success, or call promise.Reject() on failure. // If not allow, ErrServiceUnavailable will be returned. Allow() (Promise, error) // Do runs the given request if the Breaker accepts it. // Do returns an error instantly if the Breaker rejects the request. // If a panic occurs in the request, the Breaker handles it as an error // and causes the same panic again. Do(req func() error) error // DoWithAcceptable runs the given request if the Breaker accepts it. // DoWithAcceptable returns an error instantly if the Breaker rejects the request. // If a panic occurs in the request, the Breaker handles it as an error // and causes the same panic again. // acceptable checks if it's a successful call, even if the err is not nil. DoWithAcceptable(req func() error, acceptable Acceptable) error // DoWithFallback runs the given request if the Breaker accepts it. // DoWithFallback runs the fallback if the Breaker rejects the request. // If a panic occurs in the request, the Breaker handles it as an error // and causes the same panic again. DoWithFallback(req func() error, fallback func(err error) error) error // DoWithFallbackAcceptable runs the given request if the Breaker accepts it. // DoWithFallbackAcceptable runs the fallback if the Breaker rejects the request. // If a panic occurs in the request, the Breaker handles it as an error // and causes the same panic again. // acceptable checks if it's a successful call, even if the err is not nil. DoWithFallbackAcceptable(req func() error, fallback func(err error) error, acceptable Acceptable) error }
func GetBreaker ¶ added in v3.4.4
GetBreaker returns the Breaker with the given name.
func NewBreaker ¶ added in v3.4.4
NewBreaker returns a Breaker object. opts can be used to customize the Breaker.
type Option ¶ added in v3.4.4
type Option func(breaker *circuitBreaker)
Option defines the method to customize a Breaker.
type Proba ¶ added in v3.4.4
type Proba struct {
// contains filtered or unexported fields
}
func (*Proba) TrueOnProba ¶ added in v3.4.4
TrueOnProba checks if true on given probability.
type Promise ¶ added in v3.4.4
type Promise interface { // Accept tells the Breaker that the call is successful. Accept() // Reject tells the Breaker that the call is failed. Reject(reason string) }
Promise interface defines the callbacks that returned by Breaker.Allow.
type ResourceManager ¶
type ResourceManager struct {
// contains filtered or unexported fields
}
A ResourceManager is a manager that used to manage resources.
func NewResourceManager ¶
func NewResourceManager() *ResourceManager
NewResourceManager returns a ResourceManager.
func (*ResourceManager) Close ¶
func (manager *ResourceManager) Close() error
Close closes the manager. Don't use the ResourceManager after Close() called.
func (*ResourceManager) GetResource ¶
func (manager *ResourceManager) GetResource(key string, create func() (io.Closer, error)) (io.Closer, error)
GetResource returns the resource associated with given key.
type RollingWindow ¶ added in v3.4.4
type RollingWindow struct {
// contains filtered or unexported fields
}
RollingWindow defines a rolling window to calculate the events in buckets with time interval.
func NewRollingWindow ¶ added in v3.4.4
func NewRollingWindow(size int, interval time.Duration, opts ...RollingWindowOption) *RollingWindow
NewRollingWindow returns a RollingWindow that with size buckets and time interval, use opts to customize the RollingWindow.
func (*RollingWindow) Add ¶ added in v3.4.4
func (rw *RollingWindow) Add(v float64)
Add adds value to current bucket.
func (*RollingWindow) Reduce ¶ added in v3.4.4
func (rw *RollingWindow) Reduce(fn func(b *Bucket))
Reduce runs fn on all buckets, ignore current bucket if ignoreCurrent was set.
type RollingWindowOption ¶ added in v3.4.4
type RollingWindowOption func(rollingWindow *RollingWindow)
RollingWindowOption let callers customize the RollingWindow.
type SingleFlight ¶
type SingleFlight interface { Do(key string, fn func() (any, error)) (any, error) DoEx(key string, fn func() (any, error)) (any, bool, error) }
SingleFlight lets the concurrent calls with the same key to share the call result. For example, A called F, before it's done, B called F. Then B would not execute F, and shared the result returned by F which called by A. The calls with the same key are dependent, concurrent calls share the returned values. A ------->calls F with key<------------------->returns val B --------------------->calls F with key------>returns val