Skip to content

Commit

Permalink
SNOW-902611 review fixes in Session Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-mhofman committed Oct 13, 2023
1 parent 94c2168 commit ed7d989
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SFConnectionPoolITAsync : SFBaseTestAsync
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFConnectionPoolITAsync>();

[OneTimeSetUp]
public void BeforeAllTests()
public static void BeforeAllTests()
{
s_previousPoolConfigRestorer = new PoolConfig();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SFConnectionPoolIT : SFBaseTest
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFConnectionPoolITAsync>();

[OneTimeSetUp]
public void BeforeAllTests()
public static void BeforeAllTests()
{
s_previousPoolConfig = new PoolConfig();
}
Expand Down
2 changes: 1 addition & 1 deletion Snowflake.Data/Client/SnowflakeDbConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Snowflake.Data.Client
public class SnowflakeDbConnectionPool
{
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SnowflakeDbConnectionPool>();
private static readonly IConnectionManager s_connectionManager = new ConnectionManagerV1();
private static readonly IConnectionManager s_connectionManager = new ConnectionCacheManager();

internal static SFSession GetSession(string connectionString, SecureString password)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 14 additions & 14 deletions Snowflake.Data/Core/Session/SessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sealed class SessionPool : IDisposable
{
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SessionPool>();
private static readonly object s_sessionPoolLock = new object();
private readonly List<SFSession> _sessionPool;
private readonly List<SFSession> _sessions;
private int _maxPoolSize;
private long _timeout;
private const int MaxPoolSize = 10;
Expand All @@ -28,7 +28,7 @@ internal SessionPool()
{
lock (s_sessionPoolLock)
{
_sessionPool = new List<SFSession>();
_sessions = new List<SFSession>();
_maxPoolSize = MaxPoolSize;
_timeout = Timeout;
}
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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();
}
}

Expand All @@ -213,7 +213,7 @@ public long GetTimeout()

public int GetCurrentPoolSize()
{
return _sessionPool.Count;
return _sessions.Count;
}

public bool SetPooling(bool isEnable)
Expand Down

0 comments on commit ed7d989

Please sign in to comment.