@@ -41,6 +41,12 @@ struct PoolInner<T> {
41
41
// connection.
42
42
parked : HashMap < Rc < String > , VecDeque < relay:: Sender < Entry < T > > > > ,
43
43
timeout : Option < Duration > ,
44
+ // Used to prevent multiple intervals from being spawned to clear
45
+ // expired connections.
46
+ //
47
+ // TODO(0.12): Remove the need for this when Client::schedule_pool_timer
48
+ // can be done in Client::new.
49
+ expired_timer_spawned : bool ,
44
50
}
45
51
46
52
impl < T : Clone + Ready > Pool < T > {
@@ -51,6 +57,7 @@ impl<T: Clone + Ready> Pool<T> {
51
57
idle : HashMap :: new ( ) ,
52
58
parked : HashMap :: new ( ) ,
53
59
timeout : timeout,
60
+ expired_timer_spawned : false ,
54
61
} ) ) ,
55
62
}
56
63
}
@@ -229,12 +236,17 @@ impl<T> Pool<T> {
229
236
230
237
impl < T : ' static > Pool < T > {
231
238
pub ( super ) fn spawn_expired_interval ( & self , handle : & Handle ) {
232
- let inner = self . inner . borrow ( ) ;
239
+ let mut inner = self . inner . borrow_mut ( ) ;
233
240
234
241
if !inner. enabled {
235
242
return ;
236
243
}
237
244
245
+ if inner. expired_timer_spawned {
246
+ return ;
247
+ }
248
+ inner. expired_timer_spawned = true ;
249
+
238
250
let dur = if let Some ( dur) = inner. timeout {
239
251
dur
240
252
} else {
@@ -529,7 +541,9 @@ mod tests {
529
541
530
542
#[ test]
531
543
fn test_pool_timer_removes_expired ( ) {
532
- let pool = Pool :: new ( true , Some ( Duration :: from_secs ( 1 ) ) ) ;
544
+ let mut core = :: tokio:: reactor:: Core :: new ( ) . unwrap ( ) ;
545
+ let pool = Pool :: new ( true , Some ( Duration :: from_millis ( 100 ) ) ) ;
546
+ pool. spawn_expired_interval ( & core. handle ( ) ) ;
533
547
let key = Rc :: new ( "foo" . to_string ( ) ) ;
534
548
535
549
let mut pooled1 = pool. pooled ( key. clone ( ) , 41 ) ;
@@ -540,9 +554,13 @@ mod tests {
540
554
pooled3. idle ( ) ;
541
555
542
556
assert_eq ! ( pool. inner. borrow( ) . idle. get( & key) . map( |entries| entries. len( ) ) , Some ( 3 ) ) ;
543
- :: std:: thread:: sleep ( pool. inner . borrow ( ) . timeout . unwrap ( ) ) ;
544
557
545
- pool. clear_expired ( ) ;
558
+ let timeout = :: tokio:: reactor:: Timeout :: new (
559
+ Duration :: from_millis ( 400 ) , // allow for too-good resolution
560
+ & core. handle ( )
561
+ ) . unwrap ( ) ;
562
+ core. run ( timeout) . unwrap ( ) ;
563
+
546
564
assert ! ( pool. inner. borrow( ) . idle. get( & key) . is_none( ) ) ;
547
565
}
548
566
0 commit comments