idgen

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2023 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxStep               = 10e7
	DefaultStep           = 2000            // default step
	DefaultRetry          = 3               // default retry times when id allocator preload next segment
	DefaultPreloadTimeout = 3 * time.Second // default timeout  when id allocator preload next segment
)

Variables

This section is empty.

Functions

func AddRandomFilter

func AddRandomFilter(bit uint) func(id int64) (int64, error)

func NewidAllocator

func NewidAllocator(bizTag string) *idAllocator

Types

type IdFilter

type IdFilter func(id int64) (int64, error)

type IdGenerator

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

func NewIdGenrator

func NewIdGenrator(store IdStore, opts ...Option) *IdGenerator

func (*IdGenerator) AddBizTag

func (this *IdGenerator) AddBizTag(ctx context.Context, bizTag string) (*idAllocator, error)

func (*IdGenerator) GetId

func (this *IdGenerator) GetId(ctx context.Context, bizTag string) (int64, error)

type IdStore

type IdStore interface {
	GetNextSegment(ctx context.Context, bizTag string, step int64) (*Seg, error)
}

type Option

type Option func(*IdGenerator)

func With2BytesRandomFilter

func With2BytesRandomFilter() Option

A filter that inserts 16 bits into the lowest part of the id

+-----------------------+
|0|0000.......0000|0...0|
+-----------------------+

1bit reserved 47bit incr id 16bit random

1.4 trillion

func WithExpireTime

func WithExpireTime(expireTime time.Duration) Option

func WithIdFilter

func WithIdFilter(filters []IdFilter) Option

func WithPreloadRetryTimes

func WithPreloadRetryTimes(times int) Option

func WithPreloadTimeout

func WithPreloadTimeout(timeout time.Duration) Option

func WithStep

func WithStep(step int64) Option

type RedisIdStore

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

func NewRedisIdStore

func NewRedisIdStore(client *redis.Client) *RedisIdStore

func (*RedisIdStore) GetNextSegment

func (this *RedisIdStore) GetNextSegment(ctx context.Context, bizTag string, step int64) (*Seg, error)

type Seg

type Seg struct {
	BizTag string
	MaxId  int64
	Step   int64
}

Jump to

Keyboard shortcuts

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