Documentation
¶
Overview ¶
Package idgen 提供高性能的 ID 生成能力,支持多种 ID 生成策略:
- Generator: 基于雪花算法的分布式有序 ID 生成接口
- UUID: UUID v7 生成(时间排序,适合数据库主键)
- Sequencer: 基于 Redis/Etcd 的分布式序列号生成器接口
- Allocator: 支持 Redis/Etcd 的 WorkerID 自动分配器
设计原则:
- 配置驱动: 统一使用 New(cfg, opts...) 模式
- 接口优先: 面向接口编程,便于切换底层实现
- 高性能: 优化热路径,无锁或低锁竞争
- 实例优先: 支持多实例共存
基本使用:
// Generator (Snowflake)
gen, _ := idgen.NewGenerator(&idgen.GeneratorConfig{WorkerID: 1})
id := gen.Next()
// UUID
uid := idgen.UUID()
// Sequencer
seq, _ := idgen.NewSequencer(&idgen.SequencerConfig{
KeyPrefix: "order:",
Step: 1,
}, idgen.WithRedisConnector(redisConn))
nextID, _ := seq.Next(ctx, "user:1")
// Allocator (自动分配 WorkerID)
allocator, _ := idgen.NewAllocator(&idgen.AllocatorConfig{
Driver: "redis",
MaxID: 512,
}, idgen.WithRedisConnector(redisConn))
workerID, _ := allocator.Allocate(ctx)
defer allocator.Stop()
go func() { if err := <-allocator.KeepAlive(ctx); err != nil { ... } }()
Index ¶
Constants ¶
View Source
const ( // MetricSnowflakeGenerated 雪花算法 ID 生成总数 (Counter) MetricSnowflakeGenerated = "idgen_snowflake_generated_total" // MetricSequenceGenerated 序列号生成总数 (Counter) MetricSequenceGenerated = "idgen_sequence_generated_total" )
Metrics 指标常量定义
Variables ¶
View Source
var ( // ErrConnectorNil 连接器为空 ErrConnectorNil = xerrors.New("idgen: connector is nil") // ErrWorkerIDExhausted WorkerID 已耗尽 ErrWorkerIDExhausted = xerrors.New("idgen: no available worker id") // ErrClockBackwards 时钟回拨超过限制 ErrClockBackwards = xerrors.New("idgen: clock moved backwards too much") // ErrInvalidInput 无效的输入 ErrInvalidInput = xerrors.New("idgen: invalid input") // ErrLeaseExpired Etcd Lease 已过期 ErrLeaseExpired = xerrors.New("idgen: lease expired") )
Functions ¶
func ParseGeneratorID ¶
ParseGeneratorID 解析 Snowflake ID,返回其组成部分
Types ¶
type Allocator ¶
type Allocator interface {
// Allocate 分配 WorkerID(阻塞直到分配成功)
Allocate(ctx context.Context) (int64, error)
// KeepAlive 保持租约(阻塞方法,应在 goroutine 中运行)
// 返回错误通道,保活失败时会发送错误
KeepAlive(ctx context.Context) <-chan error
// Stop 停止保活并释放资源
Stop()
}
Allocator WorkerID 分配器接口 用于在集群环境中自动分配唯一的 WorkerID,避免手动配置冲突
func NewAllocator ¶
func NewAllocator(cfg *AllocatorConfig, opts ...Option) (Allocator, error)
NewAllocator 创建 WorkerID 分配器 根据 cfg.Driver 选择 redis 或 etcd 实现
使用示例:
// Redis 分配器
allocator, _ := idgen.NewAllocator(&idgen.AllocatorConfig{
Driver: "redis",
MaxID: 512,
}, idgen.WithRedisConnector(redisConn))
workerID, _ := allocator.Allocate(ctx)
defer allocator.Stop()
go func() {
if err := <-allocator.KeepAlive(ctx); err != nil {
// 处理保活失败
}
}()
type AllocatorConfig ¶
type AllocatorConfig struct {
// Driver 后端类型: "redis" | "etcd"
Driver string `yaml:"driver" json:"driver"`
// KeyPrefix 键前缀,默认 "genesis:idgen:worker"
KeyPrefix string `yaml:"key_prefix" json:"key_prefix"`
// MaxID 最大 ID 范围 [0, maxID),默认 1024
MaxID int `yaml:"max_id" json:"max_id"`
// TTL 租约 TTL(秒),默认 30
TTL int `yaml:"ttl" json:"ttl"`
}
AllocatorConfig WorkerID 分配器配置
type Generator ¶
type Generator interface {
// Next 生成下一个 ID
Next() int64
// NextString 生成下一个 ID (字符串形式)
NextString() string
}
Generator ID 生成器接口 提供高性能的分布式 ID 生成能力
func NewGenerator ¶
func NewGenerator(cfg *GeneratorConfig, opts ...Option) (Generator, error)
NewGenerator 创建 ID 生成器 (Snowflake 实现)
使用示例:
gen, _ := idgen.NewGenerator(&idgen.GeneratorConfig{WorkerID: 1})
id := gen.Next()
type GeneratorConfig ¶
type GeneratorConfig struct {
// WorkerID 工作节点 ID [0, 1023]
WorkerID int64 `yaml:"worker_id" json:"worker_id"`
// DatacenterID 数据中心 ID [0, 31],可选
DatacenterID int64 `yaml:"datacenter_id" json:"datacenter_id"`
}
GeneratorConfig ID 生成器配置 (Snowflake)
type Option ¶
type Option func(*options)
Option 组件初始化选项函数
func WithEtcdConnector ¶
func WithEtcdConnector(conn connector.EtcdConnector) Option
WithEtcdConnector 注入 Etcd 连接器 目前仅用于 Allocator 组件
func WithRedisConnector ¶
func WithRedisConnector(conn connector.RedisConnector) Option
WithRedisConnector 注入 Redis 连接器 用于 Allocator、Sequencer 等组件
type Sequencer ¶
type Sequencer interface {
// Next 为指定键生成下一个序列号
Next(ctx context.Context, key string) (int64, error)
// NextBatch 为指定键批量生成序列号
NextBatch(ctx context.Context, key string, count int) ([]int64, error)
// Set 直接设置序列号的值
// 警告:此操作会覆盖现有值,请谨慎使用
Set(ctx context.Context, key string, value int64) error
// SetIfNotExists 仅当键不存在时设置序列号的值
// 返回 true 表示设置成功,false 表示键已存在
SetIfNotExists(ctx context.Context, key string, value int64) (bool, error)
}
Sequencer 序列号生成器接口 提供基于 Redis/Etcd 的分布式序列号生成能力
func NewSequencer ¶
func NewSequencer(cfg *SequencerConfig, opts ...Option) (Sequencer, error)
NewSequencer 创建序列号生成器(配置驱动,目前仅支持 Redis)
使用示例:
seq, _ := idgen.NewSequencer(&idgen.SequencerConfig{
KeyPrefix: "im:seq",
Step: 1,
TTL: 3600, // 1 hour (秒)
}, idgen.WithRedisConnector(redisConn))
// IM 场景使用
id, _ := seq.Next(ctx, "alice") // Alice 的消息序号
id, _ := seq.Next(ctx, "bob") // Bob 的消息序号
type SequencerConfig ¶
type SequencerConfig struct {
// Driver 后端类型: "redis" | "etcd",默认 "redis"
Driver string `yaml:"driver" json:"driver"`
// KeyPrefix 键前缀
KeyPrefix string `yaml:"key_prefix" json:"key_prefix"`
// Step 步长,默认为 1
Step int64 `yaml:"step" json:"step"`
// MaxValue 最大值限制,达到后循环(0 表示不限制)
MaxValue int64 `yaml:"max_value" json:"max_value"`
// TTL 键过期时间(秒),0 表示永不过期
TTL int64 `yaml:"ttl" json:"ttl"`
}
SequencerConfig 序列号生成器配置
Click to show internal directories.
Click to hide internal directories.