You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: broadcast/snowflake.go
+10-4
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
package broadcast
2
2
3
3
import (
4
+
"log/slog"
4
5
"math/rand"
5
6
"sync"
6
7
"time"
@@ -16,8 +17,8 @@ type Snowflake struct {
16
17
}
17
18
18
19
const (
19
-
maxShard=float32(1<<4)
20
-
maxMachineID=float32(1<<12)
20
+
MaxMachineID=uint16(1<<12)
21
+
maxShard=uint8(1<<4)
21
22
maxSequenceNum=uint32(1<<18)
22
23
bitMaskTimestamp=uint64((1<<30)-1) <<34
23
24
bitMaskShardID=uint64((1<<4)-1) <<30
@@ -26,10 +27,15 @@ const (
26
27
epoch="2024-01-01T00:00:00"
27
28
)
28
29
29
-
funcNewSnowflake(addrstring) *Snowflake {
30
+
funcNewSnowflake(iduint64) *Snowflake {
31
+
ifid<0||id>=uint64(MaxMachineID) {
32
+
newID:=uint64(rand.Int31n(int32(MaxMachineID)))
33
+
slog.Warn("snowflakeID: provided machienID is higher than max or lower than min. A random ID will be assigned instead.", "max", MaxMachineID, "min", 0, "givenID", id, "assignedID", newID)
0 commit comments