Documentation ¶
Overview ¶
Package glflake implements glflake, duoxieyun distributed unique ID generator inspired by Twitter's Snowflake.
+------------------------------------------------------------------------+ | 1 Bit Unused | 39 Bit Timestamp | 16 Bit MachineID | 8 Bit Sequence ID | +------------------------------------------------------------------------+
39 bits for time in units of 10 msec (178 years) 16 bits for a machine id (65536 nodes, distributed machines)
8 bits for a sequence number (0 ~ 255))
Index ¶
- Constants
- Variables
- func Decompose(id ID) map[string]int64
- type GLflake
- type ID
- func ParseBase2(id string) (ID, error)
- func ParseBase32(b []byte) (ID, error)
- func ParseBase36(id string) (ID, error)
- func ParseBase58(b []byte) (ID, error)
- func ParseBase64(id string) (ID, error)
- func ParseBytes(id []byte) (ID, error)
- func ParseInt64(id int64) ID
- func ParseIntBytes(id [8]byte) ID
- func ParseString(id string) (ID, error)
- func (f ID) Base2() string
- func (f ID) Base32() string
- func (f ID) Base36() string
- func (f ID) Base58() string
- func (f ID) Base64() string
- func (f ID) Bytes() []byte
- func (f ID) Int64() int64
- func (f ID) IntBytes() [8]byte
- func (f ID) LeadingZerosString(zeroN uint8) string
- func (f ID) MarshalJSON() ([]byte, error)
- func (f ID) String() string
- func (f *ID) UnmarshalJSON(b []byte) error
- type JSONSyntaxError
- type Settings
Constants ¶
const ( BitLenTime = 39 // bit length of time BitLenMachineID = 16 // bit length of machineID BitLenSequence = 8 // bit length of sequence number )
These constants are the bit lengths of glflake ID parts.
Variables ¶
var ErrInvalidBase32 = errors.New("invalid base32")
ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte
var ErrInvalidBase58 = errors.New("invalid base58")
ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte
Functions ¶
Types ¶
type GLflake ¶ added in v1.0.1
type GLflake struct {
// contains filtered or unexported fields
}
GLflake is a distributed unique ID generator.
func NewGlflake ¶
NewGlflake returns a new glflake configured with the given Settings. NewGlflake returns nil in the following cases: - Settings.StartTime is ahead of the current time. - Settings.MachineID returns an error. - Settings.CheckMachineID returns false.
type ID ¶
type ID int64
An ID is a custom type used for a glflake ID. This is used so we can attach methods onto the ID.
func ParseBase2 ¶
ParseBase2 converts a Base2 string into a glflake ID
func ParseBase32 ¶
ParseBase32 parses a base32 []byte into a glflake ID NOTE: There are many different base32 implementations so becareful when doing any interoperation.
func ParseBase36 ¶
ParseBase36 converts a Base36 string into a glflake ID
func ParseBase58 ¶
ParseBase58 parses a base58 []byte into a glflake ID
func ParseBase64 ¶
ParseBase64 converts a base64 string into a glflake ID
func ParseBytes ¶
ParseBytes converts a byte slice into a glflake ID
func ParseIntBytes ¶
ParseIntBytes converts an array of bytes encoded as big endian integer as a glflake ID
func ParseString ¶
ParseString converts a string into a glflake ID
func (ID) Base32 ¶
Base32 uses the z-base-32 character set but encodes and decodes similar to base58, allowing it to create an even smaller result string. NOTE: There are many different base32 implementations so becareful when doing any interoperation.
func (ID) IntBytes ¶
IntBytes returns an array of bytes of the glflake ID, encoded as a big endian integer.
func (ID) LeadingZerosString ¶
LeadingZerosString returns a string of the glflake ID, leading zeros
func (ID) MarshalJSON ¶
MarshalJSON returns a json byte array string of the glflake ID.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON converts a json byte array of a glflake ID into an ID type.
type JSONSyntaxError ¶
type JSONSyntaxError struct {
// contains filtered or unexported fields
}
A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.
func (JSONSyntaxError) Error ¶
func (j JSONSyntaxError) Error() string
type Settings ¶
type Settings struct { StartTime time.Time MachineID func() (uint16, error) CheckMachineID func(uint16) bool }
Settings configures glflake:
StartTime is the time since which the glflake time is defined as the elapsed time. If StartTime is 0, the start time of the glflake is set to "2021-10-01 00:00:00 +0000 UTC". If StartTime is ahead of the current time, glflake is not created.
MachineID returns the unique ID of the glflake instance. If MachineID returns an error, glflake is not created. If MachineID is nil, default MachineID(0) is used.
CheckMachineID validates the uniqueness of the machine ID. If CheckMachineID returns false, glflake is not created. If CheckMachineID is nil, no validation is done.
func (*Settings) StartTimeSet ¶
StartTimeSet set start time