idalloc

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const IDMaxLimit uint32 = math.MaxUint32

IDMaxLimit is the maximum possible ID value that could be returned by an IDAllocator.

Variables

View Source
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

func (a *IDAllocator) AllocateWithHash(hash [sha256.Size]byte) (id uint32, err error)

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.

Jump to

Keyboard shortcuts

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