snow2

package
v0.0.0-...-561b508 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package snow2 provides a very simple Twitter snowflake generator and parser.

Index

Constants

View Source
const (
	// NodeBits holds the number of bits to use for Node
	// Remember, you have a total 22 bits to share between Node/Step
	NodeBits uint8 = 10

	// StepBits holds the number of bits to use for Step
	// Remember, you have a total 22 bits to share between Node/Step
	StepBits uint8 = 12
)

Variables

View Source
var Default, _ = NewNode()

Node12 每秒可以生成 256 个,可以有 4 个节点,可以使用 27.8 年,计算过程,见 cid_test.go 97656251 = 100000000000 >> 10 + 1

NodeUint32 creates and returns a unique snowflake ID for uint32 CAUTION: only for low frequency usages,低频使用场景. 每秒可以生成 2^3 = 8 个,可以有 1 个节点,可以使用 17 年 timestamp(29) + node(0) + step(3), 可用 2^29/60/60/24/365 ≈ 17 年 max uint32 is 4294967295 (0b11111111_11111111_11111111_11111111)

Functions

func Next

func Next() int64

Types

type Config

type Config struct {
	Epoch     time.Time
	EpochAdd  int64
	TimeRound time.Duration
	NodeBits  uint8
	StepBits  uint8
	NodeID    int64
}

type ConfigFn

type ConfigFn func(*Config)

func WithEpoch

func WithEpoch(n time.Time) ConfigFn

func WithEpochAdd

func WithEpochAdd(n int64) ConfigFn

func WithNodeBits

func WithNodeBits(n uint8) ConfigFn

func WithNodeID

func WithNodeID(n int64) ConfigFn

func WithStepBits

func WithStepBits(n uint8) ConfigFn

func WithTimeRound

func WithTimeRound(n time.Duration) ConfigFn

type Node

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

A Node struct holds the basic information needed for a snowflake generator node

func NewNode

func NewNode(fns ...ConfigFn) (*Node, error)

NewNode returns a new snowflake node that can be used to generate snowflake IDs

func (*Node) Next

func (n *Node) Next() int64

Next creates and returns a unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node ID

Jump to

Keyboard shortcuts

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