Documentation
¶
Overview ¶
Package sfid generates Twitter-compatible 63-bit Snowflake IDs.
A Generator issues strictly increasing IDs and suits most programs. Generator.Fill issues a whole batch at a fraction of the per-ID cost. A Pool trades ordering for less cache-line contention when many goroutines generate IDs on many cores. Each node ID admits at most 4096 IDs per millisecond; generation waits for the next millisecond once the current one is consumed.
Index ¶
Constants ¶
const ( // EpochMilliseconds is the original Twitter Snowflake epoch in Unix milliseconds. EpochMilliseconds int64 = 1288834974657 // MaxNodeID is the largest node identifier supported by the 10-bit node field. MaxNodeID uint16 = (1 << nodeBits) - 1 // MaxSequence is the largest sequence value supported within one millisecond. MaxSequence uint16 = (1 << seqBits) - 1 // MaxBucketCount is the largest pool bucket count supported by the sequence field. MaxBucketCount uint = 1 << seqBits )
Variables ¶
var ( // ErrInvalidNodeID reports a node identifier outside the 10-bit node field. ErrInvalidNodeID = errors.New("sfid: node ID exceeds 10 bits") // ErrInvalidBucketCount reports a zero, oversized, or non-power-of-two bucket count. ErrInvalidBucketCount = errors.New("sfid: bucket count must be a power of two from 1 through 4096") // ErrEpochNotReached reports that the system time is earlier than the Snowflake epoch. ErrEpochNotReached = errors.New("sfid: system time is earlier than the epoch") // ErrTimestampOverflow reports that the 41-bit timestamp field is exhausted. ErrTimestampOverflow = errors.New("sfid: timestamp exceeds 41 bits") )
Functions ¶
This section is empty.
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
A Generator generates strictly increasing Snowflake IDs for one node and is safe for concurrent use.
A Generator issues at most 4096 IDs per millisecond.
func New ¶
New constructs a concurrent Snowflake Generator for nodeID.
The caller must assign a unique nodeID to each Generator process that can produce IDs for the same system.
func (*Generator) Fill ¶
Fill stores strictly increasing Snowflake IDs in every element of ids.
Fill does not allocate, retain ids, or read its previous contents. It reserves each run of IDs within one millisecond with a single clock read and atomic operation, so it costs far less per ID than repeated Generator.Generate calls. Fill waits for the next millisecond whenever the current one is consumed, so filling n IDs spans at least (n-1)/4096 milliseconds, rounded down.
type ID ¶
type ID int64
An ID is a Twitter-compatible Snowflake identifier.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
A Pool generates Snowflake IDs through cache-separated sequence buckets and is safe for concurrent use.
Each goroutine prefers the bucket of the processor it runs on, so concurrent callers rarely share a cache line. A bucket that exhausts its share of a millisecond spills callers to the other buckets. A Pool issues at most 4096 IDs per millisecond, and its IDs are unique but not ordered.
func NewPool ¶
NewPool constructs a concurrent Snowflake Pool with bucketCount cache-separated sequence buckets for nodeID.
bucketCount must be a power of two from 1 through MaxBucketCount. The caller must assign a unique nodeID to each generator process that can produce IDs for the same system.
func (*Pool) Fill ¶
Fill stores unique Snowflake IDs in every element of ids.
Fill does not allocate, retain ids, or read its previous contents. It reserves each run of IDs from one bucket and millisecond with a single clock read and atomic operation. A run holds at most 4096/bucketCount IDs, so Generator.Fill suits large batches better. Fill waits for the next millisecond whenever the current one is consumed.