@@ -30,12 +30,7 @@ namespace System.Net.Http
3030 internal sealed class HttpConnectionPoolManager : IDisposable
3131 {
3232 /// <summary>How frequently an operation should be initiated to clean out old pools and connections in those pools.</summary>
33- private const int CleanPoolTimeoutMilliseconds =
34- #if DEBUG
35- 1_000 ;
36- #else
37- 30_000 ;
38- #endif
33+ private readonly TimeSpan _cleanPoolTimeout ;
3934 /// <summary>The pools, indexed by endpoint.</summary>
4035 private readonly ConcurrentDictionary < HttpConnectionKey , HttpConnectionPool > _pools ;
4136 /// <summary>Timer used to initiate cleaning of the pools.</summary>
@@ -63,7 +58,21 @@ public HttpConnectionPoolManager(HttpConnectionSettings settings)
6358 _settings = settings ;
6459 _maxConnectionsPerServer = settings . _maxConnectionsPerServer ;
6560 _pools = new ConcurrentDictionary < HttpConnectionKey , HttpConnectionPool > ( ) ;
61+
6662 // Start out with the timer not running, since we have no pools.
63+ // When it does run, run it with a frequency based on the idle timeout.
64+ if ( settings . _pooledConnectionIdleTimeout == Timeout . InfiniteTimeSpan )
65+ {
66+ const int DefaultScavengeSeconds = 30 ;
67+ _cleanPoolTimeout = TimeSpan . FromSeconds ( DefaultScavengeSeconds ) ;
68+ }
69+ else
70+ {
71+ const int ScavengesPerIdle = 4 ;
72+ const int MinScavengeSeconds = 1 ;
73+ TimeSpan timerPeriod = settings . _pooledConnectionIdleTimeout / ScavengesPerIdle ;
74+ _cleanPoolTimeout = timerPeriod . TotalSeconds >= MinScavengeSeconds ? timerPeriod : TimeSpan . FromSeconds ( MinScavengeSeconds ) ;
75+ }
6776
6877 // Figure out proxy stuff.
6978 if ( settings . _useProxy )
@@ -194,7 +203,7 @@ public Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request, Uri p
194203 {
195204 if ( ! _timerIsRunning )
196205 {
197- _cleaningTimer . Change ( CleanPoolTimeoutMilliseconds , CleanPoolTimeoutMilliseconds ) ;
206+ _cleaningTimer . Change ( _cleanPoolTimeout , _cleanPoolTimeout ) ;
198207 _timerIsRunning = true ;
199208 }
200209 }
0 commit comments