From 6d5272f95ee761594ba12acfcccd27b333782b04 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Sat, 26 Oct 2024 16:35:09 +0800 Subject: [PATCH] fix: uuid v7 monotonicity in rare case --- version7.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/version7.go b/version7.go index 3fec671..d866697 100644 --- a/version7.go +++ b/version7.go @@ -95,9 +95,16 @@ func getV7Time() (milli, seq int64) { seq = (nano - milli*nanoPerMilli) >> 8 now := milli<<12 + seq if now <= lastV7time { - now = lastV7time + 1 - milli = now >> 12 - seq = now & 0xfff + // uuid ver bits may eat seq carry + if seq == 0xfff { + milli = milli + 1 + seq = 0 + now = milli<<12 + seq + } else { + now = lastV7time + 1 + milli = now >> 12 + seq = now & 0xfff + } } lastV7time = now return milli, seq