snowflake

package
v0.0.0-...-cb7ffae Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MachineIDBits  uint8 = 10
	SeqBits        uint8 = 12
	MachineIDShift       = SeqBits
	TimeShift            = SeqBits + MachineIDBits
	MachineIDMax   int   = -1 ^ (-1 << MachineIDBits)
	SeqMask        int32 = -1 ^ (-1 << SeqBits)
)

1+41+10+12

Variables

View Source
var (
	ErrClockMoveBackwards = errors.New("system clock move backwards")
	ErrInvalidMachineID   = errors.New("invalid machine id")
)

Functions

This section is empty.

Types

type Factory

type Factory interface {
	Next() (int64, error)
}

func NewFactory

func NewFactory(machineID int) (Factory, error)

不支持多routine并发

type Metadata

type Metadata struct {
	Name      string // 服务名称
	Addr      string // 监听IP:PORT
	MachineID int    // 机器ID 0 - 1023
	Timestamp int64  // 最后更新的时间
}

type Service

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

func NewService

func NewService(name, addr string, storage Storage, logger log.Logger) *Service

func (*Service) Close

func (s *Service) Close() error

func (*Service) Get

func (s *Service) Get(ctx context.Context, biztag string, count int) (ids []int64, err error)

type Storage

type Storage interface {
	// 查找指定的metadata,不存在则创建并返回合适的machineID
	GetOrNew(ctx context.Context, name, addr string) (md Metadata, err error)
	// 获取所有的metadata
	List(ctx context.Context) (mds []Metadata, err error)
	// 更新时间戳
	Update(ctx context.Context, md Metadata) (err error)
}

func NewRedisStorage

func NewRedisStorage(p *redis.Pool, logger log.Logger) Storage

Jump to

Keyboard shortcuts

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