mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
优化 连接池对象预热效率,开启每10个线程进行预热;
This commit is contained in:
parent
40571ed0e5
commit
c3c7539150
@ -1,6 +1,10 @@
|
|||||||
using System;
|
using SafeObjectPool;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Data.Common;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public static class FreeUtil {
|
public static class FreeUtil {
|
||||||
|
|
||||||
@ -27,4 +31,36 @@ public static class FreeUtil {
|
|||||||
var guid = $"{uninxtime.ToString("x8").PadLeft(8, '0')}{__staticMachine.ToString("x8").PadLeft(8, '0').Substring(2, 6)}{__staticPid.ToString("x8").PadLeft(8, '0').Substring(6, 2)}{increment.ToString("x8").PadLeft(8, '0')}{rand.ToString("x8").PadLeft(8, '0')}";
|
var guid = $"{uninxtime.ToString("x8").PadLeft(8, '0')}{__staticMachine.ToString("x8").PadLeft(8, '0').Substring(2, 6)}{__staticPid.ToString("x8").PadLeft(8, '0').Substring(6, 2)}{increment.ToString("x8").PadLeft(8, '0')}{rand.ToString("x8").PadLeft(8, '0')}";
|
||||||
return Guid.Parse(guid);
|
return Guid.Parse(guid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static void PrevReheatConnectionPool(ObjectPool<DbConnection> pool) {
|
||||||
|
var initTestOk = true;
|
||||||
|
var initStartTime = DateTime.Now;
|
||||||
|
var initConns = new ConcurrentBag<Object<DbConnection>>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
var conn = pool.Get();
|
||||||
|
initConns.Add(conn);
|
||||||
|
pool.Policy.OnCheckAvailable(conn);
|
||||||
|
} catch {
|
||||||
|
initTestOk = false; //预热一次失败,后面将不进行
|
||||||
|
}
|
||||||
|
for (var a = 1; initTestOk && a < pool.Policy.PoolSize; a += 10) {
|
||||||
|
if (initStartTime.Subtract(DateTime.Now).TotalSeconds > 3) break; //预热耗时超过3秒,退出
|
||||||
|
var b = Math.Min(pool.Policy.PoolSize - a, 10); //每10个预热
|
||||||
|
var initTasks = new Task[b];
|
||||||
|
for (var c = 0; c < b; c++) {
|
||||||
|
initTasks[c] = Task.Run(() => {
|
||||||
|
try {
|
||||||
|
var conn = pool.Get();
|
||||||
|
initConns.Add(conn);
|
||||||
|
pool.Policy.OnCheckAvailable(conn);
|
||||||
|
} catch {
|
||||||
|
initTestOk = false; //有失败,下一组退出预热
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Task.WaitAll(initTasks);
|
||||||
|
}
|
||||||
|
while (initConns.TryTake(out var conn)) pool.Return(conn);
|
||||||
|
}
|
||||||
}
|
}
|
@ -69,16 +69,7 @@ namespace FreeSql.MySql {
|
|||||||
_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
|
_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
var initConns = new List<Object<DbConnection>>();
|
FreeUtil.PrevReheatConnectionPool(_pool);
|
||||||
for (var a = 0; a < PoolSize; a++)
|
|
||||||
try {
|
|
||||||
var conn = _pool.Get();
|
|
||||||
initConns.Add(conn);
|
|
||||||
conn.Value.Ping(true);
|
|
||||||
} catch {
|
|
||||||
break; //预热失败一次就退出
|
|
||||||
}
|
|
||||||
foreach (var conn in initConns) _pool.Return(conn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,16 +83,7 @@ namespace FreeSql.Oracle {
|
|||||||
_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
|
_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
var initConns = new List<Object<DbConnection>>();
|
FreeUtil.PrevReheatConnectionPool(_pool);
|
||||||
for (var a = 0; a < PoolSize; a++)
|
|
||||||
try {
|
|
||||||
var conn = _pool.Get();
|
|
||||||
initConns.Add(conn);
|
|
||||||
conn.Value.Ping(true);
|
|
||||||
} catch {
|
|
||||||
break; //预热失败一次就退出
|
|
||||||
}
|
|
||||||
foreach (var conn in initConns) _pool.Return(conn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,16 +78,7 @@ namespace FreeSql.PostgreSQL {
|
|||||||
_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
|
_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
var initConns = new List<Object<DbConnection>>();
|
FreeUtil.PrevReheatConnectionPool(_pool);
|
||||||
for (var a = 0; a < PoolSize; a++)
|
|
||||||
try {
|
|
||||||
var conn = _pool.Get();
|
|
||||||
initConns.Add(conn);
|
|
||||||
conn.Value.Ping(true);
|
|
||||||
} catch {
|
|
||||||
break; //预热失败一次就退出
|
|
||||||
}
|
|
||||||
foreach (var conn in initConns) _pool.Return(conn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,16 +73,7 @@ namespace FreeSql.SqlServer {
|
|||||||
_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
|
_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
var initConns = new List<Object<DbConnection>>();
|
FreeUtil.PrevReheatConnectionPool(_pool);
|
||||||
for (var a = 0; a < PoolSize; a++)
|
|
||||||
try {
|
|
||||||
var conn = _pool.Get();
|
|
||||||
initConns.Add(conn);
|
|
||||||
conn.Value.Ping(true);
|
|
||||||
} catch {
|
|
||||||
break; //预热失败一次就退出
|
|
||||||
}
|
|
||||||
foreach (var conn in initConns) _pool.Return(conn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,16 +80,7 @@ namespace FreeSql.Sqlite {
|
|||||||
Attaches = (idx == -1 ? att[1] : att[1].Substring(0, idx)).Split(',');
|
Attaches = (idx == -1 ? att[1] : att[1].Substring(0, idx)).Split(',');
|
||||||
}
|
}
|
||||||
|
|
||||||
var initConns = new List<Object<DbConnection>>();
|
FreeUtil.PrevReheatConnectionPool(_pool);
|
||||||
for (var a = 0; a < PoolSize; a++)
|
|
||||||
try {
|
|
||||||
var conn = _pool.Get();
|
|
||||||
initConns.Add(conn);
|
|
||||||
conn.Value.Ping(true);
|
|
||||||
} catch {
|
|
||||||
break; //预热失败一次就退出
|
|
||||||
}
|
|
||||||
foreach (var conn in initConns) _pool.Return(conn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user