Documentation
¶
Overview ¶
Package goldflake implements Goldflake, a distributed unique ID generator inspired by Twitter's Snowflake & Sonyflake
A Goldflake ID is composed of
39 bits for time in units of 10 msec 11 bits for a sequence number 13 bits for a machine id
Index ¶
Constants ¶
const ( BitLenTime = 39 // bit length of time BitLenSequence = 11 // bit length of sequence number BitLenMachineID = 63 - BitLenTime - BitLenSequence // bit length of machine id 63-39-11=13 )
These constants are the bit lengths of Goldflake ID parts.
const DefaultMachineID = 8191
DefaultMachineID Default MachineID
Variables ¶
var DefaultStartTime = time.Date(2020, 9, 1, 0, 0, 0, 0, time.UTC)
DefaultStartTime Default Start Time
Functions ¶
Types ¶
type Goldflake ¶
type Goldflake struct {
// contains filtered or unexported fields
}
Goldflake is a distributed unique ID generator.
func NewGoldflake ¶
NewGoldflake returns a new Goldflake configured with the given Settings. NewGoldflake returns nil in the following cases: - Settings.StartTime is ahead of the current time. - Settings.MachineID returns an error. - Settings.CheckMachineID returns false.
type Settings ¶
type Settings struct {
StartTime time.Time
MachineID func() (uint16, error)
CheckMachineID func(uint16) bool
}
Settings configures Goldflake:
StartTime is the time since which the Goldflake time is defined as the elapsed time. If StartTime is 0, the start time of the Goldflake is set to "2020-09-01 00:00:00 +0000 UTC". If StartTime is ahead of the current time, Goldflake is not created.
MachineID returns the unique ID of the Goldflake instance. If MachineID returns an error, Goldflake is not created. If MachineID is nil, default MachineID is used. Default MachineID is the max possible value based of 2^13 i.e. 8191
CheckMachineID validates the uniqueness of the machine ID. If CheckMachineID returns false, Goldflake is not created. If CheckMachineID is nil, no validation is done.