Added a channel version..

This commit is contained in:
Bruce Marriner 2016-05-25 15:40:25 -05:00
parent 63ab6161db
commit 4677b9c7e1
2 changed files with 34 additions and 0 deletions

View File

@ -54,8 +54,28 @@ func NewFlakeNode(node int64) (*Node, error) {
}
// high performance generator
// well, that w as the idea...
func (n *Node) Generator(c chan Flake) {
ticker := time.NewTicker(time.Millisecond)
now := int64(time.Now().UnixNano() / 1000000)
for {
n.step = 0
select {
case c <- Flake((now-n.epoch)<<TimeShift | (n.node << NodeShift) | (n.step)):
n.step = (n.step + 1) & StepMask
if n.step == 0 {
// wait for ticker..
}
case <-ticker.C:
now++
// continue
}
}
}
// Return a freshly generated Flake ID

View File

@ -6,6 +6,20 @@ import (
//////////////////////////////////////////////////////////////////////////////
// Benchmarks Presence Update event with fake data.
func BenchmarkGenerateChan(b *testing.B) {
node, _ := NewFlakeNode(1)
c := make(chan Flake)
go node.Generator(c)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
<-c
}
}
// Benchmarks Presence Update event with fake data.
func BenchmarkGenerate(b *testing.B) {