Documentation ¶
Overview ¶
This package provides immutable UUID structs and the functions NewV3, NewV4, NewV5 and Parse() for generating versions 3, 4 and 5 UUIDs as specified in RFC 4122.
Copyright (C) 2011 by Krzysztof Kowalik <chris@nu7hat.ch>
Index ¶
Constants ¶
View Source
const ( SequenceBits = 10 MachineIDBits = 16 TimestampShift = SequenceBits + MachineIDBits TimeUnitBits = 63 - TimestampShift Twepoch = 1577836800_000_000_000 // custom epoch in nanosecond, (2020-01-01 00:00:00 UTC) )
View Source
const ( TimeoutSec = 3 MaxRetry = 2 )
View Source
const ( ReservedNCS byte = 0x80 ReservedRFC4122 byte = 0x40 ReservedMicrosoft byte = 0x20 ReservedFuture byte = 0x00 )
The UUID reserved variants.
View Source
const (
DefaultSeqStep = 2000 // 默认每个counter分配2000个号
)
View Source
const (
UUIDCollection = "uuid"
)
Variables ¶
View Source
var (
NamespaceDNS, _ = ParseHex("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
NamespaceURL, _ = ParseHex("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
NamespaceOID, _ = ParseHex("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
NamespaceX500, _ = ParseHex("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
)
The following standard UUIDs are for use with NewV3() or NewV5().
View Source
var (
ErrCannotPutEtcd = errors.New("cannot put counter to etcd")
)
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 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 SequenceID ¶
type SequenceID struct {
// contains filtered or unexported fields
}
使用发号器算法的uuid生成器 1. 算法把一个64位的整数按step范围划分为N个号段; 2. service从发号器拿到号段后才可分配此号段内的ID; 3. 发号器依赖存储(etcd, redis)把当前待分配号段持续自增;
func NewSequenceID ¶
func NewSequenceID(store Storage, step int32) *SequenceID
func (*SequenceID) Init ¶
func (s *SequenceID) Init() error
func (*SequenceID) MustNext ¶
func (s *SequenceID) MustNext() int64
func (*SequenceID) Next ¶
func (s *SequenceID) Next() (int64, error)
type UUID ¶
type UUID [16]byte
A UUID representation compliant with specification in RFC 4122 document.
func ParseHex ¶
ParseHex creates a UUID object from given hex string representation. Function accepts UUID string in following formats:
uuid.ParseHex("6ba7b814-9dad-11d1-80b4-00c04fd430c8") uuid.ParseHex("{6ba7b814-9dad-11d1-80b4-00c04fd430c8}") uuid.ParseHex("urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8")
Click to show internal directories.
Click to hide internal directories.