snowflake

package module
v0.0.0-...-d41d492 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 9 Imported by: 0

README

snowflake

Release Go Version

Snowflakes are distributed and sortable IDs based on Twitter’s Snowflake algorithm.

By design, the IDs generated are intentionally predictable to maintain sortability. Do not use them for sensitive identifiers like session tokens.

Installation

go get github.com/ruhrcloud/snowflake

Usage

import (
	"fmt"
	"github.com/ruhrcloud/snowflake"
)

func main() {
    node, _ := snowflake.New(1)
    id, _ := node.Generate()
    fmt.Println(id.String())
}

Documentation

Full documentation is available on pkg.go.dev.

Documentation

Index

Constants

View Source
const (
	TimestampBits = uint8(41)
	NodeBits      = uint8(10)
	StepBits      = uint8(12)

	DatacenterBits = uint8(5)
	WorkerBits     = uint8(5)

	MaxNode = int64(-1 ^ (-1 << NodeBits))
	MaxStep = int64(-1 ^ (-1 << StepBits))

	MaxDatacenter = int64(-1 ^ (-1 << DatacenterBits))
	MaxWorker     = int64(-1 ^ (-1 << WorkerBits))

	NodeShift      = uint8(0)
	StepShift      = NodeBits
	TimestampShift = NodeBits + StepBits
)
View Source
const BaseEpoch = int64(1288834974657)

TODO This is currently based on Twitters epoch

Variables

View Source
var (
	ErrNodeIDInvalid  = errors.New("node ID must be between 0 and 1023")
	ErrClockBackwards = errors.New("clock moved backwards")
)

Functions

This section is empty.

Types

type Decomposed

type Decomposed struct {
	Timestamp int64
	Node      int64
	Step      int64
}

type ID

type ID uint64

func ParseString

func ParseString(s string) (ID, error)

func (ID) Compare

func (id ID) Compare(other ID) int

func (ID) Decompose

func (f ID) Decompose() Decomposed

func (ID) Equal

func (id ID) Equal(other ID) bool

func (ID) Int64

func (f ID) Int64() int64

func (ID) MarshalBinary

func (id ID) MarshalBinary() ([]byte, error)

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

func (ID) MarshalText

func (id ID) MarshalText() ([]byte, error)

func (ID) Node

func (f ID) Node() int64

func (*ID) Scan

func (id *ID) Scan(v interface{}) error

func (ID) Step

func (f ID) Step() int64

func (ID) String

func (f ID) String() string

func (ID) Timestamp

func (f ID) Timestamp() int64

func (*ID) UnmarshalBinary

func (id *ID) UnmarshalBinary(data []byte) error

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(b []byte) error

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(data []byte) error

func (ID) Value

func (id ID) Value() (driver.Value, error)

type Node

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

func New

func New(id int64) (*Node, error)

func NewFromParts

func NewFromParts(datacenterID, workerID int64) (*Node, error)

func (*Node) Generate

func (n *Node) Generate() (ID, error)

func (*Node) SetEpoch

func (n *Node) SetEpoch(epoch int64)

Jump to

Keyboard shortcuts

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