@@ -198,41 +198,6 @@ impl<T: Poolable> Pool<T> {
198
198
. unwrap_or ( 0 )
199
199
}
200
200
201
- fn take ( & self , key : & Key ) -> Option < Pooled < T > > {
202
- let entry = {
203
- let mut inner = self . inner . as_ref ( ) ?. lock ( ) . unwrap ( ) ;
204
- let expiration = Expiration :: new ( inner. timeout ) ;
205
- let maybe_entry = inner. idle . get_mut ( key)
206
- . and_then ( |list| {
207
- trace ! ( "take? {:?}: expiration = {:?}" , key, expiration. 0 ) ;
208
- // A block to end the mutable borrow on list,
209
- // so the map below can check is_empty()
210
- {
211
- let popper = IdlePopper {
212
- key,
213
- list,
214
- } ;
215
- popper. pop ( & expiration)
216
- }
217
- . map ( |e| ( e, list. is_empty ( ) ) )
218
- } ) ;
219
-
220
- let ( entry, empty) = if let Some ( ( e, empty) ) = maybe_entry {
221
- ( Some ( e) , empty)
222
- } else {
223
- // No entry found means nuke the list for sure.
224
- ( None , true )
225
- } ;
226
- if empty {
227
- //TODO: This could be done with the HashMap::entry API instead.
228
- inner. idle . remove ( key) ;
229
- }
230
- entry
231
- } ;
232
-
233
- entry. map ( |e| self . reuse ( key, e. value ) )
234
- }
235
-
236
201
pub ( super ) fn pooled ( & self , mut connecting : Connecting < T > , value : T ) -> Pooled < T > {
237
202
let ( value, pool_ref) = if let Some ( ref enabled) = self . inner {
238
203
match value. reserve ( ) {
@@ -296,23 +261,6 @@ impl<T: Poolable> Pool<T> {
296
261
value : Some ( value) ,
297
262
}
298
263
}
299
-
300
- fn waiter ( & self , key : Key , tx : oneshot:: Sender < T > ) {
301
- debug_assert ! (
302
- self . is_enabled( ) ,
303
- "pool.waiter() should not be called if disabled" ,
304
- ) ;
305
- trace ! ( "checkout waiting for idle connection: {:?}" , key) ;
306
- self . inner
307
- . as_ref ( )
308
- . expect ( "pool.waiter() expects pool is enabled" )
309
- . lock ( )
310
- . unwrap ( )
311
- . waiters
312
- . entry ( key)
313
- . or_insert ( VecDeque :: new ( ) )
314
- . push_back ( tx) ;
315
- }
316
264
}
317
265
318
266
/// Pop off this list, looking for a usable connection that hasn't expired.
@@ -643,15 +591,54 @@ impl<T: Poolable> Checkout<T> {
643
591
}
644
592
}
645
593
646
- fn add_waiter ( & mut self ) {
647
- debug_assert ! ( self . pool. is_enabled( ) ) ;
594
+ fn checkout ( & mut self ) -> Option < Pooled < T > > {
595
+ let entry = {
596
+ let mut inner = self . pool . inner . as_ref ( ) ?. lock ( ) . unwrap ( ) ;
597
+ let expiration = Expiration :: new ( inner. timeout ) ;
598
+ let maybe_entry = inner. idle . get_mut ( & self . key )
599
+ . and_then ( |list| {
600
+ trace ! ( "take? {:?}: expiration = {:?}" , self . key, expiration. 0 ) ;
601
+ // A block to end the mutable borrow on list,
602
+ // so the map below can check is_empty()
603
+ {
604
+ let popper = IdlePopper {
605
+ key : & self . key ,
606
+ list,
607
+ } ;
608
+ popper. pop ( & expiration)
609
+ }
610
+ . map ( |e| ( e, list. is_empty ( ) ) )
611
+ } ) ;
648
612
649
- if self . waiter . is_none ( ) {
650
- let ( tx, mut rx) = oneshot:: channel ( ) ;
651
- let _ = rx. poll ( ) ; // park this task
652
- self . pool . waiter ( self . key . clone ( ) , tx) ;
653
- self . waiter = Some ( rx) ;
654
- }
613
+ let ( entry, empty) = if let Some ( ( e, empty) ) = maybe_entry {
614
+ ( Some ( e) , empty)
615
+ } else {
616
+ // No entry found means nuke the list for sure.
617
+ ( None , true )
618
+ } ;
619
+ if empty {
620
+ //TODO: This could be done with the HashMap::entry API instead.
621
+ inner. idle . remove ( & self . key ) ;
622
+ }
623
+
624
+ if entry. is_none ( ) && self . waiter . is_none ( ) {
625
+ let ( tx, mut rx) = oneshot:: channel ( ) ;
626
+ let _ = rx. poll ( ) ; // park this task
627
+
628
+ trace ! ( "checkout waiting for idle connection: {:?}" , self . key) ;
629
+ inner
630
+ . waiters
631
+ . entry ( self . key . clone ( ) )
632
+ . or_insert ( VecDeque :: new ( ) )
633
+ . push_back ( tx) ;
634
+
635
+ self . waiter = Some ( rx) ;
636
+ }
637
+
638
+ entry
639
+ } ;
640
+
641
+ entry. map ( |e| self . pool . reuse ( & self . key , e. value ) )
655
642
}
656
643
}
657
644
@@ -664,14 +651,11 @@ impl<T: Poolable> Future for Checkout<T> {
664
651
return Ok ( Async :: Ready ( pooled) ) ;
665
652
}
666
653
667
- let entry = self . pool . take ( & self . key ) ;
668
-
669
- if let Some ( pooled) = entry {
654
+ if let Some ( pooled) = self . checkout ( ) {
670
655
Ok ( Async :: Ready ( pooled) )
671
656
} else if !self . pool . is_enabled ( ) {
672
657
Err ( :: Error :: new_canceled ( Some ( "pool is disabled" ) ) )
673
658
} else {
674
- self . add_waiter ( ) ;
675
659
Ok ( Async :: NotReady )
676
660
}
677
661
}
0 commit comments