Skip to content

Commit

Permalink
SNOW-902611 removed singleton pattern from SessionPool; introduced ne…
Browse files Browse the repository at this point in the history
…w interface for ConnectionManager; split tests of ConnectionPool and some cleanup in the classes
  • Loading branch information
sfc-gh-mhofman committed Oct 11, 2023
1 parent 127343f commit ca04c5b
Show file tree
Hide file tree
Showing 8 changed files with 887 additions and 813 deletions.
361 changes: 361 additions & 0 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolAsyncIT.cs

Large diffs are not rendered by default.

411 changes: 411 additions & 0 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolIT.cs

Large diffs are not rendered by default.

775 changes: 0 additions & 775 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolT.cs

This file was deleted.

29 changes: 29 additions & 0 deletions Snowflake.Data.Tests/Util/PoolConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2012-2021 Snowflake Computing Inc. All rights reserved.
*/

using Snowflake.Data.Client;

namespace Snowflake.Data.Tests.Util
{
class PoolConfig
{
private readonly bool _pooling;
private readonly long _timeout;
private readonly int _maxPoolSize;

public PoolConfig()
{
_maxPoolSize = SnowflakeDbConnectionPool.GetMaxPoolSize();
_timeout = SnowflakeDbConnectionPool.GetTimeout();
_pooling = SnowflakeDbConnectionPool.GetPooling();
}

public void Reset()
{
SnowflakeDbConnectionPool.SetMaxPoolSize(_maxPoolSize);
SnowflakeDbConnectionPool.SetTimeout(_timeout);
SnowflakeDbConnectionPool.SetPooling(_pooling);
}
}
}
43 changes: 28 additions & 15 deletions Snowflake.Data/Client/SnowflakeDbConnectionPool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Security;
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

using System.Security;
using System.Threading;
using System.Threading.Tasks;
using Snowflake.Data.Core;
Expand All @@ -10,63 +14,72 @@ namespace Snowflake.Data.Client
public class SnowflakeDbConnectionPool
{
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SnowflakeDbConnectionPool>();

internal static SFSession GetSession(string connStr, SecureString password)
private static readonly IConnectionManager s_connectionManager = new ConnectionManagerV1();

internal static SFSession GetSession(string connectionString, SecureString password)
{
s_logger.Debug("SnowflakeDbConnectionPool::GetSession");
return SessionPoolSingleton.Instance.GetSession(connStr, password);
return s_connectionManager.GetSession(connectionString, password);
}

internal static Task<SFSession> GetSessionAsync(string connStr, SecureString password, CancellationToken cancellationToken)
internal static Task<SFSession> GetSessionAsync(string connectionString, SecureString password, CancellationToken cancellationToken)
{
return SessionPoolSingleton.Instance.GetSessionAsync(connStr, password, cancellationToken);
s_logger.Debug("SnowflakeDbConnectionPool::GetSessionAsync");
return s_connectionManager.GetSessionAsync(connectionString, password, cancellationToken);
}

internal static bool AddSession(SFSession session)
{
s_logger.Debug("SnowflakeDbConnectionPool::AddSession");
return SessionPoolSingleton.Instance.AddSession(session);
return s_connectionManager.AddSession(session);
}

public static void ClearAllPools()
{
s_logger.Debug("SnowflakeDbConnectionPool::ClearAllPools");
SessionPoolSingleton.Instance.ClearAllPools();
s_connectionManager.ClearAllPools();
}

public static void SetMaxPoolSize(int size)
{
SessionPoolSingleton.Instance.SetMaxPoolSize(size);
s_logger.Debug("SnowflakeDbConnectionPool::SetMaxPoolSize");
s_connectionManager.SetMaxPoolSize(size);
}

public static int GetMaxPoolSize()
{
return SessionPoolSingleton.Instance.GetMaxPoolSize();
s_logger.Debug("SnowflakeDbConnectionPool::GetMaxPoolSize");
return s_connectionManager.GetMaxPoolSize();
}

public static void SetTimeout(long time)
{
SessionPoolSingleton.Instance.SetTimeout(time);
s_logger.Debug("SnowflakeDbConnectionPool::SetTimeout");
s_connectionManager.SetTimeout(time);
}

public static long GetTimeout()
{
return SessionPoolSingleton.Instance.GetTimeout();
s_logger.Debug("SnowflakeDbConnectionPool::GetTimeout");
return s_connectionManager.GetTimeout();
}

public static int GetCurrentPoolSize()
{
return SessionPoolSingleton.Instance.GetCurrentPoolSize();
s_logger.Debug("SnowflakeDbConnectionPool::GetCurrentPoolSize");
return s_connectionManager.GetCurrentPoolSize();
}

public static bool SetPooling(bool isEnable)
{
return SessionPoolSingleton.Instance.SetPooling(isEnable);
s_logger.Debug("SnowflakeDbConnectionPool::SetPooling");
return s_connectionManager.SetPooling(isEnable);
}

public static bool GetPooling()
{
return SessionPoolSingleton.Instance.GetPooling();
s_logger.Debug("SnowflakeDbConnectionPool::GetPooling");
return s_connectionManager.GetPooling();
}
}
}
23 changes: 23 additions & 0 deletions Snowflake.Data/Core/Session/ConnectionManagerV1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Security;
using System.Threading;
using System.Threading.Tasks;

namespace Snowflake.Data.Core.Session
{
internal sealed class ConnectionManagerV1 : IConnectionManager
{
private readonly SessionPool _sessionPool = new SessionPool();
public SFSession GetSession(string connectionString, SecureString password) => _sessionPool.GetSession(connectionString, password);
public Task<SFSession> GetSessionAsync(string connectionString, SecureString password, CancellationToken cancellationToken)
=> _sessionPool.GetSessionAsync(connectionString, password, cancellationToken);
public bool AddSession(SFSession session) => _sessionPool.AddSession(session);
public void ClearAllPools() => _sessionPool.ClearAllPools();
public void SetMaxPoolSize(int maxPoolSize) => _sessionPool.SetMaxPoolSize(maxPoolSize);
public int GetMaxPoolSize() => _sessionPool.GetMaxPoolSize();
public void SetTimeout(long connectionTimeout) => _sessionPool.SetTimeout(connectionTimeout);
public long GetTimeout() => _sessionPool.GetTimeout();
public int GetCurrentPoolSize() => _sessionPool.GetCurrentPoolSize();
public bool SetPooling(bool poolingEnabled) => _sessionPool.SetPooling(poolingEnabled);
public bool GetPooling() => _sessionPool.GetPooling();
}
}
25 changes: 25 additions & 0 deletions Snowflake.Data/Core/Session/IConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

using System.Security;
using System.Threading;
using System.Threading.Tasks;

namespace Snowflake.Data.Core.Session
{
public interface IConnectionManager
{
SFSession GetSession(string connectionString, SecureString password);
Task<SFSession> GetSessionAsync(string connectionString, SecureString password, CancellationToken cancellationToken);
bool AddSession(SFSession session);
void ClearAllPools();
void SetMaxPoolSize(int maxPoolSize);
int GetMaxPoolSize();
void SetTimeout(long connectionTimeout);
long GetTimeout();
int GetCurrentPoolSize();
bool SetPooling(bool poolingEnabled);
bool GetPooling();
}
}
33 changes: 10 additions & 23 deletions Snowflake.Data/Core/Session/SessionPool.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,20 +13,18 @@

namespace Snowflake.Data.Core.Session
{
sealed class SessionPoolSingleton : IDisposable
sealed class SessionPool : IDisposable
{
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SessionPoolSingleton>();
private static SessionPoolSingleton s_instance = null;
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SessionPool>();
private static readonly object s_sessionPoolLock = new object();

private readonly List<SFSession> _sessionPool;
private int _maxPoolSize;
private long _timeout;
private const int MaxPoolSize = 10;
private const long Timeout = 3600;
private bool _pooling = true;

SessionPoolSingleton()
internal SessionPool()
{
lock (s_sessionPoolLock)
{
Expand All @@ -31,7 +33,8 @@ sealed class SessionPoolSingleton : IDisposable
_timeout = Timeout;
}
}
~SessionPoolSingleton()

~SessionPool()
{
ClearAllPools();
}
Expand All @@ -41,21 +44,6 @@ public void Dispose()
ClearAllPools();
}

public static SessionPoolSingleton Instance
{
get
{
lock (s_sessionPoolLock)
{
if(s_instance == null)
{
s_instance = new SessionPoolSingleton();
}
return s_instance;
}
}
}

private void CleanExpiredSessions()
{
s_logger.Debug("SessionPool::CleanExpiredSessions");
Expand Down Expand Up @@ -246,5 +234,4 @@ public bool GetPooling()
return _pooling;
}
}

}
}

0 comments on commit ca04c5b

Please sign in to comment.