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