Fixed event counters (busy/idle connections count)#3476
Fixed event counters (busy/idle connections count)#3476neyromant wants to merge 4 commits intonpgsql:mainfrom
Conversation
roji
left a comment
There was a problem hiding this comment.
@neyromant thanks for spotting this - you're right with your analysis of the bug.
The fix looks good overall (see some comments below). However, I'm a bit worried about adding more book-keeping inside PoolManager, specifically with respect to #3396. If we wanted to, we could also fix this by deduplicating the pools inside NpgsqlEventSource with a HashSet (though it's true this is better done once when adding pools, rather than every time).
@vonzshik what do you think?
PS I've opened #3480 to have an issue tracking this (not just a PR)
src/Npgsql/PoolManager.cs
Outdated
|
|
||
| var idx = 0; | ||
| ConnectorPool? existsPool = null; | ||
| for (; idx < _pools.Length; idx++) |
There was a problem hiding this comment.
Note that in NpgsqlConnection we've already found the pool via the canonical/normalized connection string, so we could just call a different method here rather than searching for it again.
There was a problem hiding this comment.
Not sure if I understood correctly ... Made an additional parameter to let me know if a new pool is being registered or an existing one.
Co-authored-by: Shay Rojansky <[email protected]>
Thanks for the review, I made some edits. // NpgsqlEventSource:
// internal void PoolCreated() => Interlocked.Increment(ref _pools);
internal void PoolCreated (ConnectorPool pool)
{
lock(Lock)
{
PoolsHashSet.Add(pool);
}
} If lock is enough to solve the thread safety, I can do a different PR |
I'll all for this, as the pool manager is already not that simple. As a possible solution, we can set a flag every time we add or remove a pool to reset the HashSet. |
I personally just don't like the idea of adding another array for the PoolManager to handle - for me it sounds like asking for more bugs. But if no one else has any objections, then I guess... |
|
@vonzshik I'm OK either way. It should be OK on PoolManager - as long as we make it clear that the pools array/HashSet isn't reliable in the same sense as the one actually used when opening a connection. But am also OK with keeping it out. |
|
@vonzshik @roji I created a pr #3481 with an alternative approach - accounting for pools in NpgsqlEventSource. |
|
Fixed in #3485 |
If a non-canonical connection string is used (e.g. trailing semicolon), EventCounters produces idle and busy connections count different from the actual ones.
The reason: