sonyflake

package module
v2.0.0 Latest Latest
Warning

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

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

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

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

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")
	ErrInvalidTimeUnit  = errors.New("invalid time unit")
)

Functions

func Decompose

func Decompose(id int64) map[string]int64

Decompose returns a set of Sonyflake ID parts.

func MachineID

func MachineID(id int64) int

MachineID returns the machine ID of a Sonyflake ID.

func SequenceNumber

func SequenceNumber(id int64) int

SequenceNumber returns the sequence number of a Sonyflake ID.

func Time

func Time(id int64) int64

Time returns the Sonyflake time when the given ID was generated.

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

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 (*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

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