Skip to content

Commit

Permalink
Saving time as a duration in Node
Browse files Browse the repository at this point in the history
  • Loading branch information
nishaad78 committed Apr 3, 2019
1 parent 75c07bb commit 7dd8375
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var ErrInvalidBase32 = errors.New("invalid base32")
// node
type Node struct {
mu sync.Mutex
time time.Time
time time.Duration
node int64
step int64
}
Expand Down Expand Up @@ -108,14 +108,14 @@ func (n *Node) Generate() ID {

n.mu.Lock()

now := time.Now()
now := time.Now().Sub(Epoch)

if now.Sub(n.time) < time.Millisecond {
if now-n.time < time.Millisecond {
n.step = (n.step + 1) & stepMask

if n.step == 0 {
for now.Sub(n.time) < time.Millisecond {
now = time.Now()
for now-n.time < time.Millisecond {
now = time.Now().Sub(Epoch)
}
}
} else {
Expand All @@ -124,7 +124,7 @@ func (n *Node) Generate() ID {

n.time = now

r := ID((now.Sub(Epoch).Nanoseconds()/1000000)<<timeShift |
r := ID((now.Nanoseconds()/1000000)<<timeShift |
(n.node << nodeShift) |
(n.step),
)
Expand Down

0 comments on commit 7dd8375

Please sign in to comment.