- 增加 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

@ -106,9 +106,12 @@ namespace base_entity
.UseAutoSyncStructure(true) .UseAutoSyncStructure(true)
.UseNoneCommandParameter(true) .UseNoneCommandParameter(true)
.UseConnectionString(FreeSql.DataType.Sqlite, "data source=test.db;max pool size=5") .UseConnectionString(FreeSql.DataType.Sqlite, "data source=test1.db;max pool size=5")
.UseSlave("data source=test1.db", "data source=test2.db", "data source=test3.db", "data source=test4.db")
.UseSlaveWeight(10, 1, 1, 5)
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3") //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3")
@ -131,13 +134,16 @@ namespace base_entity
//.UseConnectionString(FreeSql.DataType.OdbcDameng, "Driver={DM8 ODBC DRIVER};Server=127.0.0.1:5236;Persist Security Info=False;Trusted_Connection=Yes;UID=USER1;PWD=123456789") //.UseConnectionString(FreeSql.DataType.OdbcDameng, "Driver={DM8 ODBC DRIVER};Server=127.0.0.1:5236;Persist Security Info=False;Trusted_Connection=Yes;UID=USER1;PWD=123456789")
.UseMonitorCommand(umcmd => Console.WriteLine(umcmd.CommandText)) .UseMonitorCommand(null, (umcmd, log) => Console.WriteLine(umcmd.Connection.ConnectionString + ":" + umcmd.CommandText))
.UseLazyLoading(true) .UseLazyLoading(true)
.UseGenerateCommandParameterWithLambda(true) .UseGenerateCommandParameterWithLambda(true)
.Build(); .Build();
BaseEntity.Initialization(fsql, () => _asyncUow.Value); BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion #endregion
for (var a = 0; a < 10000; a++)
fsql.Select<User1>().First();
for (var a = 0; a < 1000; a++) for (var a = 0; a < 1000; a++)
{ {
fsql.Transaction(() => fsql.Transaction(() =>

View File

@ -4314,6 +4314,11 @@
后台定时检查可用性间隔秒数 后台定时检查可用性间隔秒数
</summary> </summary>
</member> </member>
<member name="P:FreeSql.Internal.ObjectPool.IPolicy`1.Weight">
<summary>
权重
</summary>
</member>
<member name="M:FreeSql.Internal.ObjectPool.IPolicy`1.OnCreate"> <member name="M:FreeSql.Internal.ObjectPool.IPolicy`1.OnCreate">
<summary> <summary>
对象池的对象被创建时 对象池的对象被创建时

View File

@ -14,6 +14,7 @@ namespace FreeSql
DataType _dataType; DataType _dataType;
string _masterConnectionString; string _masterConnectionString;
string[] _slaveConnectionString; string[] _slaveConnectionString;
int[] _slaveWeights;
Func<DbConnection> _connectionFactory; Func<DbConnection> _connectionFactory;
bool _isAutoSyncStructure = false; bool _isAutoSyncStructure = false;
bool _isSyncStructureToLower = false; bool _isSyncStructureToLower = false;
@ -55,6 +56,12 @@ namespace FreeSql
_slaveConnectionString = slaveConnectionString; _slaveConnectionString = slaveConnectionString;
return this; return this;
} }
public FreeSqlBuilder UseSlaveWeight(params int[] slaveWeights)
{
if (_slaveConnectionString?.Length != slaveWeights.Length) throw new Exception("SlaveConnectionString 数量与 SlaveWeights 不相同");
_slaveWeights = slaveWeights;
return this;
}
/// <summary> /// <summary>
/// 使用自定义数据库连接对象(放弃内置对象连接池技术) /// 使用自定义数据库连接对象(放弃内置对象连接池技术)
/// </summary> /// </summary>
@ -458,6 +465,9 @@ namespace FreeSql
ret.Ado.MasterPool.Policy.IsAutoDisposeWithSystem = _isExitAutoDisposePool; ret.Ado.MasterPool.Policy.IsAutoDisposeWithSystem = _isExitAutoDisposePool;
ret.Ado.SlavePools.ForEach(a => a.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; return ret;

View File

@ -582,7 +582,20 @@ namespace FreeSql.Internal.CommonProvider
if (availables.Any()) if (availables.Any())
{ {
isSlave = true; 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 IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
public DbConnection OnCreate() public DbConnection OnCreate()
{ {

View File

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

View File

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

View File

@ -64,6 +64,7 @@ namespace FreeSql.ClickHouse
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -68,6 +68,7 @@ namespace FreeSql.Dameng
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -52,6 +52,7 @@ namespace FreeSql.Firebird
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -52,6 +52,7 @@ namespace FreeSql.GBase
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -74,6 +74,7 @@ namespace FreeSql.KingbaseES
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -57,6 +57,7 @@ namespace FreeSql.MsAccess
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
private string _connectionString; private string _connectionString;
public string ConnectionString public string ConnectionString

View File

@ -56,6 +56,7 @@ namespace FreeSql.MySql
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -74,6 +74,7 @@ namespace FreeSql.Odbc.Dameng
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -57,6 +57,7 @@ namespace FreeSql.Odbc.Default
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -74,6 +74,7 @@ namespace FreeSql.Odbc.KingbaseES
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -52,6 +52,7 @@ namespace FreeSql.Odbc.MySql
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -74,6 +74,7 @@ namespace FreeSql.Odbc.Oracle
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -64,6 +64,7 @@ namespace FreeSql.Odbc.PostgreSQL
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -57,6 +57,7 @@ namespace FreeSql.Odbc.SqlServer
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -74,6 +74,7 @@ namespace FreeSql.Oracle
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -64,6 +64,7 @@ namespace FreeSql.PostgreSQL
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -62,6 +62,7 @@ namespace FreeSql.ShenTong
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -61,6 +61,7 @@ namespace FreeSql.SqlServer
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private string _connectionString; private string _connectionString;

View File

@ -64,6 +64,7 @@ namespace FreeSql.Sqlite
public bool IsThrowGetTimeoutException { get; set; } = true; public bool IsThrowGetTimeoutException { get; set; } = true;
public bool IsAutoDisposeWithSystem { get; set; } = true; public bool IsAutoDisposeWithSystem { get; set; } = true;
public int CheckAvailableInterval { get; set; } = 5; public int CheckAvailableInterval { get; set; } = 5;
public int Weight { get; set; } = 1;
public string[] Attaches = new string[0]; public string[] Attaches = new string[0];
private string _connectionString; private string _connectionString;