moving from uint64 to int64. allows use of snowflakes id in databases
This commit is contained in:
parent
852a57d9b3
commit
3991c52689
@ -16,30 +16,30 @@ const (
|
|||||||
sequenceBits = 12
|
sequenceBits = 12
|
||||||
|
|
||||||
// Custom Epoch (January 1, 2015 Midnight UTC = 2015-01-01T00:00:00Z)
|
// Custom Epoch (January 1, 2015 Midnight UTC = 2015-01-01T00:00:00Z)
|
||||||
customEpoch uint64 = 1420070400000
|
customEpoch int64 = 1420070400000
|
||||||
)
|
)
|
||||||
|
|
||||||
var maxNodeID uint64
|
var maxNodeID int64
|
||||||
var maxSequence uint64
|
var maxSequence int64
|
||||||
|
|
||||||
var nodeID uint64
|
var nodeID int64
|
||||||
var lastTimestamp uint64 = 0
|
var lastTimestamp int64 = 0
|
||||||
var sequence uint64
|
var sequence int64
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
maxNodeID = uint64(math.Pow(2, nodeIDBits) - 1)
|
maxNodeID = int64(math.Pow(2, nodeIDBits) - 1)
|
||||||
maxSequence = uint64(math.Pow(2, sequenceBits) - 1)
|
maxSequence = int64(math.Pow(2, sequenceBits) - 1)
|
||||||
nodeID = generateNodeID()
|
nodeID = generateNodeID()
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateNodeID() uint64 {
|
func generateNodeID() int64 {
|
||||||
var nodeID uint64
|
var nodeID int64
|
||||||
if interfaces, err := net.Interfaces(); err == nil {
|
if interfaces, err := net.Interfaces(); err == nil {
|
||||||
h := fnv.New32a()
|
h := fnv.New32a()
|
||||||
for _, i := range interfaces {
|
for _, i := range interfaces {
|
||||||
h.Write(i.HardwareAddr)
|
h.Write(i.HardwareAddr)
|
||||||
}
|
}
|
||||||
nodeID = uint64(h.Sum32())
|
nodeID = int64(h.Sum32())
|
||||||
} else {
|
} else {
|
||||||
panic("interfaces not available")
|
panic("interfaces not available")
|
||||||
}
|
}
|
||||||
@ -51,7 +51,7 @@ var timestampMutex sync.Mutex
|
|||||||
var sequenceMutex sync.Mutex
|
var sequenceMutex sync.Mutex
|
||||||
|
|
||||||
// Next returns the next logical snowflake
|
// Next returns the next logical snowflake
|
||||||
func Next() uint64 {
|
func Next() int64 {
|
||||||
timestampMutex.Lock()
|
timestampMutex.Lock()
|
||||||
currentTimestamp := ts()
|
currentTimestamp := ts()
|
||||||
timestampMutex.Unlock()
|
timestampMutex.Unlock()
|
||||||
@ -69,21 +69,22 @@ func Next() uint64 {
|
|||||||
sequenceMutex.Unlock()
|
sequenceMutex.Unlock()
|
||||||
|
|
||||||
lastTimestamp = currentTimestamp
|
lastTimestamp = currentTimestamp
|
||||||
id := currentTimestamp << (totalBits - epochBits)
|
// id := currentTimestamp << (totalBits - epochBits)
|
||||||
fmt.Printf("%b\n", id)
|
// id |= (nodeID << (totalBits - epochBits - nodeIDBits))
|
||||||
id |= (nodeID << (totalBits - epochBits - nodeIDBits))
|
// id |= sequence
|
||||||
fmt.Printf("%b\n", id)
|
var id int64 = currentTimestamp << (nodeIDBits + sequenceBits)
|
||||||
|
id |= (nodeID << sequenceBits)
|
||||||
id |= sequence
|
id |= sequence
|
||||||
|
|
||||||
fmt.Printf("%b\n", id)
|
fmt.Printf("%b\n", id)
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func ts() uint64 {
|
func ts() int64 {
|
||||||
return uint64(time.Now().UnixNano()/1000000) - customEpoch
|
return int64(time.Now().UnixNano()/1000000) - customEpoch
|
||||||
}
|
}
|
||||||
|
|
||||||
func waitNextMillis(currentTimestamp uint64) uint64 {
|
func waitNextMillis(currentTimestamp int64) int64 {
|
||||||
for currentTimestamp == lastTimestamp {
|
for currentTimestamp == lastTimestamp {
|
||||||
currentTimestamp = ts()
|
currentTimestamp = ts()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user