- 增加 UseSlaveWeight 读权重设置;#1046

This commit is contained in:
2881099
2022-03-18 21:55:02 +08:00
parent 203681ea6a
commit 47b032b6ae
26 changed files with 64 additions and 4 deletions

View File

@ -582,7 +582,20 @@ namespace FreeSql.Internal.CommonProvider
if (availables.Any())
{
isSlave = true;
pool = availables.Count == 1 ? availables[0] : availables[slaveRandom.Next(availables.Count)];
if (availables.Count == 1) pool = availables[0];
else
{
var rnd = slaveRandom.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;
}
}
}
}
}
}

View File

@ -110,6 +110,7 @@ namespace FreeSql.Internal.CommonProvider
public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
public DbConnection OnCreate()
{

View File

@ -17,6 +17,7 @@ namespace FreeSql.Internal.ObjectPool
public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
public Func<T> CreateObject;
public Action<Object<T>> OnGetObject;

View File

@ -48,6 +48,11 @@ namespace FreeSql.Internal.ObjectPool
/// </summary>
int CheckAvailableInterval { get; set; }
/// <summary>
/// 权重
/// </summary>
int Weight { get; set; }
/// <summary>
/// 对象池的对象被创建时
/// </summary>