Merge branch 'master' into monotonic_clock

This commit is contained in:
Nishaad Ajani
2019-04-10 17:14:22 +08:00
committed by GitHub
4 changed files with 80 additions and 20 deletions

View File

@@ -18,19 +18,21 @@ var (
// Note: add time.Duration to time.Now() to make sure we use the monotonic clock if available.
Epoch = curTime.Add(time.Date(2010, time.November, 4, 1, 42, 54, 0, time.UTC).Sub(curTime))
// Number of bits to use for Node
// NodeBits holds the number of bits to use for Node
// Remember, you have a total 22 bits to share between Node/Step
NodeBits uint8 = 10
// Number of bits to use for Step
// StepBits holds the number of bits to use for Step
// Remember, you have a total 22 bits to share between Node/Step
StepBits uint8 = 12
// DEPRECATED: the below four variables will be removed in a future release.
mu sync.Mutex
nodeMax int64 = -1 ^ (-1 << NodeBits)
nodeMask int64 = nodeMax << StepBits
nodeMask = nodeMax << StepBits
stepMask int64 = -1 ^ (-1 << StepBits)
timeShift uint8 = NodeBits + StepBits
nodeShift uint8 = StepBits
timeShift = NodeBits + StepBits
nodeShift = StepBits
)
const encodeBase32Map = "ybndrfg8ejkmcpqxot1uwisza345h769"
@@ -81,6 +83,12 @@ type Node struct {
time time.Duration
node int64
step int64
nodeMax int64
nodeMask int64
stepMask int64
timeShift uint8
nodeShift uint8
}
// An ID is a custom type used for a snowflake ID. This is used so we can
@@ -92,17 +100,28 @@ type ID int64
func NewNode(node int64) (*Node, error) {
// re-calc in case custom NodeBits or StepBits were set
// DEPRECATED: the below block will be removed in a future release.
mu.Lock()
nodeMax = -1 ^ (-1 << NodeBits)
nodeMask = nodeMax << StepBits
stepMask = -1 ^ (-1 << StepBits)
timeShift = NodeBits + StepBits
nodeShift = StepBits
mu.Unlock()
if node < 0 || node > nodeMax {
return nil, errors.New("Node number must be between 0 and " + strconv.FormatInt(nodeMax, 10))
n := Node{}
n.node = node
n.nodeMax = -1 ^ (-1 << NodeBits)
n.nodeMask = n.nodeMax << StepBits
n.stepMask = -1 ^ (-1 << StepBits)
n.timeShift = NodeBits + StepBits
n.nodeShift = StepBits
if n.node < 0 || n.node > n.nodeMax {
return nil, errors.New("Node number must be between 0 and " + strconv.FormatInt(n.nodeMax, 10))
}
return &Node{node: node}, nil
return &n, nil
}
// Generate creates and returns a unique snowflake ID
@@ -113,7 +132,7 @@ func (n *Node) Generate() ID {
now := time.Since(Epoch)
if now-n.time < time.Millisecond {
n.step = (n.step + 1) & stepMask
n.step = (n.step + 1) & n.stepMask
if n.step == 0 {
for now-n.time < time.Millisecond {
@@ -126,8 +145,8 @@ func (n *Node) Generate() ID {
n.time = now
r := ID((now.Nanoseconds()/1000000)<<timeShift |
(n.node << nodeShift) |
r := ID((now.Nanoseconds()/1000000)<<n.timeShift |
(n.node << n.nodeShift) |
(n.step),
)
@@ -251,16 +270,19 @@ func (f ID) IntBytes() [8]byte {
}
// Time returns an int64 unix timestamp in miliseconds of the snowflake ID time
// DEPRECATED: the below function will be removed in a future release.
func (f ID) Time() int64 {
return (int64(f) >> timeShift) + (Epoch.UnixNano() / 1000000)
}
// Node returns an int64 of the snowflake ID node number
// DEPRECATED: the below function will be removed in a future release.
func (f ID) Node() int64 {
return int64(f) & nodeMask >> nodeShift
}
// Step returns an int64 of the snowflake step (or sequence) number
// DEPRECATED: the below function will be removed in a future release.
func (f ID) Step() int64 {
return int64(f) & stepMask
}