Documentation ¶
Index ¶
Constants ¶
View Source
const ( SequenceBits = 12 WorkerIDBits = 13 TimeUnitBits = 36 ClockBackwardsBits = 2 WorkIDMask = 1<<WorkerIDBits - 1 MaxSeqID = (1 << SequenceBits) - 1 TimestampShift = WorkerIDBits + SequenceBits MaxTimeUnits = (1 << TimeUnitBits) - 1 BackwardsMaskShift = TimeUnitBits + WorkerIDBits + SequenceBits TimeUnit = int64(time.Millisecond * 10) // 厘秒(10毫秒) CustomEpoch = int64(1704038400 * time.Second) // 起始纪元 2024-01-01 00:00:00 UTC )
一个64位UUID由以下部分组成
1位符号位 2位时钟回拨标记,时钟最多被回拨3次 36位时间戳(厘秒),~=21y288d, 最大可以表示到2045-10-10 13位服务器ID,最大服务器ID=8191 12位序列号,单个时间单位的最大分配数量(4096/毫秒)
View Source
const (
CollectionName = "uuid"
)
View Source
const (
DefaultSeqIDStep = 2000 // 默认步长
)
Variables ¶
View Source
var ( ErrClockGoneBackwards = errors.New("clock gone backwards") ErrTimeUnitOverflow = errors.New("clock time unit overflow") ErrUUIDIntOverflow = errors.New("uuid integer overflow") )
View Source
var ErrCounterOutOfRange = errors.New("counter out of range")
Functions ¶
This section is empty.
Types ¶
type CounterStore ¶
type CounterStore interface { Init(context.Context) error Close() error Incr(context.Context) (int64, error) }
CounterStore 表示一个存储组件,维持一个持续递增(不一定连续)的counter
func NewMongoDBCounter ¶
func NewMongoDBCounter(uri, db, label string) CounterStore
func NewRedisCounter ¶
func NewRedisCounter(addr, key string) CounterStore
type IDGenerator ¶
IDGenerator ID生成器
type MongoDBCounter ¶
type MongoDBCounter struct {
// contains filtered or unexported fields
}
MongoDBCounter 使用MongoDB的自增计数器
func (*MongoDBCounter) Close ¶
func (s *MongoDBCounter) Close() error
func (*MongoDBCounter) IncrementAndLoad ¶ added in v1.0.3
IncrementAndLoad 把counter自增再读取最新的counter https://docs.mongodb.com/manual/core/write-operations-atomicity/
type RedisCounter ¶
type RedisCounter struct {
// contains filtered or unexported fields
}
RedisCounter 基于redis INCR命令实现的计数器
func (*RedisCounter) Close ¶
func (s *RedisCounter) Close() error
type SegmentIDGen ¶
type SegmentIDGen struct {
// contains filtered or unexported fields
}
func NewSegmentIDGen ¶
func NewSegmentIDGen(store CounterStore, step int32) *SegmentIDGen
type Snowflake ¶
type Snowflake struct {
// contains filtered or unexported fields
}
func NewSnowflake ¶
type StorageType ¶
type StorageType int8
const ( StorageMongoDB StorageType = 1 StorageRedis StorageType = 2 )
Click to show internal directories.
Click to hide internal directories.