Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ID ¶
type ID uint64
ID is a numeric identifier
const NoID ID = 0
NoID is a special ID that represents "no ID available"
type IDPool ¶
type IDPool struct {
// contains filtered or unexported fields
}
IDPool represents a pool of IDs that can be managed concurrently via local usage and external events.
An intermediate state (leased) is introduced to the life cycle of an ID in the pool, in order to prevent lost updates to the pool that can occur as a result of employing both management schemes simultaneously. Local usage of an ID becomes a two stage process of leasing the ID from the pool, and later, Use()ing or Release()ing the ID on the pool upon successful or unsuccessful usage respectively,
The table below shows the state transitions in the ID's life cycle. In the case of LeaseAvailableID() the ID is returned rather than provided as an input to the operation. All ID's begin in the available state.
--------------------------------------------------------------------- |state\event | LeaseAvailableID | Release | Use | Insert | Remove | --------------------------------------------------------------------- |1 available | 2 | * | * | * | 3 | --------------------------------------------------------------------- |2 leased | ** | 1 | 3 | * | 3 | --------------------------------------------------------------------- |3 unavailable | ** | * | * | 1 | * | --------------------------------------------------------------------- * The event has no effect. ** This is guaranteed never to occur.
func (*IDPool) AllocateID ¶
AllocateID returns a random available ID. Unlike LeaseAvailableID, the ID is immediately marked for use and there is no need to call Use().
func (*IDPool) Insert ¶
Insert makes an unavailable ID available in the pool and has no effect otherwise. Returns true if the ID was added back to the pool.
func (*IDPool) LeaseAvailableID ¶
LeaseAvailableID returns an available ID at random from the pool. Returns an ID or NoID if no there is no available ID in the pool.
func (*IDPool) Release ¶
Release returns a leased ID back to the pool. This operation accounts for IDs that were previously leased from the pool but were unused, e.g if allocation was unsuccessful. Thus, it has no effect if the ID is not currently leased in the pool, or the pool has since been refreshed.
Returns true if the ID was returned back to the pool as a result of this call.