Documentation
¶
Overview ¶
Package zflake implements distributed unique ID generator inspired by Twitter's Snowflake.
A zflake ID is composed of
1 bit (most significant) reserved 38 bits for time in units of 10 msec 13 bits for a sequence number 12 bits for a generator ID (GID)
Above bit assigment dictate following zflake properties:
- The lifetime of ~87 years since the start of `zflake` epoch. - Can generate at most 2^13 (8192) IDs per 10ms for each generator ID. - 2^12 (4096) generators. - Ability to generate Base62 string representations of int64 IDs.
Index ¶
Constants ¶
const ( // BucketLen defines the length of zflake time bucket in nanoseconds. BucketLen = int64(10 * time.Millisecond) // BitLenTim number of bits assigned to time buckets. BitLenTim = 38 // BitLenSeq number of bits assigned to sequence number. BitLenSeq = 13 // BitLenGID number of bits assigned to generator ID (GID). BitLenGID = 63 - BitLenTim - BitLenSeq // DefaultEpoch represents default zflake epoch 2020-01-01T00:00:00Z as // nanoseconds since Unix epoch (1970-01-01T00:00:00Z). DefaultEpoch int64 = 1577836800000000000 // DefaultGID represents default generator ID. DefaultGID uint16 = 0 )
Variables ¶
This section is empty.
Functions ¶
func Clock ¶
Clock is Flake constructor option injecting custom clock.
This option is mostly used to test zflake behaviour.
func DecodeSID ¶ added in v0.2.0
DecodeSID decodes Base62 representation of the zflake ID back to int64.
Types ¶
type Gen ¶
type Gen struct {
// contains filtered or unexported fields
}
Gen represents distributed unique ID generator.
func NewGen ¶
NewGen returns a new generator with default configuration. NewGen returns nil if epoch is set ahead of the current time.