summaryrefslogtreecommitdiff
path: root/pkg/snowflake
diff options
context:
space:
mode:
authorKyren223 <ulmliad223@gmail.com>2024-10-20 20:12:12 +0300
committerKyren223 <ulmliad223@gmail.com>2024-10-20 20:12:12 +0300
commit6f83f199f8311bf7782da59bc2e07df38073e65a (patch)
tree8cfbe0a551bea927cf1c7f23bf5753f8ddf0ce46 /pkg/snowflake
parent1997bc8a150b92783060cc7129e1ee72a761182b (diff)
refactor: finished refactoring server-side code
Diffstat (limited to 'pkg/snowflake')
-rw-r--r--pkg/snowflake/snowflake.go58
1 files changed, 31 insertions, 27 deletions
diff --git a/pkg/snowflake/snowflake.go b/pkg/snowflake/snowflake.go
index bb4a07b..ab5fafa 100644
--- a/pkg/snowflake/snowflake.go
+++ b/pkg/snowflake/snowflake.go
@@ -12,16 +12,34 @@ import (
const (
// Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC in milliseconds
// TODO: change this to eko epoch when eko is production ready
- Epoch int64 = 1288834974657
- nodeBits = 10
- stepBits = 12
- NodeMax = 1<<nodeBits - 1
- nodeMask = NodeMax << stepBits
- stepMask = 1<<stepBits - 1
- timeShift = nodeBits + stepBits
- nodeShift = stepBits
+ Epoch int64 = 1288834974657
+ nodeBits = 10
+ stepBits = 12
+ NodeMax = 1<<nodeBits - 1
+ nodeMask = NodeMax << stepBits
+ stepMask = 1<<stepBits - 1
+ timeShift = nodeBits + stepBits
+ nodeShift = stepBits
)
+type ID int64
+
+func (id ID) String() string {
+ return strconv.FormatInt(int64(id), 10)
+}
+
+func (id ID) Time() (msSinceEpoch int64) {
+ return (int64(id) >> timeShift) + Epoch
+}
+
+func (id ID) Node() int64 {
+ return int64(id) & nodeMask >> nodeShift
+}
+
+func (id ID) Step() int64 {
+ return int64(id) & stepMask
+}
+
type Node struct {
mu sync.Mutex
epoch time.Time
@@ -30,11 +48,13 @@ type Node struct {
step int64
}
-type ID int64
+func (n *Node) String() string {
+ return fmt.Sprintf("Node%v(epoch: %v, step: %v, time: %v)", n.node, n.epoch, n.step, n.time)
+}
func NewNode(node int64) *Node {
- assert.Assert(nodeBits+stepBits <= 22, "node and step bits must add up to 22 or less")
- assert.Assert(0 <= node && node <= NodeMax, "node and step bits must add up to 22 or less")
+ assert.Assert(nodeBits+stepBits == 22, "node and step bits must add up to 22")
+ assert.Assert(0 <= node && node <= NodeMax, "node must be within 0 and NodeMax", "node", node)
// Credit to https://github.com/bwmarrin/snowflake
currentTime := time.Now()
@@ -67,22 +87,6 @@ func (n *Node) Generate() ID {
return ID((now << timeShift) | (n.node << nodeShift) | (n.step))
}
-func (id ID) String() string {
- return strconv.FormatInt(int64(id), 10)
-}
-
-func (id ID) Time() (msSinceEpoch int64) {
- return (int64(id) >> timeShift) + Epoch
-}
-
-func (id ID) Node() int64 {
- return int64(id) & nodeMask >> nodeShift
-}
-
-func (id ID) Step() int64 {
- return int64(id) & stepMask
-}
-
// Json marshling to avoid inprecision of json number (float64)
func (id ID) MarshalJSON() ([]byte, error) {