idgen

package
v0.0.0-...-2d29fd2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package idgen

(集中 server 部署情况下, 不需要区分 scope) normal: 32b timestamp(second) + 10b timestamp(millisecond) + 8b counter + 14b server_id (1 + 13b id_server_id) custom: 32b timestamp(second) + 18b custom_counter + 01 + 12b scope_id fallback: 32b timestamp(second) + 10b timestamp(millisecond) + 8b counter + 00 + 12b scope_id fake: 32b timestamp(second) + 10b timestamp(millisecond) + 8b counter + 00 + 000000000000

todo: 支持 counter 向 timestamp 借位

Index

Constants

View Source
const (
	ControlIDDigits = 14
	CounterDigits   = 8

	ControlDigitsPos = 1 << 13
)

Variables

View Source
var ErrGenerateIDFailed = errors.New("generate id failed")

Functions

This section is empty.

Types

type IDGen

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

func NewIDGen

func NewIDGen(generators ...IGenerator) *IDGen

func (*IDGen) Get

func (g *IDGen) Get(ctx context.Context) (int64, error)

func (*IDGen) MGet

func (g *IDGen) MGet(ctx context.Context, count int64) ([]int64, error)

type IGenerator

type IGenerator interface {
	Get(ctx context.Context) (int64, error)
	MGet(ctx context.Context, count int64) ([]int64, error)
}

func NewFakeGen

func NewFakeGen() IGenerator

NewFakeGen returns a fake generator rule: 32b timestamp(second) + 10b timestamp(millisecond) + 8b counter + 00 + 000000000000

type LocalMUGen

type LocalMUGen struct {
	syncounter.Counter
	// contains filtered or unexported fields
}

func NewLocalMUGen

func NewLocalMUGen(scopeID int64, fallback bool) *LocalMUGen

NewLocalMUGen fallback: 32b timestamp(second) + 10b timestamp(millisecond) + 8b counter + 00 + 12b scope_id 分为运行在 global server 和运行在业务 server 两种模式, 后者为 fallback todo: 允许 counter 向 timestamp 借位

func (*LocalMUGen) Get

func (gen *LocalMUGen) Get(ctx context.Context) (int64, error)

func (*LocalMUGen) MGet

func (gen *LocalMUGen) MGet(ctx context.Context, count int64) ([]int64, error)

Jump to

Keyboard shortcuts

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