Skip to content

Commit

Permalink
SNOW-1353952 GetPool interface driven with user/password
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-mhofman committed Apr 29, 2024
1 parent 43d8fd4 commit 48463c7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
18 changes: 12 additions & 6 deletions Snowflake.Data/Client/SnowflakeDbConnectionPool.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
* Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
*/

using System;
Expand Down Expand Up @@ -29,25 +29,31 @@ private static IConnectionManager ConnectionManager
return s_connectionManager;
}
}

internal static SFSession GetSession(string connectionString, SecureString password)
{
s_logger.Debug($"SnowflakeDbConnectionPool::GetSession");
return ConnectionManager.GetSession(connectionString, password);
}

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

public static SnowflakeDbSessionPool GetPool(string connectionString, SecureString password)
{
s_logger.Debug($"SnowflakeDbConnectionPool::GetPool");
return new SnowflakeDbSessionPool(ConnectionManager.GetPool(connectionString, password));
}

internal static SessionPool GetPool(string connectionString)
{
s_logger.Debug($"SnowflakeDbConnectionPool::GetPool");
return ConnectionManager.GetPool(connectionString);
}

internal static bool AddSession(SFSession session)
{
s_logger.Debug("SnowflakeDbConnectionPool::AddSession");
Expand Down Expand Up @@ -83,7 +89,7 @@ public static void SetTimeout(long connectionTimeout)
s_logger.Debug("SnowflakeDbConnectionPool::SetTimeout");
ConnectionManager.SetTimeout(connectionTimeout);
}

public static long GetTimeout()
{
s_logger.Debug("SnowflakeDbConnectionPool::GetTimeout");
Expand All @@ -110,7 +116,7 @@ public static bool GetPooling()

internal static void SetOldConnectionPoolVersion() // TODO: set to public once development of entire ConnectionPoolManager epic is complete
{
SetConnectionPoolVersion(ConnectionPoolType.SingleConnectionCache);
SetConnectionPoolVersion(ConnectionPoolType.SingleConnectionCache);
}

internal static void SetConnectionPoolVersion(ConnectionPoolType requestedPoolType)
Expand Down
28 changes: 28 additions & 0 deletions Snowflake.Data/Client/SnowflakeDbSessionPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
*/
using System;
using Snowflake.Data.Core.Session;

namespace Snowflake.Data.Client
{
public class SnowflakeDbSessionPool : IDisposable
{
private SessionPool _sessionPool;

internal SnowflakeDbSessionPool(SessionPool sessionPool)
=> _sessionPool = sessionPool ?? throw new NullReferenceException("SessionPool not provided!");
public void Dispose() => _sessionPool = null;

public void SetMaxPoolSize(int size) => _sessionPool.SetMaxPoolSize(size);
public int GetMaxPoolSize() => _sessionPool.GetMaxPoolSize();

public void SetTimeout(long seconds) => _sessionPool.SetTimeout(seconds);
public long GetTimeout() => _sessionPool.GetTimeout();

public int GetCurrentPoolSize() => _sessionPool.GetCurrentPoolSize();

public bool SetPooling(bool isEnable) => _sessionPool.SetPooling(isEnable);
public bool GetPooling() => _sessionPool.GetPooling();
}
}
3 changes: 2 additions & 1 deletion Snowflake.Data/Core/Session/ConnectionCacheManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
* Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
*/

using System.Security;
Expand All @@ -25,5 +25,6 @@ public Task<SFSession> GetSessionAsync(string connectionString, SecureString pas
public bool SetPooling(bool poolingEnabled) => _sessionPool.SetPooling(poolingEnabled);
public bool GetPooling() => _sessionPool.GetPooling();
public SessionPool GetPool(string _) => _sessionPool;
public SessionPool GetPool(string _, SecureString __) => _sessionPool;
}
}
19 changes: 12 additions & 7 deletions Snowflake.Data/Core/Session/ConnectionPoolManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
* Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
*/

using System;
Expand Down Expand Up @@ -27,7 +27,7 @@ internal ConnectionPoolManager()
_pools = new Dictionary<string, SessionPool>();
}
}

public SFSession GetSession(string connectionString, SecureString password)
{
s_logger.Debug($"ConnectionPoolManager::GetSession");
Expand Down Expand Up @@ -57,11 +57,11 @@ public void ClearAllPools()
s_logger.Debug("ConnectionPoolManager::ClearAllPools");
foreach (var sessionPool in _pools.Values)
{
sessionPool.ClearSessions();
sessionPool.ClearSessions();
}
_pools.Clear();
}

public void SetMaxPoolSize(int maxPoolSize)
{
throw s_operationNotAvailable;
Expand Down Expand Up @@ -104,7 +104,7 @@ public bool SetPooling(bool poolingEnabled)
throw s_operationNotAvailable;
}

public bool GetPooling()
public bool GetPooling()
{
s_logger.Debug("ConnectionPoolManager::GetPooling");
var values = _pools.Values.Select(it => it.GetPooling()).Distinct().ToList();
Expand All @@ -113,11 +113,11 @@ public bool GetPooling()
: throw new SnowflakeDbException(SFError.INCONSISTENT_RESULT_ERROR, "Multiple pools have different Pooling values");
}

internal SessionPool GetPool(string connectionString, SecureString password)
public SessionPool GetPool(string connectionString, SecureString password)
{
s_logger.Debug($"ConnectionPoolManager::GetPool");
var poolKey = GetPoolKey(connectionString);

if (_pools.TryGetValue(poolKey, out var item))
return item;
lock (s_poolsLock)
Expand All @@ -134,6 +134,11 @@ internal SessionPool GetPool(string connectionString, SecureString password)
public SessionPool GetPool(string connectionString)
{
s_logger.Debug($"ConnectionPoolManager::GetPool");
if (!connectionString.ToLower().Contains("password="))
{
s_logger.Error($"To obtain a pool a password must to be given with a connection string or SecureString parameter");
throw new SnowflakeDbException(SFError.MISSING_CONNECTION_PROPERTY, "Could not provide the pool without the password");
}
return GetPool(connectionString, null);
}

Expand Down
3 changes: 2 additions & 1 deletion Snowflake.Data/Core/Session/IConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
* Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
*/

using System.Security;
Expand All @@ -23,5 +23,6 @@ internal interface IConnectionManager
bool SetPooling(bool poolingEnabled);
bool GetPooling();
SessionPool GetPool(string connectionString);
SessionPool GetPool(string connectionString, SecureString password);
}
}

0 comments on commit 48463c7

Please sign in to comment.