Skip to content

Commit

Permalink
Merge pull request #663 from ploxiln/time_div_optim
Browse files Browse the repository at this point in the history
nsqd: optimize guid generation
  • Loading branch information
mreiferson committed Oct 1, 2015
2 parents 223c53c + 8cf036a commit ba7c4b4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions nsqd/guid.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const (
timestampShift = sequenceBits + workerIDBits
sequenceMask = int64(-1) ^ (int64(-1) << sequenceBits)

// Thu Nov 4 01:42:54 UTC 2010
twepoch = int64(1288834974657)
// ( 2012-10-28 16:23:42 UTC ).UnixNano() >> 20
twepoch = int64(1288834974288)
)

var ErrTimeBackwards = errors.New("time has gone backwards")
Expand All @@ -39,7 +39,8 @@ type guidFactory struct {
}

func (f *guidFactory) NewGUID(workerID int64) (guid, error) {
ts := time.Now().UnixNano() / 1e6
// divide by 1048576, giving pseudo-milliseconds
ts := time.Now().UnixNano() >> 20

if ts < f.lastTimestamp {
return 0, ErrTimeBackwards
Expand Down

0 comments on commit ba7c4b4

Please sign in to comment.