From 49a6eaa6d4d29f987f041d2a47cfa3f9043e6765 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Thu, 31 Jul 2025 12:52:00 +0800 Subject: [PATCH] rpc: opt NewID --- rpc/subscription.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/rpc/subscription.go b/rpc/subscription.go index 9e400c8b60..5b4599c855 100644 --- a/rpc/subscription.go +++ b/rpc/subscription.go @@ -24,7 +24,7 @@ import ( "encoding/hex" "encoding/json" "errors" - "math/rand" + "math/rand/v2" "reflect" "strings" "sync" @@ -59,23 +59,28 @@ func NewID() ID { // randomIDGenerator returns a function generates a random IDs. func randomIDGenerator() func() ID { - var buf = make([]byte, 8) - var seed int64 + var buf = make([]byte, 16) + var seed1, seed2 uint64 if _, err := crand.Read(buf); err == nil { - seed = int64(binary.BigEndian.Uint64(buf)) + seed1 = binary.BigEndian.Uint64(buf[:8]) + seed2 = binary.BigEndian.Uint64(buf[8:]) } else { - seed = int64(time.Now().Nanosecond()) + seed1 = uint64(time.Now().Nanosecond()) + seed2 = uint64(time.Now().Nanosecond()) } var ( mu sync.Mutex - rng = rand.New(rand.NewSource(seed)) + rng = rand.New(rand.NewPCG(seed1, seed2)) ) return func() ID { mu.Lock() defer mu.Unlock() id := make([]byte, 16) - rng.Read(id) + idPart0 := rng.Uint64() + idPart1 := rng.Uint64() + binary.BigEndian.PutUint64(id[:8], idPart0) + binary.BigEndian.PutUint64(id[8:], idPart1) return encodeID(id) } }