Documentation
¶
Overview ¶
Package sonyflake implements Sonyflake, a distributed unique ID generator inspired by Twitter's Snowflake.
By default, 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 ¶
This section is empty.
Variables ¶
var ( ErrInvalidBitsTime = errors.New("bit length for time must be 32 or more") ErrInvalidBitsSequence = errors.New("invalid bit length for sequence number") ErrInvalidBitsMachineID = errors.New("invalid bit length for machine id") ErrInvalidTimeUnit = errors.New("invalid time unit") ErrInvalidSequence = errors.New("invalid sequence number") ErrInvalidMachineID = errors.New("invalid machine id") ErrStartTimeAhead = errors.New("start time is ahead") ErrOverTimeLimit = errors.New("over the time limit") ErrNoPrivateAddress = errors.New("no private ip address") )
Functions ¶
This section is empty.
Types ¶
type Settings ¶
type Settings struct {
BitsSequence int
BitsMachineID int
TimeUnit time.Duration
StartTime time.Time
MachineID func() (int, error)
CheckMachineID func(int) bool
}
Settings configures Sonyflake:
BitsSequence is the bit length of a sequence number. If BitsSequence is 0, the default bit length is used, which is 8. If BitsSequence is 31 or more, an error is returned.
BitsMachineID is the bit length of a machine ID. If BitsMachineID is 0, the default bit length is used, which is 16. If BitsMachineID is 31 or more, an error is returned.
TimeUnit is the time unit of Sonyflake. If TimeUnit is 0, the default time unit is used, which is 10 msec. TimeUnit must be 1 msec or longer.
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.
The bit length of time is calculated by 63 - BitsSequence - BitsMachineID. If it is less than 32, an error is returned.
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.BitsSequence is less than 0 or greater than 30. - Settings.BitsMachineID is less than 0 or greater than 30. - Settings.BitsSequence + Settings.BitsMachineID is 32 or more. - Settings.TimeUnit is less than 1 msec. - Settings.StartTime is ahead of the current time. - Settings.MachineID returns an error. - Settings.CheckMachineID returns false.
func (*Sonyflake) Compose ¶ added in v2.1.0
Compose creates a Sonyflake ID from its components. The time parameter should be the time when the ID was generated. The sequence parameter should be between 0 and 2^BitsSequence-1 (inclusive). The machineID parameter should be between 0 and 2^BitsMachineID-1 (inclusive).
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. |