snowflake

package
v0.0.0-...-5c9bec8 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lock

func Lock() error

func NewService

func NewService(generator *Snowflake) api.SnowflakeServer

Types

type Coordinator

type Coordinator interface {
	GetOrCreateWorker(ctx context.Context) (*Worker, error)
	SyncTime(ctx context.Context) error
}

func NewEtcdv3Coordinator

func NewEtcdv3Coordinator() Coordinator

type Etcdv3Coordinator

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

func (*Etcdv3Coordinator) GetOrCreateWorker

func (e *Etcdv3Coordinator) GetOrCreateWorker(ctx context.Context) (*Worker, error)

func (*Etcdv3Coordinator) SyncTime

func (e *Etcdv3Coordinator) SyncTime(ctx context.Context) error

func (*Etcdv3Coordinator) WorkerIDKeyPrefix

func (e *Etcdv3Coordinator) WorkerIDKeyPrefix() string

type Options

type Options struct {
	WorkID       uint64
	DataCenterID uint64
}

type Service

type Service struct {
	api.UnimplementedSnowflakeServer
	// contains filtered or unexported fields
}

func (*Service) FetchNext

func (s *Service) FetchNext(ctx context.Context, req *api.FetchSnowflakeNextReq) (*api.UUID, error)

type Snowflake

type Snowflake struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewWorker

func NewWorker(coordinator Coordinator) *Snowflake

初始化节点标识

func (*Snowflake) FetchNext

func (s *Snowflake) FetchNext() (uint64, error)

GetUID 得到全局唯一ID int64类型 首位0(1位) + 毫秒时间戳(41位) + 数据中心标识(5位) + 工作机器标识(5位) + 自增id(12位) 时间可以保证400年不重复 数据中心和机器标识一起标识节点,最多支持1024个节点 每个节点每一毫秒能生成最多4096个id 63 62 21 16 11 0 +-------+-------------+-------------+---------+--------+ | 未使用 | 毫秒级时间戳 | 数据中心标识 | 工作机器 | 自增id | +-------+-------------+-------------+---------+--------+

type Worker

type Worker struct {
	WorkerID *uint64 `json:"workerID,omitempty"`
	IP       string  `json:"ip,omitempty"`
	LastTime int64   `json:"lastTime"`
	Working  bool    `json:"working,omitempty"`
}

Jump to

Keyboard shortcuts

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