Skip to content

Commit

Permalink
fix: initializing random seeds in 'for' loop caused a performance deg…
Browse files Browse the repository at this point in the history
…radation
  • Loading branch information
OxalisCu committed Aug 23, 2024
1 parent c9a6d2d commit 4642de6
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions internal/writer/redis_standalone_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type redisStandaloneWriter struct {
address string
clients []*client.Redis
DbId int
rand *rand.Rand

clientNum int
offReply bool
Expand All @@ -54,6 +55,7 @@ func NewRedisStandaloneWriter(ctx context.Context, opts *RedisWriterOptions) Wri
rw.address = opts.Address
rw.stat.Name = "writer_" + strings.Replace(opts.Address, ":", "_", -1)
rw.clientNum = opts.Clients
rw.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < rw.clientNum; i++ {
rw.clients = append(rw.clients, client.NewRedisClient(ctx, opts.Address, opts.Username, opts.Password, opts.Tls, false))
}
Expand Down Expand Up @@ -128,8 +130,7 @@ func (w *redisStandaloneWriter) StartWrite(ctx context.Context) (ch chan *entry.

func (w *redisStandaloneWriter) Write(e *entry.Entry) {
// random select a client to send
rand.New(rand.NewSource(time.Now().UnixNano()))
selected := rand.Intn(len(w.clients))
selected := w.rand.Intn(len(w.clients))
w.chs[selected] <- e
}

Expand Down

0 comments on commit 4642de6

Please sign in to comment.