mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #9642 from lorneli/idutil-dev
pkg/idutil: use count field as atomic variable
This commit is contained in:
commit
624f421d21
@ -18,7 +18,7 @@ package idutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"sync"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -47,7 +47,6 @@ const (
|
|||||||
// id generated after restart is unique because etcd throughput is <<
|
// id generated after restart is unique because etcd throughput is <<
|
||||||
// 256req/ms(250k reqs/second).
|
// 256req/ms(250k reqs/second).
|
||||||
type Generator struct {
|
type Generator struct {
|
||||||
mu sync.Mutex
|
|
||||||
// high order 2 bytes
|
// high order 2 bytes
|
||||||
prefix uint64
|
prefix uint64
|
||||||
// low order 6 bytes
|
// low order 6 bytes
|
||||||
@ -66,10 +65,8 @@ func NewGenerator(memberID uint16, now time.Time) *Generator {
|
|||||||
|
|
||||||
// Next generates a id that is unique.
|
// Next generates a id that is unique.
|
||||||
func (g *Generator) Next() uint64 {
|
func (g *Generator) Next() uint64 {
|
||||||
g.mu.Lock()
|
suffix := atomic.AddUint64(&g.suffix, 1)
|
||||||
defer g.mu.Unlock()
|
id := g.prefix | lowbit(suffix, suffixLen)
|
||||||
g.suffix++
|
|
||||||
id := g.prefix | lowbit(g.suffix, suffixLen)
|
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,3 +53,12 @@ func TestNext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkNext(b *testing.B) {
|
||||||
|
g := NewGenerator(0x12, time.Now())
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
g.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user