Documentation ¶
Index ¶
- Variables
- func Guard(lock sync.Locker, fn func())
- func Once(fn func()) func()
- type AtomicBool
- type AtomicDuration
- type AtomicFloat64
- type Barrier
- type Cond
- type DoneChan
- type ImmutableResource
- type ImmutableResourceOption
- type Limit
- type LockedCalls
- type ManagedResource
- type OnceGuard
- type Pool
- type PoolOption
- type RefResource
- type ResourceManager
- type SharedCalls
- type SingleFlight
- type SpinLock
- type TimeoutLimit
Constants ¶
This section is empty.
Variables ¶
var ErrLimitReturn = errors.New("discarding limited token, resource pool is full, someone returned multiple times")
ErrLimitReturn indicates that the more than borrowed elements were returned.
var ErrTimeout = errors.New("borrow timeout")
ErrTimeout is an error that indicates the borrow timeout.
var ErrUseOfCleaned = errors.New("using a cleaned resource")
ErrUseOfCleaned is an error that indicates using a cleaned resource.
Functions ¶
Types ¶
type AtomicBool ¶
type AtomicBool uint32
An AtomicBool is an atomic implementation for boolean values.
func ForAtomicBool ¶
func ForAtomicBool(val bool) *AtomicBool
ForAtomicBool returns an AtomicBool with given val.
func (*AtomicBool) CompareAndSwap ¶
func (b *AtomicBool) CompareAndSwap(old, val bool) bool
CompareAndSwap compares current value with given old, if equals, set to given val.
func (*AtomicBool) True ¶
func (b *AtomicBool) True() bool
True returns true if current value is true.
type AtomicDuration ¶
type AtomicDuration int64
An AtomicDuration is an implementation of atomic duration.
func ForAtomicDuration ¶
func ForAtomicDuration(val time.Duration) *AtomicDuration
ForAtomicDuration returns an AtomicDuration with given value.
func NewAtomicDuration ¶
func NewAtomicDuration() *AtomicDuration
NewAtomicDuration returns an AtomicDuration.
func (*AtomicDuration) CompareAndSwap ¶
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 ¶
func (d *AtomicDuration) Load() time.Duration
Load loads the current duration.
func (*AtomicDuration) Set ¶
func (d *AtomicDuration) Set(val time.Duration)
Set sets the value to val.
type AtomicFloat64 ¶
type AtomicFloat64 uint64
An AtomicFloat64 is an implementation of atomic float64.
func ForAtomicFloat64 ¶
func ForAtomicFloat64(val float64) *AtomicFloat64
ForAtomicFloat64 returns an AtomicFloat64 with given val.
func NewAtomicFloat64 ¶
func NewAtomicFloat64() *AtomicFloat64
NewAtomicFloat64 returns an AtomicFloat64.
func (*AtomicFloat64) Add ¶
func (f *AtomicFloat64) Add(val float64) float64
Add adds val to current value.
func (*AtomicFloat64) CompareAndSwap ¶
func (f *AtomicFloat64) CompareAndSwap(old, val float64) bool
CompareAndSwap compares current value with old, if equals, set the value to val.
func (*AtomicFloat64) Set ¶
func (f *AtomicFloat64) Set(val float64)
Set sets the current value to val.
type Barrier ¶
type Barrier struct {
// contains filtered or unexported fields
}
A Barrier is used to facility the barrier on a resource.
type Cond ¶
type Cond struct {
// contains filtered or unexported fields
}
A Cond is used to wait for conditions.
type DoneChan ¶
type DoneChan struct {
// contains filtered or unexported fields
}
A DoneChan is used as a channel that can be closed multiple times and wait for done.
func (*DoneChan) Close ¶
func (dc *DoneChan) Close()
Close closes dc, it's safe to close more than once.
func (*DoneChan) Done ¶
func (dc *DoneChan) Done() chan lang.PlaceholderType
Done returns a channel that can be notified on dc closed.
type ImmutableResource ¶
type ImmutableResource struct {
// contains filtered or unexported fields
}
An ImmutableResource is used to manage an immutable resource.
func NewImmutableResource ¶
func NewImmutableResource(fn func() (interface{}, error), opts ...ImmutableResourceOption) *ImmutableResource
NewImmutableResource returns an ImmutableResource.
func (*ImmutableResource) Get ¶
func (ir *ImmutableResource) Get() (interface{}, error)
Get gets the immutable resource, fetches automatically if not loaded.
type ImmutableResourceOption ¶
type ImmutableResourceOption func(resource *ImmutableResource)
ImmutableResourceOption defines the method to customize an ImmutableResource.
func WithRefreshIntervalOnFailure ¶
func WithRefreshIntervalOnFailure(interval time.Duration) ImmutableResourceOption
WithRefreshIntervalOnFailure sets refresh interval on failure. Set interval to 0 to enforce refresh every time if not succeeded, default is time.Second.
type Limit ¶
type Limit struct {
// contains filtered or unexported fields
}
Limit controls the concurrent requests.
func (Limit) Borrow ¶
func (l Limit) Borrow()
Borrow borrows an element from Limit in blocking mode.
type LockedCalls ¶
LockedCalls makes sure the calls with the same key to be called sequentially. For example, A called F, before it's done, B called F, then B's call would not blocked, after A's call finished, B's call got executed. The calls with the same key are independent, not sharing the returned values. A ------->calls F with key and executes<------->returns B ------------------>calls F with key<--------->executes<---->returns
type ManagedResource ¶
type ManagedResource struct {
// contains filtered or unexported fields
}
A ManagedResource is used to manage a resource that might be broken and refetched, like a connection.
func NewManagedResource ¶
func NewManagedResource(generate func() interface{}, equals func(a, b interface{}) bool) *ManagedResource
NewManagedResource returns a ManagedResource.
func (*ManagedResource) MarkBroken ¶
func (mr *ManagedResource) MarkBroken(resource interface{})
MarkBroken marks the resource broken.
func (*ManagedResource) Take ¶
func (mr *ManagedResource) Take() interface{}
Take takes the resource, if not loaded, generates it.
type OnceGuard ¶
type OnceGuard struct {
// contains filtered or unexported fields
}
A OnceGuard is used to make sure a resource can be taken once.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
A Pool is used to pool resources. The difference between sync.Pool is that:
- the limit of the resources
- max age of the resources can be set
- the method to destroy resources can be customized
func NewPool ¶
func NewPool(n int, create func() interface{}, destroy func(interface{}), opts ...PoolOption) *Pool
NewPool returns a Pool.
type PoolOption ¶
type PoolOption func(*Pool)
PoolOption defines the method to customize a Pool.
func WithMaxAge ¶
func WithMaxAge(duration time.Duration) PoolOption
WithMaxAge returns a function to customize a Pool with given max age.
type RefResource ¶
type RefResource struct {
// contains filtered or unexported fields
}
A RefResource is used to reference counting a resource.
func NewRefResource ¶
func NewRefResource(clean func()) *RefResource
NewRefResource returns a RefResource.
func (*RefResource) Clean ¶
func (r *RefResource) Clean()
Clean cleans a resource with reference count decremented.
func (*RefResource) Use ¶
func (r *RefResource) Use() error
Use uses the resource with reference count incremented.
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.
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 SharedCalls ¶
type SharedCalls = SingleFlight
SharedCalls is an alias of SingleFlight. Deprecated: use SingleFlight.
type SingleFlight ¶ added in v1.2.0
type SingleFlight interface { Do(key string, fn func() (interface{}, error)) (interface{}, error) DoEx(key string, fn func() (interface{}, error)) (interface{}, 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 NewSharedCalls ¶
func NewSharedCalls() SingleFlight
NewSharedCalls returns a SingleFlight. Deprecated: use NewSingleFlight.
func NewSingleFlight ¶ added in v1.2.0
func NewSingleFlight() SingleFlight
NewSingleFlight returns a SingleFlight.
type SpinLock ¶
type SpinLock struct {
// contains filtered or unexported fields
}
A SpinLock is used as a lock a fast execution.
type TimeoutLimit ¶
type TimeoutLimit struct {
// contains filtered or unexported fields
}
A TimeoutLimit is used to borrow with timeouts.
func NewTimeoutLimit ¶
func NewTimeoutLimit(n int) TimeoutLimit
NewTimeoutLimit returns a TimeoutLimit.