sonyflake

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2025 License: MIT Imports: 5 Imported by: 17

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

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

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.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

func (sf *Sonyflake) Compose(t time.Time, sequence, machineID int) (int64, error)

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).

func (*Sonyflake) Decompose added in v2.0.1

func (sf *Sonyflake) Decompose(id int64) map[string]int64

Decompose returns a set of Sonyflake ID parts.

func (*Sonyflake) NextID

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

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

func (*Sonyflake) ToTime

func (sf *Sonyflake) ToTime(id int64) time.Time

ToTime returns the time when the given ID was generated.

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.

Jump to

Keyboard shortcuts

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