@@ -178,8 +178,6 @@ public async Task ReapAsync(IOBehavior ioBehavior, CancellationToken cancellatio
178
178
{
179
179
Log . Debug ( "{0} reaping connection pool" , m_logArguments ) ;
180
180
RecoverLeakedSessions ( ) ;
181
- if ( ConnectionSettings . ConnectionIdleTimeout == 0 )
182
- return ;
183
181
await CleanPoolAsync ( ioBehavior , session => ( DateTime . UtcNow - session . LastReturnedUtc ) . TotalSeconds >= ConnectionSettings . ConnectionIdleTimeout , true , cancellationToken ) . ConfigureAwait ( false ) ;
184
182
}
185
183
@@ -398,12 +396,6 @@ public static async Task ClearPoolsAsync(IOBehavior ioBehavior, CancellationToke
398
396
await pool . ClearAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
399
397
}
400
398
401
- public static async Task ReapPoolsAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
402
- {
403
- foreach ( var pool in GetAllPools ( ) )
404
- await pool . ReapAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
405
- }
406
-
407
399
private static IReadOnlyList < ConnectionPool > GetAllPools ( )
408
400
{
409
401
var pools = new List < ConnectionPool > ( s_pools . Count ) ;
@@ -430,6 +422,7 @@ private ConnectionPool(ConnectionSettings cs)
430
422
foreach ( var hostName in cs . HostNames )
431
423
m_hostSessions [ hostName ] = 0 ;
432
424
}
425
+
433
426
m_loadBalancer = cs . ConnectionType != ConnectionType . Tcp ? null :
434
427
cs . HostNames . Count == 1 || cs . LoadBalance == MySqlLoadBalance . FailOver ? FailOverLoadBalancer . Instance :
435
428
cs . LoadBalance == MySqlLoadBalance . Random ? RandomLoadBalancer . Instance :
@@ -440,6 +433,28 @@ private ConnectionPool(ConnectionSettings cs)
440
433
m_logArguments = new object [ ] { "Pool{0}" . FormatInvariant ( Id ) } ;
441
434
if ( Log . IsInfoEnabled ( ) )
442
435
Log . Info ( "{0} creating new connection pool for {1}" , m_logArguments [ 0 ] , cs . ConnectionStringBuilder . GetConnectionString ( includePassword : false ) ) ;
436
+
437
+ if ( cs . ConnectionIdleTimeout > 0 )
438
+ {
439
+ var reaperInterval = TimeSpan . FromSeconds ( Math . Max ( 1 , Math . Min ( 60 , cs . ConnectionIdleTimeout / 2 ) ) ) ;
440
+ m_reaperTask = Task . Run ( async ( ) =>
441
+ {
442
+ while ( true )
443
+ {
444
+ var task = Task . Delay ( reaperInterval ) ;
445
+ try
446
+ {
447
+ using ( var source = new CancellationTokenSource ( reaperInterval ) )
448
+ await ReapAsync ( IOBehavior . Asynchronous , source . Token ) . ConfigureAwait ( false ) ;
449
+ }
450
+ catch
451
+ {
452
+ // do nothing; we'll try to reap again
453
+ }
454
+ await task . ConfigureAwait ( false ) ;
455
+ }
456
+ } ) ;
457
+ }
443
458
}
444
459
445
460
private void AdjustHostConnectionCount ( ServerSession session , int delta )
@@ -478,26 +493,6 @@ public ConnectionStringPool(string connectionString, ConnectionPool pool)
478
493
479
494
static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager . CreateLogger ( nameof ( ConnectionPool ) ) ;
480
495
static readonly ConcurrentDictionary < string , ConnectionPool > s_pools = new ConcurrentDictionary < string , ConnectionPool > ( ) ;
481
- #if DEBUG
482
- static readonly TimeSpan ReaperInterval = TimeSpan . FromSeconds ( 1 ) ;
483
- #else
484
- static readonly TimeSpan ReaperInterval = TimeSpan . FromMinutes ( 1 ) ;
485
- #endif
486
- static readonly Task Reaper = Task . Run ( async ( ) => {
487
- while ( true )
488
- {
489
- var task = Task . Delay ( ReaperInterval ) ;
490
- try
491
- {
492
- await ReapPoolsAsync ( IOBehavior . Asynchronous , new CancellationTokenSource ( ReaperInterval ) . Token ) . ConfigureAwait ( false ) ;
493
- }
494
- catch
495
- {
496
- // do nothing; we'll try to reap again
497
- }
498
- await task . ConfigureAwait ( false ) ;
499
- }
500
- } ) ;
501
496
502
497
static int s_poolId ;
503
498
static ConnectionStringPool s_mruCache ;
@@ -510,6 +505,7 @@ public ConnectionStringPool(string connectionString, ConnectionPool pool)
510
505
readonly ILoadBalancer m_loadBalancer ;
511
506
readonly Dictionary < string , int > m_hostSessions ;
512
507
readonly object [ ] m_logArguments ;
508
+ readonly Task m_reaperTask ;
513
509
uint m_lastRecoveryTime ;
514
510
int m_lastSessionId ;
515
511
Dictionary < string , CachedProcedure > m_procedureCache ;
0 commit comments