Skip to content
Closed
Changes from all commits
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
23 changes: 11 additions & 12 deletions Source/MySql.Data/MySqlPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal sealed class MySqlPool
private bool beingCleared;
private int available;
private AutoResetEvent autoEvent;
private int maxUsedConnections;

private void EnqueueIdle(Driver driver)
{
Expand All @@ -63,6 +64,7 @@ public MySqlPool(MySqlConnectionStringBuilder settings)
inUsePool = new List<Driver>((int)maxSize);
idlePool = new Queue<Driver>((int)maxSize);

maxUsedConnections = (int)minSize;
// prepopulate the idle pool to minSize
for (int i = 0; i < minSize; i++)
EnqueueIdle(CreateNewPooledConnection());
Expand Down Expand Up @@ -158,6 +160,9 @@ private Driver GetPooledConnection()
lock ((inUsePool as ICollection).SyncRoot)
{
inUsePool.Add(driver);
int currentlyInUse = inUsePool.Count;
if (currentlyInUse > maxUsedConnections)
maxUsedConnections = currentlyInUse;
}
return driver;
}
Expand Down Expand Up @@ -227,6 +232,7 @@ public void RemoveConnection(Driver driver)
private Driver TryToGetDriver()
{
int count = Interlocked.Decrement(ref available);

if (count < 0)
{
Interlocked.Increment(ref available);
Expand Down Expand Up @@ -310,21 +316,14 @@ internal List<Driver> RemoveOldIdleConnections()
// The drivers appear to be ordered by their age, i.e it is
// sufficient to remove them until the first element is not
// too old.
while (idlePool.Count > minSize)
int targetSize = minSize > maxUsedConnections ? (int)minSize : maxUsedConnections;
while (idlePool.Count > targetSize)
{
Driver d = idlePool.Peek();
DateTime expirationTime = d.IdleSince.Add(
new TimeSpan(0, 0, MySqlPoolManager.maxConnectionIdleTime));
if (expirationTime.CompareTo(now) < 0)
{
oldDrivers.Add(d);
idlePool.Dequeue();
}
else
{
break;
}
oldDrivers.Add(d);
idlePool.Dequeue();
}
maxUsedConnections = (int)minSize;
}
return oldDrivers;
}
Expand Down