// Package sonyflake implements Sonyflake, a distributed unique ID generator inspired by Twitter's Snowflake. // // A Sonyflake 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 package sonyflake import ( "errors" "net" "sync" "time" "github.com/sony/sonyflake/v2/types" ) // These constants are the bit lengths of Sonyflake ID parts. const ( BitLenTime = 39 // bit length of time BitLenSequence = 8 // bit length of sequence number BitLenMachine = 63 - BitLenTime - BitLenSequence // bit length of machine id ) // Settings configures Sonyflake: // // StartTime is the time since which the Sonyflake time is defined as the elapsed time. // If StartTime is 0, the start time of the Sonyflake is set to "2014-09-01 00:00:00 +0000 UTC". // If StartTime is ahead of the current time, Sonyflake is not created. // // MachineID returns the unique ID of the Sonyflake instance. // If MachineID returns an error, Sonyflake 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, Sonyflake is not created. // If CheckMachineID is nil, no validation is done. type Settings struct { StartTime time.Time MachineID func() (int, error) CheckMachineID func(int) bool } // Sonyflake is a distributed unique ID generator. type Sonyflake struct { mutex *sync.Mutex startTime int64 elapsedTime int64 sequence int machine int } var ( ErrStartTimeAhead = errors.New("start time is ahead of now") ErrNoPrivateAddress = errors.New("no private ip address") ErrOverTimeLimit = errors.New("over the time limit") ErrInvalidMachineID = errors.New("invalid machine id") ) var defaultInterfaceAddrs = net.InterfaceAddrs // New returns a new Sonyflake configured with the given Settings. // New returns an error in the following cases: // - Settings.StartTime is ahead of the current time. // - Settings.MachineID returns an error. // - Settings.CheckMachineID returns false. func New(st Settings) (*Sonyflake, error) { if st.StartTime.After(time.Now()) { return nil, ErrStartTimeAhead } sf := new(Sonyflake) sf.mutex = new(sync.Mutex) sf.sequence = 1<= 1<= 16 && ip[1] < 32) || ip[0] == 192 && ip[1] == 168 || ip[0] == 169 && ip[1] == 254) } func lower16BitPrivateIP(interfaceAddrs types.InterfaceAddrs) (int, error) { ip, err := privateIPv4(interfaceAddrs) if err != nil { return 0, err } return int(ip[2])<<8 + int(ip[3]), nil } // ElapsedTime returns the elapsed time when the given Sonyflake ID was generated. func ElapsedTime(id int64) time.Duration { return time.Duration(elapsedTime(id) * sonyflakeTimeUnit) } func elapsedTime(id int64) int64 { return id >> (BitLenSequence + BitLenMachine) } // SequenceNumber returns the sequence number of a Sonyflake ID. func SequenceNumber(id int64) int { const maskSequence = int64((1<> BitLenMachine) } // MachineID returns the machine ID of a Sonyflake ID. func MachineID(id int64) int { const maskMachine = int64(1<