Skip to content

Commit

Permalink
SNOW-902611 refactor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-mhofman committed Oct 9, 2023
1 parent 09c7b3d commit 1616fed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
5 changes: 0 additions & 5 deletions Snowflake.Data/Client/SnowflakeDbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,8 @@ public void SetArrayBindStageCreated()
_isArrayBindStageCreated = true;
}

/// <summary>
/// Create a new SFsession with the connection string settings.
/// </summary>
/// <exception cref="SnowflakeDbException">If the connection string can't be processed</exception>
private void OnSessionConnecting()
{
// SfSession = new SFSession(ConnectionString, Password);
_connectionState = ConnectionState.Connecting;
}

Expand Down
47 changes: 18 additions & 29 deletions Snowflake.Data/Core/Session/SessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static SessionPoolSingleton Instance

private void CleanExpiredSessions()
{
s_logger.Debug("SessionPool::cleanExpiredSessions");
s_logger.Debug("SessionPool::CleanExpiredSessions");
lock (s_sessionPoolLock)
{
long timeNow = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
Expand All @@ -79,36 +79,22 @@ internal SFSession GetSession(string connStr, SecureString password)
s_logger.Debug("SessionPool::GetSession");
if (!_pooling)
return NewSession(connStr, password);
lock (s_sessionPoolLock)
{
for (int i = 0; i < _sessionPool.Count; i++)
{
if (_sessionPool[i].connStr.Equals(connStr))
{
SFSession session = _sessionPool[i];
_sessionPool.RemoveAt(i);
long timeNow = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (session.IsExpired(_timeout, timeNow))
{
session.close();
i--;
}
else
{
s_logger.Debug($"reuse pooled session with sid {session.sessionId}");
return session;
}
}
}
}
return NewSession(connStr, password);
SFSession session = GetIdleSession(connStr);
return session ?? NewSession(connStr, password);
}

internal Task<SFSession> GetSessionAsync(string connStr, SecureString password, CancellationToken cancellationToken)
{
s_logger.Debug("SessionPool::GetSession");
s_logger.Debug("SessionPool::GetSessionAsync");
if (!_pooling)
return NewSessionAsync(connStr, password, cancellationToken);
SFSession session = GetIdleSession(connStr);
return session != null ? Task.FromResult(session) : NewSessionAsync(connStr, password, cancellationToken);
}

private SFSession GetIdleSession(string connStr)
{
s_logger.Debug("SessionPool::GetIdleSession");
lock (s_sessionPoolLock)
{
for (int i = 0; i < _sessionPool.Count; i++)
Expand All @@ -126,16 +112,17 @@ internal Task<SFSession> GetSessionAsync(string connStr, SecureString password,
else
{
s_logger.Debug($"reuse pooled session with sid {session.sessionId}");
return Task.FromResult(session);
return session;
}
}
}
}
return NewSessionAsync(connStr, password, cancellationToken);
return null;
}

internal SFSession NewSession(String connectionString, SecureString password)
private SFSession NewSession(String connectionString, SecureString password)
{
s_logger.Debug("SessionPool::NewSession");
try
{
var session = new SFSession(connectionString, password);
Expand All @@ -155,8 +142,9 @@ internal SFSession NewSession(String connectionString, SecureString password)
}
}

internal Task<SFSession> NewSessionAsync(String connectionString, SecureString password, CancellationToken cancellationToken)
private Task<SFSession> NewSessionAsync(String connectionString, SecureString password, CancellationToken cancellationToken)
{
s_logger.Debug("SessionPool::NewSessionAsync");
try
{
var session = new SFSession(connectionString, password);
Expand Down Expand Up @@ -255,6 +243,7 @@ public int GetCurrentPoolSize()

public bool SetPooling(bool isEnable)
{
s_logger.Info($"SessionPool::SetPooling({isEnable})");
if (_pooling == isEnable)
return false;
_pooling = isEnable;
Expand Down

0 comments on commit 1616fed

Please sign in to comment.