Skip to content

Commit

Permalink
Fix Random thread safety.(#502) (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaFrock authored Dec 25, 2023
1 parent 8ad186b commit eb461df
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public sealed partial class RedisConnectionPoolManager : IRedisConnectionPoolMan
private readonly IStateAwareConnection[] connections;
private readonly RedisConfiguration redisConfiguration;
private readonly ILogger<RedisConnectionPoolManager> logger;
private readonly Random random = new();
private bool isDisposed;

/// <summary>
Expand Down Expand Up @@ -71,7 +70,12 @@ public IConnectionMultiplexer GetConnection()
switch (redisConfiguration.ConnectionSelectionStrategy)
{
case ConnectionSelectionStrategy.RoundRobin:
var nextIdx = random.Next(0, redisConfiguration.PoolSize);
var nextIdx
#if NET6_0_OR_GREATER
= Random.Shared.Next(0, redisConfiguration.PoolSize);
#else
= new Random().Next(0, redisConfiguration.PoolSize);
#endif
connection = connections[nextIdx];
break;

Expand Down

0 comments on commit eb461df

Please sign in to comment.