惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
博客园_首页
大猫的无限游戏
大猫的无限游戏
Recent Announcements
Recent Announcements
量子位
H
Help Net Security
D
Docker
小众软件
小众软件
Google DeepMind News
Google DeepMind News
U
Unit 42
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
S
SegmentFault 最新的问题
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
Martin Fowler
Martin Fowler
D
DataBreaches.Net
AI
AI
SecWiki News
SecWiki News
V
Visual Studio Blog
Google Online Security Blog
Google Online Security Blog
腾讯CDC
J
Java Code Geeks
Jina AI
Jina AI
O
OpenAI News
N
News | PayPal Newsroom
Help Net Security
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cloudbric
Cloudbric
S
Secure Thoughts
V
V2EX
N
News and Events Feed by Topic
F
Full Disclosure
MyScale Blog
MyScale Blog
The Cloudflare Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Forbes - Security
Forbes - Security
T
Troy Hunt's Blog
WordPress大学
WordPress大学
H
Hacker News: Front Page
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

博客园 - 干炸小黄鱼

timex 处理时间戳 gorm-gen golang每日一库--协程池库ants golang每日一库--json解析库gjson python高级编程-asyncio python高级编程-condition python高级编程-event python装饰器-自动重试 EAP系统 go实现实现 SECS/GEM 协议 设备通信协议 SECS go项目使用Jenkins进行CICD go操作ES mongo db聚合查询 go如何使用mongodb Apache ShardingSphere paxos and raft (分布式一致性算法) go使用zookeeper分布式锁以及和redis差异 go使用 seata 示例 Alibaba 分布式事务 Seata go中使用saga go中使用TCC示例 分布式事务TCC 熔断器 Hystrix OR Sentinel k8s下部署consul and etcd Consul OR Etcd 【力扣hot100】双指针-盛水最多的容器 【力扣hot100】滑动窗口-最小覆盖子串 shell脚本合集 分布式id生成器 springboot通用CURD Python PB级检索系统架构设计 rancher 在三台机器搭建k8s集群 python ssh clinet 数据库排序Null值字段靠后/靠前 常规web项目 docker-compose 例子 手搓一个验证码 使用itertools 中的groupby 对字典数组进行分组后排序 使用开源库 geoip2 获取某ip的经纬度地理信息 python中 apscheduler.schedulers.blocking.BlockingScheduler 定时执行任务 简单的python web项目的docker-compose.yml 示例 python和sliver交互 golang sliver二次开发自定义命令(格式乱后面再调) pydantic做参数校验 基于rancher部署k8s 地理位置相关基础数据 flask migrate时报错 Can't locate revision identified by '3d80e4c025df'
go 雪花算法
干炸小黄鱼 · 2026-06-04 · via 博客园 - 干炸小黄鱼
machine.go
package snowf

type Machine interface {
	// GetID 获取机器 ID (0 <= ID < 1024)
	GetID() int64
}

machine_redis.go

package snowf

import (
	"fmt"
	"github.com/go-redis/redis"
	"sync"
	"time"
)

const (
	MachineIDLockPrefix = "machine-id-lock"
	MachineIncrPrefix   = "machine-id-incr"
)

type MachineRedis struct {
	redisClient   *redis.Client
	serverName    string
	machineID     int64
	machineIDOnce sync.Once
}

func (m *MachineRedis) GetID() int64 {
	if nil == m.redisClient || "" == m.serverName {
		panic("machine not initial")
	}

	m.machineIDOnce.Do(func() {
		redisClient := m.redisClient
		for {
			// 尝试获取 Redis 锁
			lockKey := fmt.Sprintf("%s:%s", MachineIDLockPrefix, m.serverName)
			locked, err := redisClient.SetNX(lockKey, 1, 1*time.Second).Result()
			if err != nil {
				time.Sleep(100 * time.Millisecond)
				continue
			}

			if locked {
				// 获取锁成功,执行自增操作
				incrCmd := redisClient.Incr(fmt.Sprintf("%s:%s", MachineIncrPrefix, m.serverName))
				m.machineID = incrCmd.Val() % 1024
				// 释放锁
				redisClient.Del(lockKey)
				break
			}

			// 获取锁失败,等待一段时间后重试
			time.Sleep(100 * time.Millisecond)
		}
	})

	return m.machineID
}

func NewMachineRedis(redisClient *redis.Client, serverName string) *MachineRedis {
	return &MachineRedis{
		redisClient: redisClient,
		serverName:  serverName,
	}
}

snowflake.go

package snowf

import (
	"errors"
	"github.com/bwmarrin/snowflake"
	"sync"
	"time"
)

type Snowflake struct {
	Machine Machine
	Valid   bool
	nodes   map[string]*snowflake.Node
	mu      sync.RWMutex
}

// InitPartitionNodes 初始化其他分区,防止并发情况下GetPartitionNode出现竞争
func InitPartitionNodes(partitions ...string) error {
	if !currentClient.Valid {
		return errors.New("service is not initial")
	}
	for _, partition := range partitions {
		_, err := currentClient.InitPartitionNode(partition)
		if err != nil {
			return err
		}
	}

	return nil
}

func (sf *Snowflake) InitPartitionNode(partition string) (node *snowflake.Node, err error) {
	sf.mu.Lock()
	defer sf.mu.Unlock()

	node, err = snowflake.NewNode(sf.Machine.GetID())
	if err != nil {
		return nil, err
	}

	sf.nodes[partition] = node
	return node, nil
}

func (sf *Snowflake) GetPartitionNode(partition string) (node *snowflake.Node, err error) {
	node, ok := sf.nodes[partition]
	if !ok {
		node, err = sf.InitPartitionNode(partition)
		if err != nil {
			return nil, err
		}
	}

	return node, nil
}

var currentClient *Snowflake

func InitSnowflake(machine Machine, partitions ...string) (err error) {
	currentClient = &Snowflake{
		Machine: machine,
		nodes:   make(map[string]*snowflake.Node),
	}

	partition := "default"
	if len(partitions) > 0 {
		partition = partitions[0]
	}

	_, err = currentClient.InitPartitionNode(partition)
	if err != nil {
		return err
	}
	currentClient.Valid = true
	return nil
}

// GetSnowID
// Deprecated: recommend to use GetIDInt64 method
func GetSnowID(partitions ...string) int64 {
	return int64(GetID(partitions...))
}

func GetIDInt64(partitions ...string) int64 {
	return int64(GetID(partitions...))
}

func GetID(partitions ...string) ID {
	if !currentClient.Valid {
		panic("service is not initial")
	}

	partition := "default"
	if len(partitions) > 0 {
		partition = partitions[0]
	}

	node, err := currentClient.GetPartitionNode(partition)
	if err != nil {
		// 服务降级
		return ID(time.Now().UnixNano())
	}
	return ID(node.Generate())
}

showid.go

package snowf

import (
	"database/sql/driver"
	"encoding/json"
	"fmt"
	"github.com/bwmarrin/snowflake"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"strconv"
	"time"
)

// ID is a custom type for IDs that should be serialized as a string in JSON.
type ID int64

// GormDataType specifies the GORM data type for ID.
func (ID) GormDataType() string {
	return "bigint" // Use the appropriate data type for your database.
}

// GormDBDataType specifies the GORM database data type for ID.
func (ID) GormDBDataType(db *gorm.DB, field *schema.Field) string {
	switch db.Dialector.Name() {
	case "sqlite", "mysql":
		return "BIGINT"
	case "postgres":
		return "BIGINT"
	// Add other databases as needed.
	default:
		return "BIGINT"
	}
}

// MarshalJSON customizes the JSON encoding of ID to be a string.
func (sid ID) MarshalJSON() ([]byte, error) {
	strID := strconv.FormatInt(int64(sid), 10)
	return []byte(`"` + strID + `"`), nil
}

// UnmarshalJSON customizes the JSON decoding of ID from a string.
func (sid *ID) UnmarshalJSON(data []byte) error {
	var s string
	if err := json.Unmarshal(data, &s); err != nil {
		return err
	}
	if s != "" {
		id, err := strconv.ParseInt(s, 10, 64)
		if err != nil {
			return fmt.Errorf("failed to parse ID from JSON: %w", err)
		}
		*sid = ID(id)
	}
	return nil
}

// Value returns the ID as a driver.Value, which is required by GORM.
func (sid ID) Value() (driver.Value, error) {
	return int64(sid), nil
}

// Scan scans the value from the database driver into ID, which is required by GORM.
func (sid *ID) Scan(value interface{}) error {
	var id int64
	switch v := value.(type) {
	case int64:
		id = v
	case []byte:
		id, _ = strconv.ParseInt(string(v), 10, 64)
	case string:
		id, _ = strconv.ParseInt(v, 10, 64)
	default:
		return fmt.Errorf("unsupported type: %T", value)
	}
	*sid = ID(id)
	return nil
}

func (sid ID) String() string {
	return strconv.FormatInt(int64(sid), 10)
}

func (sid ID) IsValid() bool {
	return int64(sid) > 0
}

func (sid ID) Time() time.Time {
	timestamp := int64(sid) >> (snowflake.NodeBits + snowflake.StepBits)
	return time.Unix(snowflake.Epoch/1000+timestamp/1000, (timestamp%1000)*1000000).UTC()
}