Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pkg/dmsg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dmsg

import (
"context"
crand "crypto/rand"
"encoding/binary"
"errors"
"fmt"
"math/rand"
Expand Down Expand Up @@ -209,8 +211,15 @@ func (ce *Client) Serve(ctx context.Context) {
ce.log.Warnf("No entries found. Retrying after %s...", ce.bo.String())
ce.serveWait()
}
// randomize dmsg servers list
rand.Shuffle(len(entries), func(i, j int) {
// randomize dmsg servers list using crypto/rand seed for true randomization
// This ensures each client connects to servers in a different order,
// preventing load imbalance when multiple clients start simultaneously
var seed int64
if err := binary.Read(crand.Reader, binary.BigEndian, &seed); err != nil {
seed = time.Now().UnixNano() // fallback to time-based seed
}
rng := rand.New(rand.NewSource(seed)) //nolint:gosec // G404: seed is from crypto/rand, math/rand is fine for shuffling
rng.Shuffle(len(entries), func(i, j int) {
entries[i], entries[j] = entries[j], entries[i]
})

Expand Down
Loading