- 修复 UseSlaveWeight 异步下可能无效的问题;#1382

This commit is contained in:
2881099
2022-12-24 16:28:16 +08:00
parent 9118407952
commit 82c0eeee82
5 changed files with 16 additions and 196 deletions

View File

@ -33,7 +33,6 @@ namespace FreeSql.Internal.CommonProvider
public CommonUtils _util { get; set; }
protected int slaveUnavailables = 0;
private object slaveLock = new object();
private Random slaveRandom = new Random();
protected Func<DbTransaction> ResolveTransaction;
public AdoProvider(DataType dataType, string connectionString, string[] slaveConnectionStrings)
@ -584,7 +583,7 @@ namespace FreeSql.Internal.CommonProvider
if (availables.Count == 1) pool = availables[0];
else
{
var rnd = slaveRandom.Next(availables.Sum(a => a.Policy.Weight));
var rnd = FreeUtil.rnd.Value.Next(availables.Sum(a => a.Policy.Weight));
for(var a = 0; a < availables.Count; a++)
{
rnd -= availables[a].Policy.Weight;

View File

@ -501,7 +501,20 @@ namespace FreeSql.Internal.CommonProvider
if (availables.Any())
{
isSlave = true;
pool = availables.Count == 1 ? this.SlavePools[0] : availables[slaveRandom.Next(availables.Count)];
if (availables.Count == 1) pool = availables[0];
else
{
var rnd = FreeUtil.rnd.Value.Next(availables.Sum(a => a.Policy.Weight));
for (var a = 0; a < availables.Count; a++)
{
rnd -= availables[a].Policy.Weight;
if (rnd < 0)
{
pool = availables[a];
break;
}
}
}
}
}
}