|
@@ -24,7 +24,7 @@ import (
|
|
|
|
|
|
|
|
const (
|
|
const (
|
|
|
tsLen = 5 * 8
|
|
tsLen = 5 * 8
|
|
|
- cntLen = 2 * 8
|
|
|
|
|
|
|
+ cntLen = 8
|
|
|
suffixLen = tsLen + cntLen
|
|
suffixLen = tsLen + cntLen
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -32,7 +32,7 @@ const (
|
|
|
// High order byte is memberID, next 5 bytes are from timestamp,
|
|
// High order byte is memberID, next 5 bytes are from timestamp,
|
|
|
// and low order 2 bytes are 0s.
|
|
// and low order 2 bytes are 0s.
|
|
|
// | prefix | suffix |
|
|
// | prefix | suffix |
|
|
|
-// | 1 byte | 5 bytes | 2 bytes |
|
|
|
|
|
|
|
+// | 2 bytes | 5 bytes | 1 byte |
|
|
|
// | memberID | timestamp | cnt |
|
|
// | memberID | timestamp | cnt |
|
|
|
//
|
|
//
|
|
|
// The timestamp 5 bytes is different when the machine is restart
|
|
// The timestamp 5 bytes is different when the machine is restart
|
|
@@ -42,16 +42,16 @@ const (
|
|
|
// The count field may overflow to timestamp field, which is intentional.
|
|
// The count field may overflow to timestamp field, which is intentional.
|
|
|
// It helps to extend the event window to 2^56. This doesn't break that
|
|
// It helps to extend the event window to 2^56. This doesn't break that
|
|
|
// id generated after restart is unique because etcd throughput is <<
|
|
// id generated after restart is unique because etcd throughput is <<
|
|
|
-// 65536req/ms.
|
|
|
|
|
|
|
+// 256req/ms(250k reqs/second).
|
|
|
type Generator struct {
|
|
type Generator struct {
|
|
|
mu sync.Mutex
|
|
mu sync.Mutex
|
|
|
- // high order byte
|
|
|
|
|
|
|
+ // high order 2 bytes
|
|
|
prefix uint64
|
|
prefix uint64
|
|
|
- // low order 7 bytes
|
|
|
|
|
|
|
+ // low order 6 bytes
|
|
|
suffix uint64
|
|
suffix uint64
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func NewGenerator(memberID uint8, now time.Time) *Generator {
|
|
|
|
|
|
|
+func NewGenerator(memberID uint16, now time.Time) *Generator {
|
|
|
prefix := uint64(memberID) << suffixLen
|
|
prefix := uint64(memberID) << suffixLen
|
|
|
unixMilli := uint64(now.UnixNano()) / uint64(time.Millisecond/time.Nanosecond)
|
|
unixMilli := uint64(now.UnixNano()) / uint64(time.Millisecond/time.Nanosecond)
|
|
|
suffix := lowbit(unixMilli, tsLen) << cntLen
|
|
suffix := lowbit(unixMilli, tsLen) << cntLen
|