File tree Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -102,22 +102,31 @@ impl<C: NetworkConnector<Stream=S>, S: NetworkStream + Send> NetworkConnector fo
102102 type Stream = PooledStream < S > ;
103103 fn connect ( & self , host : & str , port : u16 , scheme : & str ) -> :: Result < PooledStream < S > > {
104104 let key = key ( host, port, scheme) ;
105- let mut should_remove = false ;
106- let inner = match self . inner . lock ( ) . unwrap ( ) . conns . get_mut ( & key) {
107- Some ( ref mut vec) => {
105+
106+ let inner = {
107+ // keep the mutex locked only in this block
108+ let mut locked = self . inner . lock ( ) . unwrap ( ) ;
109+ let mut should_remove = false ;
110+ let inner = locked. conns . get_mut ( & key) . map ( |vec| {
108111 trace ! ( "Pool had connection, using" ) ;
109112 should_remove = vec. len ( ) == 1 ;
110113 vec. pop ( ) . unwrap ( )
114+ } ) ;
115+ if should_remove {
116+ locked. conns . remove ( & key) ;
111117 }
118+ inner
119+ } ;
120+
121+ let inner = match inner {
122+ Some ( inner) => inner,
112123 None => PooledStreamInner {
113124 key : key. clone ( ) ,
114125 stream : try!( self . connector . connect ( host, port, scheme) ) ,
115126 previous_response_expected_no_content : false ,
116127 }
128+
117129 } ;
118- if should_remove {
119- self . inner . lock ( ) . unwrap ( ) . conns . remove ( & key) ;
120- }
121130 Ok ( PooledStream {
122131 inner : Some ( inner) ,
123132 is_closed : false ,
You can’t perform that action at this time.
0 commit comments