snowflake

package module
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

Snowflake

distributed unique ID generator inspired by Twitter's Snowflake with custom bit assignments:

39 bits for time in units of 10 milliseconds(since 2020-01-01 00:00:00 UTC by default), can hold around 174 yrs of time
16 bits for a node ID
 9 bits for a sequence number

snowflake provide an out of box default settings for usage on public cloud and containers(such as AWS, Azure) by using the lowest 16 bits of the private ip address as node ID.

NOTE: please don't run multiple instance in same machine/container with the default generator

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicGenerator added in v0.1.2

func AtomicGenerator(currentTime int64) (uint16, error)

AtomicGenerator define as atomic sequence Resolver, base on standard sync/atomic.

func NewID

func NewID() uint64

NewID generate a unique snowflake ID with default settings, use lower 16 bits of current private IP as nodeID which is out-of-box for AWS private networks and also containers, will ignore the errors when generating unique ID

func NextID

func NextID() (uint64, error)

NextID generate a unique snowflake ID with default settings, use lower 16 bits of current private IP as nodeID which is out-of-box for AWS private networks and also containers, return the errors when generating the unique ID

Types

type Generator

type Generator struct {
	Settings
	// contains filtered or unexported fields
}

Generator a snowflakeID generator

func NewGenerator

func NewGenerator(settings Settings) (*Generator, error)

NewGenerator create a generator with custom settings

func (*Generator) Decompose

func (g *Generator) Decompose(sid uint64) (id ID)

Decompose returns a map of snowflake id parts.

func (*Generator) NextID

func (g *Generator) NextID() (sid uint64, err error)

NextID generate a new unique snowflake ID

type ID

type ID struct {
	Timestamp  time.Time `json:"timestamp,omitempty"`
	SinceEpoch uint64    `json:"since_epoch,omitempty"`
	NodeID     uint16    `json:"node_id,omitempty"`
	Sequence   uint16    `json:"sequence,omitempty"`
}

ID a snowflake ID

func Decompose

func Decompose(id uint64) ID

Decompose returns a struct of snowflake ID with the default generator

type NodeIDProvider

type NodeIDProvider func() (uint16, error)

NodeIDProvider the snowflake Node Generator provider.

type SequenceGenerator added in v0.1.2

type SequenceGenerator func(current int64) (uint16, error)

SequenceGenerator the snowflake sequence generator. When use the snowflake algorithm to generate unique ID, make sure:

The sequence-number generated in the same 10 milliseconds of the same node is unique.

Based on this, we create this interface provides following Generator:

AtomicGenerator : base sync/atomic (by default).

type Settings

type Settings struct {
	// Epoch base time for the timestamp Ref: https://en.wikipedia.org/wiki/Epoch_(computing)
	Epoch             time.Time
	SequenceGenerator SequenceGenerator
	NodeIDProvider    NodeIDProvider
}

Settings snowflake generate settings

Jump to

Keyboard shortcuts

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