Skip to content

Commit 16413b4

Browse files
committed
fix: remove wait to shrink
1 parent 11a49ee commit 16413b4

1 file changed

Lines changed: 14 additions & 45 deletions

File tree

Pool/Pool.cs

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace Pool;
99
public class Pool<T> : IPool<T> where T : class
1010
{
1111
private readonly TimeSpan _defaultShrinkInterval = TimeSpan.FromMinutes(30);
12-
private TaskCompletionSource<bool> _shrinkCompletionSource;
1312
private readonly System.Timers.Timer _shrinkTimer;
1413
private readonly Action<T> _cleanupAction;
1514
private readonly SemaphoreSlim _semaphore;
@@ -18,7 +17,6 @@ public class Pool<T> : IPool<T> where T : class
1817
private readonly Func<T> _factory;
1918
private readonly int _createIncrement;
2019
private readonly int _maxPoolSize;
21-
private bool _isShrinking;
2220
private int _currentSize;
2321
private bool _disposed;
2422

@@ -84,7 +82,6 @@ public Pool(Func<T> factory, Action<T>? cleanupAction = null, TimeSpan? shrinkIn
8482
_currentSize = initPoolSize;
8583
_createIncrement = createIncrement;
8684
_semaphore = new SemaphoreSlim(maxPoolSize, maxPoolSize);
87-
_shrinkCompletionSource = new TaskCompletionSource<bool>();
8885
_cleanupAction = cleanupAction ?? (item =>
8986
{
9087
if (item is IDisposable disposable)
@@ -119,9 +116,6 @@ public T GetFromPool()
119116

120117
try
121118
{
122-
// Avoid taking items if the pool is shrinking
123-
WaitForShrinkToCompleteAsync().Wait();
124-
125119
_ = _items.TryTake(out var result);
126120

127121
if (result == null)
@@ -165,9 +159,6 @@ public async Task<T> GetFromPoolAsync()
165159

166160
try
167161
{
168-
// Avoid taking items if the pool is shrinking
169-
await WaitForShrinkToCompleteAsync().ConfigureAwait(false);
170-
171162
_ = _items.TryTake(out var result);
172163

173164
if (result == null)
@@ -342,15 +333,6 @@ private void Create()
342333
}
343334
}
344335

345-
private async Task WaitForShrinkToCompleteAsync()
346-
{
347-
if (_isShrinking)
348-
{
349-
// If shrink is in progress, we need to wait until the TaskCompletionSource is set
350-
await (_shrinkCompletionSource?.Task ?? Task.CompletedTask).ConfigureAwait(false);
351-
}
352-
}
353-
354336
private void ShrinkPool(int initPoolSize)
355337
{
356338
lock (_lock)
@@ -360,40 +342,27 @@ private void ShrinkPool(int initPoolSize)
360342
return;
361343
}
362344

363-
try
364-
{
365-
_isShrinking = true;
366-
_ = _shrinkCompletionSource.TrySetResult(true);
345+
var itemsToRemove = Math.Max(0, _currentSize - initPoolSize);
367346

368-
var itemsToRemove = Math.Max(0, _currentSize - initPoolSize);
369-
370-
for (var i = 0; i < itemsToRemove; i++)
347+
for (var i = 0; i < itemsToRemove; i++)
348+
{
349+
try
371350
{
372-
try
351+
if (_items.TryTake(out var item))
373352
{
374-
if (_items.TryTake(out var item))
375-
{
376-
_cleanupAction(item);
377-
378-
_ = Interlocked.Decrement(ref _currentSize);
379-
}
380-
else
381-
{
382-
break;
383-
}
353+
_cleanupAction(item);
354+
355+
_ = Interlocked.Decrement(ref _currentSize);
384356
}
385-
catch (Exception e)
357+
else
386358
{
387-
Console.WriteLine(e);
359+
break;
388360
}
389361
}
390-
}
391-
finally
392-
{
393-
_isShrinking = false;
394-
395-
// After shrinking is complete, reset the TaskCompletionSource for the next time
396-
_shrinkCompletionSource = new TaskCompletionSource<bool>();
362+
catch (Exception e)
363+
{
364+
Console.WriteLine(e);
365+
}
397366
}
398367
}
399368
}

0 commit comments

Comments
 (0)