From eb461df31e31abbfad94227f645c43859c05eeb0 Mon Sep 17 00:00:00 2001 From: LeaFrock Date: Mon, 25 Dec 2023 23:16:42 +0800 Subject: [PATCH] Fix `Random` thread safety.(#502) (#579) --- .../Implementations/RedisConnectionPoolManager.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/StackExchange.Redis.Extensions.Core/Implementations/RedisConnectionPoolManager.cs b/src/core/StackExchange.Redis.Extensions.Core/Implementations/RedisConnectionPoolManager.cs index 1f875cf0..c4fcde5f 100644 --- a/src/core/StackExchange.Redis.Extensions.Core/Implementations/RedisConnectionPoolManager.cs +++ b/src/core/StackExchange.Redis.Extensions.Core/Implementations/RedisConnectionPoolManager.cs @@ -21,7 +21,6 @@ public sealed partial class RedisConnectionPoolManager : IRedisConnectionPoolMan private readonly IStateAwareConnection[] connections; private readonly RedisConfiguration redisConfiguration; private readonly ILogger logger; - private readonly Random random = new(); private bool isDisposed; /// @@ -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;