Documentation ¶
Overview ¶
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
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 Sonyflake ID parts.
Variables ¶
Functions ¶
func ElapsedTime ¶ added in v1.1.0
ElapsedTime returns the elapsed time when the given Sonyflake ID was generated.
func SequenceNumber ¶ added in v1.1.0
SequenceNumber returns the sequence number of a Sonyflake ID.
Types ¶
type Settings ¶
type Settings struct { StartTime time.Time MachineID func() (uint16, error) CheckMachineID func(uint16) bool }
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 Sonyflake ¶
type Sonyflake struct {
// contains filtered or unexported fields
}
Sonyflake is a distributed unique ID generator.
func New ¶ added in v1.2.0
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 NewSonyflake ¶
NewSonyflake returns a new Sonyflake configured with the given Settings. NewSonyflake returns nil in the following cases: - Settings.StartTime is ahead of the current time. - Settings.MachineID returns an error. - Settings.CheckMachineID returns false.
Directories ¶
Path | Synopsis |
---|---|
Package awsutil provides utility functions for using Sonyflake on AWS.
|
Package awsutil provides utility functions for using Sonyflake on AWS. |
Package mock offers implementations of interfaces defined in types.go This allows complete control over input / output for any given method that consumes a given type
|
Package mock offers implementations of interfaces defined in types.go This allows complete control over input / output for any given method that consumes a given type |
Package Types defines type signatures used throughout SonyFlake.
|
Package Types defines type signatures used throughout SonyFlake. |