mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	- 优化 连接预热策略:min pool size 不设置或 <= 0,则默认预热 5个;也可以设置 1;
This commit is contained in:
		@@ -32,7 +32,9 @@ public static class FreeUtil {
 | 
			
		||||
		return Guid.Parse(guid);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	internal static void PrevReheatConnectionPool(ObjectPool<DbConnection> pool) {
 | 
			
		||||
	internal static void PrevReheatConnectionPool(ObjectPool<DbConnection> pool, int minPoolSize) {
 | 
			
		||||
		if (minPoolSize <= 0) minPoolSize = Math.Min(5, pool.Policy.PoolSize);
 | 
			
		||||
		if (minPoolSize > pool.Policy.PoolSize) minPoolSize = pool.Policy.PoolSize;
 | 
			
		||||
		var initTestOk = true;
 | 
			
		||||
		var initStartTime = DateTime.Now;
 | 
			
		||||
		var initConns = new ConcurrentBag<Object<DbConnection>>();
 | 
			
		||||
@@ -44,9 +46,9 @@ public static class FreeUtil {
 | 
			
		||||
		} catch {
 | 
			
		||||
			initTestOk = false; //预热一次失败,后面将不进行
 | 
			
		||||
		}
 | 
			
		||||
		for (var a = 1; initTestOk && a < pool.Policy.PoolSize; a += 10) {
 | 
			
		||||
		for (var a = 1; initTestOk && a < minPoolSize; a += 10) {
 | 
			
		||||
			if (initStartTime.Subtract(DateTime.Now).TotalSeconds > 3) break; //预热耗时超过3秒,退出
 | 
			
		||||
			var b = Math.Min(pool.Policy.PoolSize - a, 10); //每10个预热
 | 
			
		||||
			var b = Math.Min(minPoolSize - a, 10); //每10个预热
 | 
			
		||||
			var initTasks = new Task[b];
 | 
			
		||||
			for (var c = 0; c < b; c++) {
 | 
			
		||||
				initTasks[c] = Task.Run(() => {
 | 
			
		||||
 
 | 
			
		||||
@@ -69,7 +69,15 @@ namespace FreeSql.MySql {
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
				var minPoolSize = 0;
 | 
			
		||||
				pattern = @"Min\s*pool\s*size\s*=\s*(\d+)";
 | 
			
		||||
				m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase);
 | 
			
		||||
				if (m.Success) {
 | 
			
		||||
					minPoolSize = int.Parse(m.Groups[1].Value);
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool, minPoolSize);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -83,7 +83,15 @@ namespace FreeSql.Oracle {
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
				var minPoolSize = 0;
 | 
			
		||||
				pattern = @"Min\s*pool\s*size\s*=\s*(\d+)";
 | 
			
		||||
				m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase);
 | 
			
		||||
				if (m.Success) {
 | 
			
		||||
					minPoolSize = int.Parse(m.Groups[1].Value);
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool, minPoolSize);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -62,9 +62,9 @@ namespace FreeSql.PostgreSQL {
 | 
			
		||||
			set {
 | 
			
		||||
				_connectionString = value ?? "";
 | 
			
		||||
 | 
			
		||||
				var pattern = @"Maximum\s*pool\s*size\s*=\s*(\d+)";
 | 
			
		||||
				var pattern = @"Max(imum)?\s*pool\s*size\s*=\s*(\d+)";
 | 
			
		||||
				Match m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase);
 | 
			
		||||
				if (m.Success == false || int.TryParse(m.Groups[1].Value, out var poolsize) == false || poolsize <= 0) poolsize = 100;
 | 
			
		||||
				if (m.Success == false || int.TryParse(m.Groups[2].Value, out var poolsize) == false || poolsize <= 0) poolsize = 50;
 | 
			
		||||
				var connStrIncr = dicConnStrIncr.AddOrUpdate(_connectionString, 1, (oldkey, oldval) => oldval + 1);
 | 
			
		||||
				PoolSize = poolsize + connStrIncr;
 | 
			
		||||
				_connectionString = m.Success ?
 | 
			
		||||
@@ -78,7 +78,15 @@ namespace FreeSql.PostgreSQL {
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
				var minPoolSize = 0;
 | 
			
		||||
				pattern = @"Min(imum)?\s*pool\s*size\s*=\s*(\d+)";
 | 
			
		||||
				m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase);
 | 
			
		||||
				if (m.Success) {
 | 
			
		||||
					minPoolSize = int.Parse(m.Groups[2].Value);
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool, minPoolSize);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -73,7 +73,15 @@ namespace FreeSql.SqlServer {
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
				var minPoolSize = 0;
 | 
			
		||||
				pattern = @"Min\s*pool\s*size\s*=\s*(\d+)";
 | 
			
		||||
				m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase);
 | 
			
		||||
				if (m.Success) {
 | 
			
		||||
					minPoolSize = int.Parse(m.Groups[1].Value);
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool, minPoolSize);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -74,13 +74,21 @@ namespace FreeSql.Sqlite {
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				var minPoolSize = 0;
 | 
			
		||||
				pattern = @"Min\s*pool\s*size\s*=\s*(\d+)";
 | 
			
		||||
				m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase);
 | 
			
		||||
				if (m.Success) {
 | 
			
		||||
					minPoolSize = int.Parse(m.Groups[1].Value);
 | 
			
		||||
					_connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				var att = Regex.Split(_connectionString, @"Attachs\s*=\s*", RegexOptions.IgnoreCase);
 | 
			
		||||
				if (att.Length == 2) {
 | 
			
		||||
					var idx = att[1].IndexOf(';');
 | 
			
		||||
					Attaches = (idx == -1 ? att[1] : att[1].Substring(0, idx)).Split(',');
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool);
 | 
			
		||||
				FreeUtil.PrevReheatConnectionPool(_pool, minPoolSize);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user