summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorKyren223 <ulmliad223@gmail.com>2024-10-24 20:06:04 +0300
committerKyren223 <ulmliad223@gmail.com>2024-10-24 20:06:04 +0300
commitd294b53e68f02f4aae98fc3f2133c7696706bf0b (patch)
tree7072187c5f8034514e0f115661bae00c10ea328c /pkg
parenta7f743f035d0f0d14400027e4781cad76503085a (diff)
chore: add docs
Diffstat (limited to 'pkg')
-rw-r--r--pkg/snowflake/snowflake.go5
-rw-r--r--pkg/utils/utils.go10
2 files changed, 6 insertions, 9 deletions
diff --git a/pkg/snowflake/snowflake.go b/pkg/snowflake/snowflake.go
index ab5fafa..6a53760 100644
--- a/pkg/snowflake/snowflake.go
+++ b/pkg/snowflake/snowflake.go
@@ -52,6 +52,8 @@ func (n *Node) String() string {
return fmt.Sprintf("Node%v(epoch: %v, step: %v, time: %v)", n.node, n.epoch, n.step, n.time)
}
+// Creates a new node for generating unique IDs.
+// To gurantee uniqueness, the given node ID must be unique.
func NewNode(node int64) *Node {
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)
@@ -67,6 +69,7 @@ func NewNode(node int64) *Node {
}
}
+// Generates a unique snowflake ID.
func (n *Node) Generate() ID {
n.mu.Lock()
defer n.mu.Unlock()
@@ -90,7 +93,7 @@ func (n *Node) Generate() ID {
// Json marshling to avoid inprecision of json number (float64)
func (id ID) MarshalJSON() ([]byte, error) {
- buffer := make([]byte, 0, 22) // 2 quotes, 19 digits for 2^63, 1 sign
+ buffer := make([]byte, 0, 22) // 2 quotes, 19 digits, 1 sign
buffer = append(buffer, '"')
buffer = strconv.AppendInt(buffer, int64(id), 10)
buffer = append(buffer, '"')
diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go
index 3f4a805..34e85d3 100644
--- a/pkg/utils/utils.go
+++ b/pkg/utils/utils.go
@@ -1,11 +1,5 @@
package utils
-func Clamp(value, min, max int64) int64 {
- if value < min {
- return min
- }
- if value > max {
- return max
- }
- return value
+func Clamp(value, minimum, maximum int64) int64 {
+ return min(max(value, minimum), maximum)
}