From a0e1763cf0b1dd0c445bbfcf9f7f51d639da37e8 Mon Sep 17 00:00:00 2001 From: "larry.nung" Date: Wed, 6 May 2020 23:24:49 +0800 Subject: [PATCH] fix: InvalidSystemClockException in concurrent --- IdGen/IdGenerator.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/IdGen/IdGenerator.cs b/IdGen/IdGenerator.cs index c2b1b6b..3282e0b 100644 --- a/IdGen/IdGenerator.cs +++ b/IdGen/IdGenerator.cs @@ -205,12 +205,12 @@ public bool TryCreateId(out long id) /// Thrown when sequence overflows. private long CreateIdImpl(out Exception exception) { - // Determine "timeslot" and make sure it's >= last timeslot (if any) - var ticks = GetTicks(); - var timestamp = ticks & MASK_TIME; - lock (_genlock) { + // Determine "timeslot" and make sure it's >= last timeslot (if any) + var ticks = GetTicks(); + var timestamp = ticks & MASK_TIME; + if (timestamp < _lastgen || ticks < 0) { exception = new InvalidSystemClockException($"Clock moved backwards or wrapped around. Refusing to generate id for {_lastgen - timestamp} ticks");