1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00

snow flake

This commit is contained in:
2022-07-25 14:51:16 +08:00
parent 886723997e
commit 37e9cabde8
2 changed files with 87 additions and 19 deletions

View File

@ -1,20 +1,22 @@
package snowflake package snowflake
import ( import (
"fmt"
"log"
"sync" "sync"
"time" "time"
) )
// 雪花算法默认起始时间 2020-01-01 // 雪花算法默认起始时间 2022-01-01
const defaultStarTimestamp = 1579536000 const defaultStarTimestamp = 1640966400000
const ( const (
MachineIdBits = uint(8) //机器id所占的位数 MachineIdBits = uint(8) //机器id所占的位数
SequenceBits = uint(12) //序列所占的位数 SequenceBits = uint(12) //序列所占的位数
//MachineIdMax = int64(-1 ^ (-1 << MachineIdBits)) //支持的最大机器id数量 MachineIdMax = int64(-1 ^ (-1 << MachineIdBits)) //支持的最大机器id数量
SequenceMask = int64(-1 ^ (-1 << SequenceBits)) // SequenceMask = int64(-1 ^ (-1 << SequenceBits)) //
MachineIdShift = SequenceBits //机器id左移位数 MachineIdShift = SequenceBits //机器id左移位数
TimestampShift = SequenceBits + MachineIdBits //时间戳左移位数 TimestampShift = SequenceBits + MachineIdBits //时间戳左移位数
) )
type SnowFlake interface { type SnowFlake interface {
@ -30,28 +32,42 @@ type snowflake struct {
} }
func CreateSnowflake(machineId int64) SnowFlake { func CreateSnowflake(machineId int64) SnowFlake {
timeBits := 63 - MachineIdBits - SequenceBits
maxTime := time.UnixMilli(defaultStarTimestamp + (int64(-1 ^ (-1 << timeBits))))
log.Println("最大可用时间:", maxTime)
return &snowflake{ return &snowflake{
startTimeStamp: defaultStarTimestamp, startTimeStamp: defaultStarTimestamp,
machineId: machineId, machineId: machineId & MachineIdMax,
} }
} }
// 组织方式 时间戳-机器码-序列号
func (s *snowflake) GetId() int64 { func (s *snowflake) GetId() int64 {
// 生成序列号规则
// 检查当前生成时间与上次生成时间对比
// 如等于上次生成时间,检查是否已经达到序列号的最大值,如已达到等待下一个时间点并且设置序列号为零。
// 如不相等则序列号自增
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
now := time.Now().UnixNano() / 1e6 now := time.Now().UnixMilli()
if s.timestamp == now { if s.timestamp == now && s.sequence == 0 {
s.sequence = (s.sequence + 1) & SequenceMask fmt.Println(time.Now().Format("2006-01-02 15:04:05.000"), "下一个时间点")
if s.sequence == 0 { for now <= s.timestamp {
for now <= s.timestamp { now = time.Now().UnixMilli()
now = time.Now().UnixNano() / 1e6
}
} }
} else {
s.sequence = 0
} }
s.timestamp = now s.timestamp = now
s.sequence = (s.sequence + 1) & SequenceMask
log.Println("时间戳:", now-s.startTimeStamp)
log.Println("时间差:", time.Now().Sub(time.UnixMilli(defaultStarTimestamp)))
r := (now-s.startTimeStamp)<<TimestampShift | (s.machineId << MachineIdShift) | (s.sequence) r := (now-s.startTimeStamp)<<TimestampShift | (s.machineId << MachineIdShift) | (s.sequence)
return r return r
} }

View File

@ -1,6 +1,15 @@
package snowflake package snowflake
import "testing" import (
"testing"
"github.com/charlienet/go-mixed/sets"
)
func TestGet(t *testing.T) {
s := CreateSnowflake(2)
t.Log(s.GetId())
}
func TestGetId(t *testing.T) { func TestGetId(t *testing.T) {
s := CreateSnowflake(22) s := CreateSnowflake(22)
@ -8,3 +17,46 @@ func TestGetId(t *testing.T) {
t.Log(s.GetId()) t.Log(s.GetId())
} }
} }
func TestMutiGetId(t *testing.T) {
s := CreateSnowflake(11)
for i := 0; i < 100000; i++ {
s.GetId()
}
}
func TestMutiConflict(t *testing.T) {
set := sets.NewHashSet[int64]()
s := CreateSnowflake(11)
for i := 0; i < 10000000; i++ {
id := s.GetId()
if set.Contains(id) {
t.Fatal("失败,生成重复数据")
}
set.Add(id)
}
}
func BenchmarkGetId(b *testing.B) {
s := CreateSnowflake(11)
for i := 0; i < b.N; i++ {
s.GetId()
}
}
func BenchmarkMutiGetId(b *testing.B) {
s := CreateSnowflake(11)
set := sets.NewHashSet[int64]().WithSync()
b.RunParallel(func(p *testing.PB) {
for i := 0; p.Next(); i++ {
id := s.GetId()
if set.Contains(id) {
b.Fatal("标识重复", id)
}
set.Add(id)
}
})
}