mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	优化 连接池对象预热效率,开启每10个线程进行预热;
This commit is contained in:
		@@ -1,6 +1,10 @@
 | 
			
		||||
using System;
 | 
			
		||||
using SafeObjectPool;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Concurrent;
 | 
			
		||||
using System.Data.Common;
 | 
			
		||||
using System.Diagnostics;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
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')}";
 | 
			
		||||
		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);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				 var initConns = new List<Object<DbConnection>>();
 | 
			
		||||
				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);
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -83,16 +83,7 @@ namespace FreeSql.Oracle {
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				var initConns = new List<Object<DbConnection>>();
 | 
			
		||||
				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);
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -78,16 +78,7 @@ namespace FreeSql.PostgreSQL {
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				var initConns = new List<Object<DbConnection>>();
 | 
			
		||||
				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);
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -73,16 +73,7 @@ namespace FreeSql.SqlServer {
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				var initConns = new List<Object<DbConnection>>();
 | 
			
		||||
				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);
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -80,16 +80,7 @@ namespace FreeSql.Sqlite {
 | 
			
		||||
					Attaches = (idx == -1 ? att[1] : att[1].Substring(0, idx)).Split(',');
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				var initConns = new List<Object<DbConnection>>();
 | 
			
		||||
				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);
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user