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
- Variables
- func Init(workerId uint16, store Storage)
- func MustCreateGUID() string
- func NextID() int64
- func NextUUID() int64
- type Counter
- type EtcdStore
- type IDGenerator
- type MongoStore
- type MySQLStore
- type RedisStore
- type SeqID
- type SnowFlake
- type Storage
- func NewEtcdStore(ctx context.Context, cli *clientv3.Client, key string) Storage
- func NewMongoDBStore(ctx context.Context, uri, db, label string, step int32) Storage
- func NewMySQLStore(ctx context.Context, dsn, table, label string, step int) Storage
- func NewRedisStore(ctx context.Context, addr, key string) Storage
- type UUID
Constants ¶
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) )
const ( ReservedNCS byte = 0x80 ReservedRFC4122 byte = 0x40 ReservedMicrosoft byte = 0x20 ReservedFuture byte = 0x00 )
The UUID reserved variants.
const (
DefaultSeqStep = 2000 // 默认每个counter分配2000个号
)
const OpTimeout = 300
DB超时
const (
UUIDCollection = "uuid"
)
Variables ¶
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().
var (
ErrCannotPutEtcd = errors.New("cannot put counter to etcd")
)
var ErrIDOutOfRange = errors.New("ID out of range")
var ErrNoRowsAffected = errors.New("no rows affected")
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 ¶ added in v1.2.1
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 MySQLStore ¶
type MySQLStore struct {
// contains filtered or unexported fields
}
func (*MySQLStore) Close ¶
func (s *MySQLStore) Close() error
func (*MySQLStore) Incr ¶
func (s *MySQLStore) 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 SeqID ¶
type SeqID struct {
// contains filtered or unexported fields
}
使用发号器算法的uuid生成器 1. 算法把一个64位的整数按step范围划分为N个号段; 2. service从发号器拿到号段后才可分配此号段内的ID; 3. 发号器依赖存储(etcd, redis)把当前待分配号段持续自增;
type Storage ¶
Storage表示一个存储组件,维持一个持续递增(不一定连续)的counter
func NewEtcdStore ¶
func NewMongoDBStore ¶
func NewMySQLStore ¶
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")