Documentation ¶
Overview ¶
Package allocator provides tools for centralized management of a common resource by multiple clients.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AbstractAllocator ¶
type AbstractAllocator struct {
// contains filtered or unexported fields
}
AbstractAllocator controls access to an abstract pool of resources of a specified capacity. Abstract in this sense means the resources don't actually represent a physical resource (such as disk, memory, etc), but a means of limiting overall concurrent usage by a set of clients.
func NewAbstractAllocator ¶
func NewAbstractAllocator(c int64) (*AbstractAllocator, error)
NewAbstractAllocator returns a new *AbstractAllocator initialized with a set capacity. Returns an error if capacity is < 0. Typical usage of this allocator:
a := NewAbstractAllocator(1024) r, err := a.Alloc(64) // handle err defer r.Release()
func (*AbstractAllocator) Alloc ¶
func (a *AbstractAllocator) Alloc(size int64) (*AbstractResource, error)
Alloc returns a resource of an indicated size or an error. If error is nil, a non-nil *AbstractResource is returned, which the client must Release when finished, or a resource leak will result.
func (*AbstractAllocator) WaitAlloc ¶
func (a *AbstractAllocator) WaitAlloc(ctx context.Context, size int64) (*AbstractResource, error)
WaitAlloc attempts to perform an Alloc up until any supplied context has been cancelled. WaitAlloc will immediately attempt an Alloc and, if unable, will retry at an unspecified rate until the context has cancelled or the Alloc has completed successfully.
type AbstractResource ¶
type AbstractResource struct {
// contains filtered or unexported fields
}
AbstractResource represents some amount of resources granted by an AbstractAllocator and held by a client.
func (*AbstractResource) Release ¶
func (r *AbstractResource) Release()
Release returns a given resource back to the allocator that created this resource. Releasing a nil or previously released resource does nothing.