Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pool/SNOW-937189 #3/4 busy sessions in pool #793

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading