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 BitLenMachine = 63 - BitLenTime - BitLenSequence // bit length of machine id )
These constants are the bit lengths of Sonyflake ID parts.
Variables ¶
Functions ¶
func SequenceNumber ¶
SequenceNumber returns the sequence number of a Sonyflake ID.
Types ¶
type Settings ¶
type Settings struct {
TimeUnit time.Duration
StartTime time.Time
MachineID func() (int, error)
CheckMachineID func(int) bool
}
Settings configures Sonyflake:
TimeUnit is the time unit of Sonyflake. If TimeUnit is 0, the default time unit is used, which is 10 msec. TimeUnit must be equal to or greater than 1 msec.
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 instance is set to "2025-01-01 00:00:00 +0000 UTC". StartTime must be before the current time.
MachineID returns the unique ID of a Sonyflake instance. If MachineID returns an error, the instance will not be created. If MachineID is nil, the default MachineID is used, which returns the lower 16 bits of the private IP address.
CheckMachineID validates the uniqueness of a machine ID. If CheckMachineID returns false, the instance will not be 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 ¶
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.
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 mock implementations of interfaces defined in types.go.
|
Package mock offers mock implementations of interfaces defined in types.go. |
|
Package types defines type signatures used throughout sonyflake.
|
Package types defines type signatures used throughout sonyflake. |