From fa19879a176c0e64d747d6aa476f69ade7d9c1be Mon Sep 17 00:00:00 2001 From: "Andrey.Kudashkin" Date: Sun, 31 Jan 2021 06:34:16 +0300 Subject: [PATCH] 3396-pool-delete-on-pruning: Support for changes #3485 --- src/Npgsql/NpgsqlConnection.cs | 9 --------- src/Npgsql/NpgsqlEventSource.cs | 8 ++++++++ src/Npgsql/PoolManager.cs | 14 +++++++++++++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Npgsql/NpgsqlConnection.cs b/src/Npgsql/NpgsqlConnection.cs index 7467e7de51..0739ac9a5a 100644 --- a/src/Npgsql/NpgsqlConnection.cs +++ b/src/Npgsql/NpgsqlConnection.cs @@ -189,15 +189,6 @@ void GetPoolAndSettings() var newPool = new ConnectorPool(Settings, canonical); _pool = PoolManager.GetOrAdd(canonical, newPool); - // If the pool we created was the one that ended up being stored we need to increment the appropriate counter. - // Avoids a race condition where multiple threads will create a pool but only one will be stored. - if (_pool == newPool) - { - // If the pool we created was the one that ended up being stored we need to increment the appropriate counter. - // Avoids a race condition where multiple threads will create a pool but only one will be stored. - NpgsqlEventSource.Log.PoolCreated(newPool); - } - _pool = PoolManager.GetOrAdd(_connectionString, _pool); } diff --git a/src/Npgsql/NpgsqlEventSource.cs b/src/Npgsql/NpgsqlEventSource.cs index f9355f9108..4e3435d075 100644 --- a/src/Npgsql/NpgsqlEventSource.cs +++ b/src/Npgsql/NpgsqlEventSource.cs @@ -89,6 +89,14 @@ internal void PoolCreated(ConnectorPool pool) } } + internal void PoolDeleted(ConnectorPool pool) + { + lock (_poolsLock) + { + _pools.Remove(pool); + } + } + internal void MultiplexingBatchSent(int numCommands, int waits, Stopwatch stopwatch) { // TODO: CAS loop instead of 4 separate interlocked operations? diff --git a/src/Npgsql/PoolManager.cs b/src/Npgsql/PoolManager.cs index 21bd0b977c..b63a161bf4 100644 --- a/src/Npgsql/PoolManager.cs +++ b/src/Npgsql/PoolManager.cs @@ -76,6 +76,9 @@ internal static ConnectorPool GetOrAdd(string key, ConnectorPool pool) _pools[_nextSlot].Key = key; _pools[_nextSlot].Pool = pool; Interlocked.Increment(ref _nextSlot); + + NpgsqlEventSource.Log.PoolCreated(pool); + return pool; } } @@ -110,7 +113,9 @@ internal static void Delete(ConnectorPool pool) Interlocked.Decrement(ref _nextSlot); _pools = newPools; - } + + NpgsqlEventSource.Log.PoolDeleted(pool); + } } internal static void Clear(string connString) @@ -151,6 +156,13 @@ internal static void Reset() lock (Lock) { ClearAll(); + var pools = _pools; + foreach (var (_, pool) in pools) + { + if (pool != null) + NpgsqlEventSource.Log.PoolDeleted(pool); + } + _pools = new (string, ConnectorPool)[InitialPoolsSize]; _nextSlot = 0; }