Saving time as a duration in Node

This commit is contained in:
Nishaad Ajani 2019-04-03 14:57:50 +08:00
parent 0685b6ac31
commit 2983bd9d27

View File

@ -76,7 +76,7 @@ var ErrInvalidBase32 = errors.New("invalid base32")
// node // node
type Node struct { type Node struct {
mu sync.Mutex mu sync.Mutex
time time.Time time time.Duration
node int64 node int64
step int64 step int64
} }
@ -108,14 +108,14 @@ func (n *Node) Generate() ID {
n.mu.Lock() n.mu.Lock()
now := time.Now().Round(time.Millisecond) now := time.Now().Sub(Epoch)
if now.Sub(n.time) < time.Millisecond { if now-n.time < time.Millisecond {
n.step = (n.step + 1) & stepMask n.step = (n.step + 1) & stepMask
if n.step == 0 { if n.step == 0 {
for now.Sub(n.time) < time.Millisecond { for now-n.time < time.Millisecond {
now = time.Now().Round(time.Millisecond) now = time.Now().Sub(Epoch)
} }
} }
} else { } else {
@ -124,7 +124,7 @@ func (n *Node) Generate() ID {
n.time = now n.time = now
r := ID((now.Sub(Epoch).Nanoseconds()/1000000)<<timeShift | r := ID((now.Nanoseconds()/1000000)<<timeShift |
(n.node << nodeShift) | (n.node << nodeShift) |
(n.step), (n.step),
) )