From a437c31751c546a46cb4a037fb975ac351fe1580 Mon Sep 17 00:00:00 2001 From: zk Date: Fri, 24 Jul 2026 18:42:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0id=20=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/id_gen.py | 14 ++++++++++++++ requirements.txt | 1 + 2 files changed, 15 insertions(+) create mode 100644 app/core/id_gen.py diff --git a/app/core/id_gen.py b/app/core/id_gen.py new file mode 100644 index 0000000..852abab --- /dev/null +++ b/app/core/id_gen.py @@ -0,0 +1,14 @@ +"""雪花 ID 生成 + +全项目共用一个生成器实例。instance 号用于区分不同服务, +避免多端 ID 冲突(job_cleaner 已占用 1、2,spider 用 3)。 +""" + +from snowflake import SnowflakeGenerator + +_id_gen = SnowflakeGenerator(instance=3) + + +def next_id() -> int: + """生成下一个雪花 ID""" + return next(_id_gen) diff --git a/requirements.txt b/requirements.txt index ca5727b..51510c5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ langchain-core>=0.3 # 工具 loguru>=0.7 +snowflake-id>=1.0