From a089ba7ed0ef853f897a9109da62dba0329b77de Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 27 Jul 2023 19:06:22 +0800 Subject: [PATCH] Snowflake ID time rollback process --- .../SqlSugar/DistributedSystem/Snowflake/IdWorker.cs | 6 +++++- Src/Asp.NetCore2/SqlSugar/Infrastructure/StaticConfig.cs | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Src/Asp.NetCore2/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs b/Src/Asp.NetCore2/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs index 347b4efda..bf2aa5d84 100644 --- a/Src/Asp.NetCore2/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs +++ b/Src/Asp.NetCore2/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs @@ -81,10 +81,14 @@ namespace SqlSugar.DistributedSystem.Snowflake if (timestamp < _lastTimestamp) { + if (StaticConfig.CustomSnowFlakeTimeErrorFunc != null) + { + return StaticConfig.CustomSnowFlakeTimeErrorFunc(); + } //exceptionCounter.incr(1); //log.Error("clock is moving backwards. Rejecting requests until %d.", _lastTimestamp); throw new InvalidSystemClock(String.Format( - "Clock moved backwards. Refusing to generate id for {0} milliseconds", _lastTimestamp - timestamp)); + "服务器时间出现回退你可以使用StaticConfig.CustomSnowFlakeTimeErrorFunc=【自定义方法】处理让他不报错返回新ID,Clock moved backwards. Refusing to generate id for {0} milliseconds", _lastTimestamp - timestamp)); } if (_lastTimestamp == timestamp) diff --git a/Src/Asp.NetCore2/SqlSugar/Infrastructure/StaticConfig.cs b/Src/Asp.NetCore2/SqlSugar/Infrastructure/StaticConfig.cs index f9819ef2c..a42aaecb0 100644 --- a/Src/Asp.NetCore2/SqlSugar/Infrastructure/StaticConfig.cs +++ b/Src/Asp.NetCore2/SqlSugar/Infrastructure/StaticConfig.cs @@ -13,6 +13,7 @@ namespace SqlSugar public const string CodeFirst_BigString = "varcharmax,longtext,text,clob"; public static Func CustomSnowFlakeFunc; + public static Func CustomSnowFlakeTimeErrorFunc; public static Action CompleteQueryableFunc; public static Action CompleteInsertableFunc;