Documentation ¶
Index ¶
Constants ¶
const ( TimestampLength = 41 MachineIDLength = 10 SequenceLength = 12 MaxSequence = 1<<SequenceLength - 1 MaxTimestamp = 1<<TimestampLength - 1 MaxMachineID = 1<<MachineIDLength - 1 )
These constants are the bit lengths of snowflake ID parts.
Variables ¶
This section is empty.
Functions ¶
func AtomicResolver ¶
AtomicResolver define as atomic sequence resolver, base on standard sync/atomic.
func ID ¶
func ID() uint64
ID use ID to generate snowflake id and it will ignore error. if you want error info, you need use NextID method. This function is thread safe.
func NextID ¶
NextID use NextID to generate snowflake id and return an error. This function is thread safe.
func PrivateIPToMachineID ¶
func PrivateIPToMachineID() uint16
PrivateIPToMachineID convert private ip to machine id.
func SetMachineID ¶
func SetMachineID(m uint16)
SetMachineID specify the machine ID. It will panic when machineid > max limit for 2^10-1. This function is thread-unsafe, recommended you call him in the main function.
func SetSequenceResolver ¶
func SetSequenceResolver(seq SequenceResolver)
SetSequenceResolver set an custom sequence resolver. This function is thread-unsafe, recommended you call him in the main function.
func SetStartTime ¶
SetStartTime set the start time for snowflake algorithm.
It will panic when:
s IsZero s > current millisecond current millisecond - s > 2^41(69 years).
This function is thread-unsafe, recommended you call him in the main function.
Types ¶
type SID ¶
SID snowflake id
func (*SID) GenerateTime ¶
GenerateTime snowflake generate at, return a UTC time.
type SequenceResolver ¶
SequenceResolver the snowflake sequence resolver.
When you want use the snowflake algorithm to generate unique ID, You must ensure: The sequence-number generated in the same millisecond of the same node is unique. Based on this, we create this interface provide following reslover:
AtomicResolver : base sync/atomic (by default).