Skip to content

Commit

Permalink
Move generator to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
shaxbee committed Apr 20, 2016
1 parent 852710f commit c1334de
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,43 @@ var (
// - 41 bits of timestamp
// - 10 bits of worker ID
// - 12 bits of sequence number
type SnowFlake <-chan int64
type Snowflake <-chan int64

// New constructs generator for snowflake IDs
// ErrInvalidWorkerID is returned if WorkerID is not within [0, 1024)
func New(workerID uint64) (SnowFlake, error) {
func New(workerID uint64) (Snowflake, error) {
if workerID < 0 || workerID > maxWorkerID {
return nil, ErrInvalidWorkerID
}

sf := make(chan int64)
go func() {
last := timestamp()
seq := uint64(0)
for {
ts := timestamp()
if ts < last {
ts = nextMillisec(last)
}
go generator(workerID, sf)
return sf, nil
}

if ts != last {
seq = 0
last = ts
} else if seq == maxSequence {
ts = nextMillisec(ts)
seq = 0
last = ts
} else {
seq++
}
func generator(workerID uint64, c chan<- int64) {
last := timestamp()
seq := uint64(0)
for {
ts := timestamp()
if ts < last {
ts = nextMillisec(last)
}

id := int64((ts << timestampOffset) | (workerID << workerIDOffset) | seq)
sf <- id
if ts != last {
seq = 0
last = ts
} else if seq == maxSequence {
ts = nextMillisec(ts)
seq = 0
last = ts
} else {
seq++
}
}()

return sf, nil
id := int64((ts << timestampOffset) | (workerID << workerIDOffset) | seq)
c <- id
}
}

func nextMillisec(ts uint64) uint64 {
Expand Down

0 comments on commit c1334de

Please sign in to comment.