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-902611 #1/4 removed singleton pattern from SessionPool; introduced ne… #791

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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);
}
}
}
47 changes: 30 additions & 17 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)
public static void SetMaxPoolSize(int maxPoolSize)
{
SessionPoolSingleton.Instance.SetMaxPoolSize(size);
s_logger.Debug("SnowflakeDbConnectionPool::SetMaxPoolSize");
s_connectionManager.SetMaxPoolSize(maxPoolSize);
}

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

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

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
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
{
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
{
internal interface IConnectionManager
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
{
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
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
{
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;
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}

}
}
Loading