sfid

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sfid generates snowflake-style int64 IDs from a clock, a worker id and a per-tick sequence.

NewDefaultIDGen uses 1ms clock units, epoch 2025-05-21T00:00:00Z, 10 worker bits and 12 sequence bits. NewIDGen customizes all of them; the layout must allow at least 10 years of IDs from the epoch. A generator returns an error from ID when the clock moves backwards within 5ms; MustID panics instead.

Attach a generator with With / Carry and fetch it with From / Must. WorkerIDFromLocalIP derives a worker id from the last two octets of the first non-loopback IPv4 address.

Usage:

ctx := sfid.With(context.Background(), sfid.NewDefaultIDGen(1))
id := sfid.Must(ctx).MustID()

This package replaces the archived module github.com/xoctopus/sfid.

Index

Constants

This section is empty.

Variables

View Source
var (
	NewFactory = factory.NewFactory
	NewWorker  = factory.NewWorker
)

Functions

func Carry

func Carry(g IDGen) contextx.Carrier

func With

func With(ctx context.Context, g IDGen) context.Context

func WorkerIDFromIP

func WorkerIDFromIP(ipv4 net.IP) uint32

func WorkerIDFromLocalIP

func WorkerIDFromLocalIP() (uint32, error)

Types

type Factory

type Factory = factory.Factory

type IDGen

type IDGen interface {
	ID() (int64, error)
	MustID() int64
}

func From

func From(ctx context.Context) (IDGen, bool)

func Must

func Must(ctx context.Context) IDGen

func NewDefaultIDGen

func NewDefaultIDGen(worker uint32) IDGen

func NewIDGen

func NewIDGen(worker uint32, unit int, base time.Time, wbs, sbs int) IDGen

NewIDGen new id generator worker: worker id unit: snowflake clock unit(milliseconds) base: snowflake epoch timestamp wbs: worker bits sbs: sequence bits

type Worker

type Worker = factory.Worker

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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