Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ShardPool ¶
type ShardPool struct {
// contains filtered or unexported fields
}
ShardPool 按物理核心数分片的 Snowflake 池,每个分片独立加锁,消除竞争。 物理核心数比逻辑核心数更准确,超线程共享执行单元,分片数对齐物理核心竞争最低。
ShardPool is a sharded pool of Snowflake generators, one shard per physical CPU core. Each shard has its own lock, eliminating cross-shard contention. Physical cores are preferred over logical cores because hyper-threads share execution units; aligning shard count to physical cores minimises lock contention.
func NewShardPool ¶
NewShardPool 创建分片池,baseID 为机器ID基础值,分片数等于物理核心数。 每个分片的 machineID = (baseID + shardIndex) & 0xFFF,保证全局唯一。
NewShardPool creates a shard pool. baseID is the base machine ID; each shard gets machineID = (baseID + shardIndex) & 0xFFF, ensuring global uniqueness.
type ShardPool2 ¶
type ShardPool2 struct {
// contains filtered or unexported fields
}
ShardPool2 本项目2的分片池(序列号8位,时间戳43位,~278年) ShardPool2 is the shard pool for Snowflake2 (8-bit sequence, 43-bit timestamp, ~278 years).
func NewShardPool2 ¶
func NewShardPool2(baseID int64) (*ShardPool2, error)
NewShardPool2 创建 Snowflake2 分片池,baseID 为机器ID基础值。 NewShardPool2 creates a shard pool for Snowflake2; baseID is the base machine ID.
func (*ShardPool2) NextID ¶
func (p *ShardPool2) NextID(idx int64) (int64, error)
NextID 根据调用方索引路由到对应分片生成 ID。 NextID routes to the shard at idx % size and generates an ID.
func (*ShardPool2) Size ¶
func (p *ShardPool2) Size() int64
Size 返回分片数(等于物理核心数)。 Size returns the number of shards (equals physical core count).
type ShardPool3 ¶
type ShardPool3 struct {
// contains filtered or unexported fields
}
ShardPool3 本项目3的分片池(序列号9位,时间戳42位,~139年) ShardPool3 is the shard pool for Snowflake3 (9-bit sequence, 42-bit timestamp, ~139 years).
func NewShardPool3 ¶
func NewShardPool3(baseID int64) (*ShardPool3, error)
NewShardPool3 创建 Snowflake3 分片池,baseID 为机器ID基础值。 NewShardPool3 creates a shard pool for Snowflake3; baseID is the base machine ID.
func (*ShardPool3) NextID ¶
func (p *ShardPool3) NextID(idx int64) (int64, error)
NextID 根据调用方索引路由到对应分片生成 ID。 NextID routes to the shard at idx % size and generates an ID.
func (*ShardPool3) Size ¶
func (p *ShardPool3) Size() int64
Size 返回分片数(等于物理核心数)。 Size returns the number of shards (equals physical core count).
type Snowflake ¶
type Snowflake struct {
// contains filtered or unexported fields
}
Snowflake 分布式ID生成器(毫秒精度) Snowflake is a distributed ID generator with millisecond precision.
func NewSnowflake ¶
NewSnowflake 创建生成器,machineID 范围 [0, 4095] NewSnowflake creates a generator; machineID must be in [0, 4095].
func NewSnowflakeAuto ¶
NewSnowflakeAuto 自动从本机MAC地址派生 machineID NewSnowflakeAuto derives the machineID automatically from the local MAC address.
type Snowflake2 ¶
type Snowflake2 struct {
// contains filtered or unexported fields
}
Snowflake2 压缩序列号版本:序列号8位,时间戳43位,可用约278年 Snowflake2 trades sequence bits for a longer timestamp: 8-bit sequence (256/ms), 43-bit timestamp (~278 years).
func NewSnowflake2 ¶
func NewSnowflake2(machineID int64) (*Snowflake2, error)
NewSnowflake2 创建 Snowflake2 生成器,machineID 范围 [0, 4095] NewSnowflake2 creates a Snowflake2 generator; machineID must be in [0, 4095].
func (*Snowflake2) MachineID ¶
func (s *Snowflake2) MachineID() int64
MachineID 返回当前实例使用的机器ID MachineID returns the machine ID used by this instance.
func (*Snowflake2) NextID ¶
func (s *Snowflake2) NextID() (int64, error)
NextID 生成下一个唯一ID NextID generates the next unique ID.
type Snowflake3 ¶
type Snowflake3 struct {
// contains filtered or unexported fields
}
Snowflake3 序列号9位版本:时间戳42位,可用约139年,每ms最多512个ID Snowflake3 is a balanced variant: 9-bit sequence (512/ms), 42-bit timestamp (~139 years).
func NewSnowflake3 ¶
func NewSnowflake3(machineID int64) (*Snowflake3, error)
NewSnowflake3 创建 Snowflake3 生成器,machineID 范围 [0, 4095] NewSnowflake3 creates a Snowflake3 generator; machineID must be in [0, 4095].
func (*Snowflake3) MachineID ¶
func (s *Snowflake3) MachineID() int64
MachineID 返回当前实例使用的机器ID MachineID returns the machine ID used by this instance.
func (*Snowflake3) NextID ¶
func (s *Snowflake3) NextID() (int64, error)
NextID 生成下一个唯一ID NextID generates the next unique ID.
type SonyflakeCompat ¶
type SonyflakeCompat struct {
// contains filtered or unexported fields
}
func NewSonyflakeCompat ¶
func NewSonyflakeCompat(machineID int64) *SonyflakeCompat
NewSonyflakeCompat 创建 SonyflakeCompat 实例,machineID 截取低12位。 NewSonyflakeCompat creates a SonyflakeCompat instance; only the low 12 bits of machineID are used.
func (*SonyflakeCompat) NextID ¶
func (s *SonyflakeCompat) NextID() (int64, error)
NextID 生成下一个唯一ID。 序列号耗尽时使用 sleep 让出调度(sony 风格),而非自旋持锁。
NextID generates the next unique ID. When the sequence is exhausted it sleeps (sony style) instead of spinning, releasing the lock so other goroutines can proceed.