resource

package
v3.7.20 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrServiceUnavailable = errors.New("circuit breaker is open")

Functions

func Do added in v3.4.4

func Do(name string, req func() error) error

Do calls Breaker.Do on the Breaker with given name.

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

func DoWithFallback(name string, req func() error, fallback func(err error) error) error

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 MinInt added in v3.4.4

func MinInt(a, b int) int

MinInt returns the smaller one of a and b.

func NoBreakerFor added in v3.4.4

func NoBreakerFor(name string)

NoBreakerFor disables the circuit breaker for the given name.

func Now added in v3.4.4

func Now() time.Duration

func Pid added in v3.4.4

func Pid() int

Pid returns pid of current process.

func ProcessName added in v3.4.4

func ProcessName() string

ProcessName returns the processname, same as the command name.

func Rand added in v3.4.4

func Rand() string

Rand returns a random string.

func Randn added in v3.4.4

func Randn(n int) string

Randn returns a random string with length n.

func Since added in v3.4.4

func Since(d time.Duration) time.Duration

Since returns a diff since given d.

Types

type Acceptable added in v3.4.4

type Acceptable func(err error) bool

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.

func (*BatchError) NotNil

func (be *BatchError) NotNil() bool

NotNil checks if any error inside.

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

func GetBreaker(name string) Breaker

GetBreaker returns the Breaker with the given name.

func NewBreaker added in v3.4.4

func NewBreaker(opts ...Option) Breaker

NewBreaker returns a Breaker object. opts can be used to customize the Breaker.

type Bucket added in v3.4.4

type Bucket struct {
	Sum   float64
	Count int64
}

type Option added in v3.4.4

type Option func(breaker *circuitBreaker)

Option defines the method to customize a Breaker.

func WithName added in v3.4.4

func WithName(name string) Option

WithName returns a function to set the name of a Breaker.

type Proba added in v3.4.4

type Proba struct {
	// contains filtered or unexported fields
}

func NewProba added in v3.4.4

func NewProba() *Proba

NewProba returns a Proba.

func (*Proba) TrueOnProba added in v3.4.4

func (p *Proba) TrueOnProba(proba float64) (truth bool)

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.

func (*ResourceManager) Inject

func (manager *ResourceManager) Inject(key string, resource io.Closer)

Inject injects 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

func NewSingleFlight

func NewSingleFlight() SingleFlight

NewSingleFlight returns a SingleFlight.

Jump to

Keyboard shortcuts

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