snowflake

package
v1.8.80 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2024 License: LGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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")
)

Functions

func Decompose added in v1.8.37

func Decompose(id uint64) map[string]uint64

Decompose returns a set of Sonyflake ID parts.

func ElapsedTime added in v1.8.37

func ElapsedTime(id uint64) time.Duration

ElapsedTime returns the elapsed time when the given Sonyflake ID was generated.

func MachineID added in v1.8.37

func MachineID(id uint64) uint64

MachineID returns the machine ID of a Sonyflake ID.

func QuickID added in v1.8.46

func QuickID() uint64

func QuickID2 added in v1.8.46

func QuickID2() uint64

func SequenceNumber added in v1.8.37

func SequenceNumber(id uint64) uint64

SequenceNumber returns the sequence number of a Sonyflake ID.

Types

type InterfaceAddrs added in v1.8.37

type InterfaceAddrs func() ([]net.Addr, error)

type Settings added in v1.8.37

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 added in v1.8.37

type Sonyflake struct {
	// contains filtered or unexported fields
}

Sonyflake is a distributed unique ID generator.

func New added in v1.8.37

func New(st Settings) (*Sonyflake, error)

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 added in v1.8.37

func NewSonyflake(st Settings) *Sonyflake

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.

func (*Sonyflake) NextID added in v1.8.37

func (sf *Sonyflake) NextID() (uint64, error)

NextID generates a next unique ID. After the Sonyflake time overflows, NextID returns an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL