Documentation
¶
Index ¶
Constants ¶
const IDMaxLimit uint32 = math.MaxUint32
IDMaxLimit is the maximum possible ID value that could be returned by an IDAllocator.
Variables ¶
var ErrIDsExhausted = errors.New("no more IDs available")
ErrIDsExhausted is returned when an IDAllocator is unable to fulfill an allocation request.
Functions ¶
This section is empty.
Types ¶
type IDAllocator ¶
type IDAllocator struct {
// contains filtered or unexported fields
}
IDAllocator is an allocator for ID values. It is implemented using a bitmap that is grown as necessary.
func New ¶
func New(numIDs uint32) IDAllocator
New creates a new IDAllocator that may allocate up to numIDs values. numIDs must be a multiple of 64.
func (*IDAllocator) Allocate ¶
func (a *IDAllocator) Allocate() (id uint32, err error)
Allocate finds an unused ID, sets it as used, and returns its value. If the IDAllocator is full and there are no more IDs available, id will be set to IDMaxLimit and err will be set to ErrIDsExhausted.
func (*IDAllocator) AllocateWithHash ¶
AllocateWithHash allocates an ID based on the given SHA256 hash. The same hash will always result in the same ID. If the calculated ID is already allocated, it will try the next ID until an unallocated one is found.
func (*IDAllocator) Free ¶
func (a *IDAllocator) Free(id uint32)
Free marks id as unused. id must have been previously returned by a successful call to Allocate.