From bcee106f8e61fcb5658628e7756e22007419336f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Fri, 13 Oct 2023 12:48:13 +0200 Subject: [PATCH] SNOW-902611 review fixes in Session Pool --- .../SFConnectionPoolAsyncIT.cs | 4 +-- .../IntegrationTests/SFConnectionPoolIT.cs | 4 +-- .../Client/SnowflakeDbConnectionPool.cs | 2 +- ...ManagerV1.cs => ConnectionCacheManager.cs} | 2 +- Snowflake.Data/Core/Session/SessionPool.cs | 28 +++++++++---------- 5 files changed, 18 insertions(+), 22 deletions(-) rename Snowflake.Data/Core/Session/{ConnectionManagerV1.cs => ConnectionCacheManager.cs} (94%) diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolAsyncIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolAsyncIT.cs index bfae38b26..05f7ed17f 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolAsyncIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolAsyncIT.cs @@ -21,10 +21,9 @@ namespace Snowflake.Data.Tests.IntegrationTests class SFConnectionPoolITAsync : SFBaseTestAsync { private static PoolConfig s_previousPoolConfigRestorer; - private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); [OneTimeSetUp] - public void BeforeAllTests() + public static void BeforeAllTests() { s_previousPoolConfigRestorer = new PoolConfig(); } @@ -69,7 +68,6 @@ public void TestConnectionPoolWithAsync() } catch (SnowflakeDbException ex) { - s_logger.Error("connection failed:" + ex); conn.CloseAsync(connectionCancelToken.Token); } diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolIT.cs index 686e8493a..5c4529225 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolIT.cs @@ -19,10 +19,9 @@ namespace Snowflake.Data.Tests.IntegrationTests class SFConnectionPoolIT : SFBaseTest { private static PoolConfig s_previousPoolConfig; - private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); [OneTimeSetUp] - public void BeforeAllTests() + public static void BeforeAllTests() { s_previousPoolConfig = new PoolConfig(); } @@ -369,7 +368,6 @@ public void TestConnectionPoolWithDispose() } catch (SnowflakeDbException ex) { - s_logger.Error("connection failed:" + ex); conn1.Close(); } diff --git a/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs b/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs index da9de769b..f643fa5c9 100644 --- a/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs +++ b/Snowflake.Data/Client/SnowflakeDbConnectionPool.cs @@ -14,7 +14,7 @@ namespace Snowflake.Data.Client public class SnowflakeDbConnectionPool { private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); - private static readonly IConnectionManager s_connectionManager = new ConnectionManagerV1(); + private static readonly IConnectionManager s_connectionManager = new ConnectionCacheManager(); internal static SFSession GetSession(string connectionString, SecureString password) { diff --git a/Snowflake.Data/Core/Session/ConnectionManagerV1.cs b/Snowflake.Data/Core/Session/ConnectionCacheManager.cs similarity index 94% rename from Snowflake.Data/Core/Session/ConnectionManagerV1.cs rename to Snowflake.Data/Core/Session/ConnectionCacheManager.cs index 0ad0520e0..e10a984e3 100644 --- a/Snowflake.Data/Core/Session/ConnectionManagerV1.cs +++ b/Snowflake.Data/Core/Session/ConnectionCacheManager.cs @@ -4,7 +4,7 @@ namespace Snowflake.Data.Core.Session { - internal sealed class ConnectionManagerV1 : IConnectionManager + internal sealed class ConnectionCacheManager : IConnectionManager { private readonly SessionPool _sessionPool = new SessionPool(); public SFSession GetSession(string connectionString, SecureString password) => _sessionPool.GetSession(connectionString, password); diff --git a/Snowflake.Data/Core/Session/SessionPool.cs b/Snowflake.Data/Core/Session/SessionPool.cs index 5acd72855..5013da99f 100644 --- a/Snowflake.Data/Core/Session/SessionPool.cs +++ b/Snowflake.Data/Core/Session/SessionPool.cs @@ -17,7 +17,7 @@ sealed class SessionPool : IDisposable { private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); private static readonly object s_sessionPoolLock = new object(); - private readonly List _sessionPool; + private readonly List _sessions; private int _maxPoolSize; private long _timeout; private const int MaxPoolSize = 10; @@ -28,7 +28,7 @@ internal SessionPool() { lock (s_sessionPoolLock) { - _sessionPool = new List(); + _sessions = new List(); _maxPoolSize = MaxPoolSize; _timeout = Timeout; } @@ -51,11 +51,11 @@ private void CleanExpiredSessions() { long timeNow = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); - foreach (var item in _sessionPool.ToList()) + foreach (var item in _sessions.ToList()) { if (item.IsExpired(_timeout, timeNow)) { - _sessionPool.Remove(item); + _sessions.Remove(item); item.close(); } } @@ -85,12 +85,12 @@ private SFSession GetIdleSession(string connStr) s_logger.Debug("SessionPool::GetIdleSession"); lock (s_sessionPoolLock) { - for (int i = 0; i < _sessionPool.Count; i++) + for (int i = 0; i < _sessions.Count; i++) { - if (_sessionPool[i].connStr.Equals(connStr)) + if (_sessions[i].connStr.Equals(connStr)) { - SFSession session = _sessionPool[i]; - _sessionPool.RemoveAt(i); + SFSession session = _sessions[i]; + _sessions.RemoveAt(i); long timeNow = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); if (session.IsExpired(_timeout, timeNow)) { @@ -162,18 +162,18 @@ internal bool AddSession(SFSession session) lock (s_sessionPoolLock) { - if (_sessionPool.Count >= _maxPoolSize) + if (_sessions.Count >= _maxPoolSize) { CleanExpiredSessions(); } - if (_sessionPool.Count >= _maxPoolSize) + if (_sessions.Count >= _maxPoolSize) { // pool is full return false; } s_logger.Debug($"pool connection with sid {session.sessionId}"); - _sessionPool.Add(session); + _sessions.Add(session); return true; } } @@ -183,11 +183,11 @@ internal void ClearAllPools() s_logger.Debug("SessionPool::ClearAllPools"); lock (s_sessionPoolLock) { - foreach (SFSession session in _sessionPool) + foreach (SFSession session in _sessions) { session.close(); } - _sessionPool.Clear(); + _sessions.Clear(); } } @@ -213,7 +213,7 @@ public long GetTimeout() public int GetCurrentPoolSize() { - return _sessionPool.Count; + return _sessions.Count; } public bool SetPooling(bool isEnable)