snowflake

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MulanPSL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

snowflake is a distributed, k-ordered unique ID generator.

Under 64 bits (Long Integer)
Lock-free (using atomic CAS)
Decentralized and no coordination needed
Docker friendly

根据 https://github.com/beinan/fastid 修改所得

Index

Constants

View Source
const (
	//StartTimeEnvName is the env key for ID generating start time
	StartTimeEnvName = "FASTID_START_TIME"
	//MachineIDEnvName is the env key for machine id
	MachineIDEnvName = "FASTID_MACHINE_ID"
)

Variables

View Source
var (

	// BenchmarkConfig is a high performance setting for benchmark
	//  40 bits timestamp
	//  15 bits seq
	//  8  bits machine id
	BenchmarkConfig = ConstructConfig(40, 15, 8)

	// CommonConfig is the recommended setting for most applications
	//  40 bits timestamp
	//  7  bits seq
	//  16 bits machine id
	CommonConfig = ConstructConfig(40, 7, 16)

	// JsConfig 兼容JS前台整型数据正常显示,支持的范围 -9007199254740992 ~ +9007199254740992
	//  40 bits timestamp
	//  7  bits seq
	//  8 bits machine id
	JsConfig = ConstructConfig(40, 7, 8)
)

Functions

This section is empty.

Types

type Config

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

Config maintains the settings for id generating

func ConstructConfig

func ConstructConfig(timeBits, seqBits, machineBits uint) *Config

ConstructConfig creates an instance of FastIDConfig with the given settings

func ConstructConfigWithMachineID

func ConstructConfigWithMachineID(timeBits, seqBits, machineBits uint, machineID int64) *Config

ConstructConfigWithMachineID creates an config with machine id, in case you don't want to use the lower 16 bits of the IP address.

func (*Config) GenInt64ID

func (c *Config) GenInt64ID() int64

GenInt64ID generates unique int64 IDs with the setting in the methond owner

func (*Config) GetSeqFromID

func (c *Config) GetSeqFromID(id int64) int64

GetSeqFromID extracts seq number from an existing ID

func (*Config) GetTimeFromID

func (c *Config) GetTimeFromID(id int64) int64

GetTimeFromID extracts timestamp from an existing ID

Jump to

Keyboard shortcuts

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