From 4c4a27940a6bb74cfd71184ff91a5e06d1e1ae2c Mon Sep 17 00:00:00 2001 From: Nishaad Ajani Date: Thu, 11 Apr 2019 13:03:43 +0800 Subject: [PATCH] Optimize code --- snowflake.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/snowflake.go b/snowflake.go index 614c3ab..abe4846 100644 --- a/snowflake.go +++ b/snowflake.go @@ -79,7 +79,7 @@ var ErrInvalidBase32 = errors.New("invalid base32") type Node struct { mu sync.Mutex epoch time.Time - time time.Duration + time int64 node int64 step int64 @@ -132,14 +132,14 @@ func (n *Node) Generate() ID { n.mu.Lock() - now := time.Since(n.epoch) + now := time.Since(n.epoch).Nanoseconds() / 1000000 - if now-n.time < time.Millisecond { + if now == n.time { n.step = (n.step + 1) & n.stepMask if n.step == 0 { - for now-n.time < time.Millisecond { - now = time.Since(n.epoch) + for now <= n.time { + now = time.Since(n.epoch).Nanoseconds() / 1000000 } } } else { @@ -148,7 +148,7 @@ func (n *Node) Generate() ID { n.time = now - r := ID((now.Nanoseconds()/1000000)<