Documentation ¶
Overview ¶
Package snowflake implements Snowflake, a distributed unique ID generator inspired by Twitter's Snowflake.
A Snowflake ID is composed of
39 bits for time in units of 10 msec 8 bits for a sequence number 16 bits for a machine id
Index ¶
Constants ¶
const ( BitLenTime = 39 // bit length of time BitLenSequence = 8 // bit length of sequence number BitLenMachineId = 63 - BitLenTime - BitLenSequence // bit length of machine id )
These constants are the bit lengths of Snowflake ID parts.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Settings ¶
type Settings struct { StartTime time.Time MachineId func() (uint16, error) CheckMachineId func(uint16) bool }
Settings configures Snowflake:
StartTime is the time since which the Snowflake time is defined as the elapsed time. If StartTime is 0, the start time of the Snowflake is set to "2014-09-01 00:00:00 +0000 UTC". If StartTime is ahead of the current time, Snowflake is not created.
MachineId returns the unique ID of the Snowflake instance. If MachineId returns an error, Snowflake is not created. If MachineId is nil, default MachineId is used. Default MachineId returns the lower 16 bits of the private IP address.
CheckMachineId validates the uniqueness of the machine ID. If CheckMachineId returns false, Snowflake is not created. If CheckMachineId is nil, no validation is done.
type Snowflake ¶
type Snowflake struct {
// contains filtered or unexported fields
}
Snowflake is a distributed unique ID generator.
func NewSnowflake ¶
NewSnowflake returns a new Snowflake configured with the given Settings. NewSnowflake returns nil in the following cases: - Settings.StartTime is ahead of the current time. - Settings.MachineId returns an error. - Settings.CheckMachineId returns false.