networkdb: rewrite with math/rand/v2 and avoid divide-by-zero

While at it, also check for zero value to prevent a (theoretical)
panic (panic: runtime error: integer divide by zero).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2026-07-14 10:37:38 +02:00
parent 27c419e5b5
commit 6044398f34

View File

@@ -6,7 +6,7 @@ import (
"encoding/hex"
"fmt"
golog "log"
rnd "math/rand"
"math/rand/v2"
"net"
"net/netip"
"slices"
@@ -234,12 +234,14 @@ func (nDB *NetworkDB) clusterLeave() error {
}
func (nDB *NetworkDB) triggerFunc(stagger time.Duration, C <-chan time.Time, f func()) {
// Use a random stagger to avoid synchronizing
randStagger := time.Duration(uint64(rnd.Int63()) % uint64(stagger)) //nolint:gosec // gosec complains about the use of rand here. It should be fine.
select {
case <-time.After(randStagger):
case <-nDB.ctx.Done():
return
if stagger > 0 {
// Use a random stagger to avoid synchronizing.
randStagger := time.Duration(rand.Int64N(int64(stagger))) // #nosec G404 -- use of math/rand/v2 is fine for this purpose.
select {
case <-time.After(randStagger):
case <-nDB.ctx.Done():
return
}
}
for {
select {