mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 UseSlaveWeight 读权重设置;#1046
This commit is contained in:
@ -4314,6 +4314,11 @@
|
||||
后台定时检查可用性间隔秒数
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:FreeSql.Internal.ObjectPool.IPolicy`1.Weight">
|
||||
<summary>
|
||||
权重
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.ObjectPool.IPolicy`1.OnCreate">
|
||||
<summary>
|
||||
对象池的对象被创建时
|
||||
|
@ -14,6 +14,7 @@ namespace FreeSql
|
||||
DataType _dataType;
|
||||
string _masterConnectionString;
|
||||
string[] _slaveConnectionString;
|
||||
int[] _slaveWeights;
|
||||
Func<DbConnection> _connectionFactory;
|
||||
bool _isAutoSyncStructure = false;
|
||||
bool _isSyncStructureToLower = false;
|
||||
@ -55,6 +56,12 @@ namespace FreeSql
|
||||
_slaveConnectionString = slaveConnectionString;
|
||||
return this;
|
||||
}
|
||||
public FreeSqlBuilder UseSlaveWeight(params int[] slaveWeights)
|
||||
{
|
||||
if (_slaveConnectionString?.Length != slaveWeights.Length) throw new Exception("SlaveConnectionString 数量与 SlaveWeights 不相同");
|
||||
_slaveWeights = slaveWeights;
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
/// 使用自定义数据库连接对象(放弃内置对象连接池技术)
|
||||
/// </summary>
|
||||
@ -458,6 +465,9 @@ namespace FreeSql
|
||||
|
||||
ret.Ado.MasterPool.Policy.IsAutoDisposeWithSystem = _isExitAutoDisposePool;
|
||||
ret.Ado.SlavePools.ForEach(a => a.Policy.IsAutoDisposeWithSystem = _isExitAutoDisposePool);
|
||||
if (_slaveWeights != null)
|
||||
for (var x = 0; x < _slaveWeights.Length; x++)
|
||||
ret.Ado.SlavePools[x].Policy.Weight = _slaveWeights[x];
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -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;
|
||||
|
@ -48,6 +48,11 @@ namespace FreeSql.Internal.ObjectPool
|
||||
/// </summary>
|
||||
int CheckAvailableInterval { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 权重
|
||||
/// </summary>
|
||||
int Weight { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 对象池的对象被创建时
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user