Documentation ¶
Index ¶
Constants ¶
View Source
const ( SequenceBits = 10 MachineIDBits = 14 TimeUnitBits = 37 MaxSeqID = (1 << SequenceBits) - 1 MachineIDMask = 0x3FFFF TimestampShift = MachineIDBits + SequenceBits MaxTimeUnits = (1 << TimeUnitBits) - 1 BackwardsMaskShift = TimeUnitBits + MachineIDBits + SequenceBits TimeUnit = int64(time.Second / 100) // 厘秒(10毫秒) CustomEpoch = int64(1577836800 * time.Second) // 起始纪元 2020-01-01 00:00:00 UTC )
View Source
const (
CollectionName = "uuid"
)
View Source
const (
DefaultSeqStep = 2000 // 默认每个counter分配2000个号
)
View Source
const OpTimeout = 3000
DB超时(毫秒)
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 (
ErrCannotPutEtcd = errors.New("cannot put counter to etcd")
)
View Source
var ErrIDOutOfRange = errors.New("ID out of range")
Functions ¶
Types ¶
type Counter ¶
type Counter struct { Label string `bson:"label"` // 识别符 Count int64 `bson:"count"` // 计数器 Step int32 `bson:"step"` // }
计数器
type EtcdStore ¶
type EtcdStore struct {
// contains filtered or unexported fields
}
使用etcd的key的版本号自增实现
type IDGenerator ¶
type MongoStore ¶
type MongoStore struct {
// contains filtered or unexported fields
}
func (*MongoStore) Close ¶
func (s *MongoStore) Close() error
func (*MongoStore) Incr ¶
func (s *MongoStore) Incr() (int64, error)
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
使用redis INCR命令实现
func (*RedisStore) Close ¶
func (s *RedisStore) Close() error
func (*RedisStore) Incr ¶
func (s *RedisStore) Incr() (int64, error)
type SeqIDGen ¶
type SeqIDGen struct {
// contains filtered or unexported fields
}
使用发号器算法的uuid生成器 1. 算法把一个64位的整数按step范围划分为N个号段; 2. service从发号器拿到号段后才可分配此号段内的ID; 3. 发号器依赖存储(etcd, redis等)把当前待分配号段持续自增;
func NewSeqIDGen ¶
type Snowflake ¶
type Snowflake struct {
// contains filtered or unexported fields
}
一个64位UUID由以下部分组成
1位符号位 2位时钟回拨标记,支持时钟被回拨3次 37位时间戳(厘秒),可以表示到2063-07-21 14位服务器ID 10位序列号,单个时间单位的最大分配数量
func NewSnowflake ¶
Click to show internal directories.
Click to hide internal directories.