From 55825bae4bacf15288b794ce120a717d5303ec05 Mon Sep 17 00:00:00 2001 From: Nishaad Ajani Date: Wed, 10 Apr 2019 17:02:30 +0800 Subject: [PATCH] Bug fix and optimization * Fix for default Epoch to use monotonic clock. * Using common case optimization in the time pkg. --- snowflake.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/snowflake.go b/snowflake.go index 2f60570..02b0606 100644 --- a/snowflake.go +++ b/snowflake.go @@ -12,9 +12,11 @@ import ( ) var ( + curTime = time.Now() // Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC // You may customize this to set a different epoch for your application. - Epoch = time.Date(2010, time.November, 4, 1, 42, 54, 0, time.UTC) + // Note: add time.Duration to time.Now() to make sure we use the monotonic clock if available. + Epoch = curTime.Add(time.Date(2010, time.November, 4, 1, 42, 54, 0, time.UTC).Sub(curTime)) // Number of bits to use for Node // Remember, you have a total 22 bits to share between Node/Step @@ -108,14 +110,14 @@ func (n *Node) Generate() ID { n.mu.Lock() - now := time.Now().Sub(Epoch) + now := time.Since(Epoch) if now-n.time < time.Millisecond { n.step = (n.step + 1) & stepMask if n.step == 0 { for now-n.time < time.Millisecond { - now = time.Now().Sub(Epoch) + now = time.Since(Epoch) } } } else {