Skip to content

Commit

Permalink
SNOW-937189 busy sessions counting in the pool
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-mhofman committed Nov 13, 2023
1 parent af36804 commit 186a6ea
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Snowflake.Data/Core/Session/SessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sealed class SessionPool : IDisposable
internal string ConnectionString { get; }
internal SecureString Password { get; }
private bool _pooling = true;
private int _busySessions;
private bool _allowExceedMaxPoolSize = true;

private SessionPool()
Expand All @@ -36,6 +37,7 @@ private SessionPool()
_idleSessions = new List<SFSession>();
_maxPoolSize = MaxPoolSize;
_timeout = Timeout;
_busySessions = 0;
}
}

Expand Down Expand Up @@ -143,6 +145,7 @@ private SFSession NewSession(String connectionString, SecureString password)
try
{
var session = s_sessionFactory.NewSession(connectionString, password);
_busySessions++;
session.Open();
return session;
}
Expand All @@ -163,6 +166,7 @@ private Task<SFSession> NewSessionAsync(String connectionString, SecureString pa
{
s_logger.Debug("SessionPool::NewSessionAsync");
var session = s_sessionFactory.NewSession(connectionString, password);
_busySessions++;
return session
.OpenAsync(cancellationToken)
.ContinueWith(previousTask =>
Expand All @@ -187,15 +191,19 @@ internal bool AddSession(SFSession session)
return false;
long timeNow = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (session.IsNotOpen() || session.IsExpired(_timeout, timeNow))
{
lock (s_sessionPoolLock)
{
_busySessions = Math.Max(_busySessions - 1, 0);
}
return false;
}

lock (s_sessionPoolLock)
{
if (_idleSessions.Count >= _maxPoolSize)
{
CleanExpiredSessions();
}
if (_idleSessions.Count >= _maxPoolSize)
_busySessions = Math.Max(_busySessions - 1, 0);
CleanExpiredSessions();
if (GetCurrentPoolSize() >= _maxPoolSize)
{
s_logger.Warn($"Pool is full - unable to add session with sid {session.sessionId}");
return false;
Expand Down Expand Up @@ -252,6 +260,8 @@ public long GetTimeout()

public int GetCurrentPoolSize()
{
if (!_allowExceedMaxPoolSize)
return _idleSessions.Count + _busySessions;
return _idleSessions.Count;
}

Expand Down

0 comments on commit 186a6ea

Please sign in to comment.