11use std:: collections:: VecDeque ;
22use std:: { cell:: RefCell , fmt, future:: Future , pin:: Pin , rc:: Rc , task:: Context , task:: Poll } ;
33
4- use ntex:: io:: { IoBoxed , IoRef , OnDisconnect } ;
4+ use ntex:: io:: { IoBoxed , IoRef , OnDisconnect , RecvError } ;
55use ntex:: util:: { poll_fn, ready, Either , Ready } ;
66use ntex:: { channel:: pool, service:: Service } ;
77
@@ -30,31 +30,37 @@ impl Client {
3030 ntex:: rt:: spawn ( async move {
3131 poll_fn ( |cx| loop {
3232 match ready ! ( io. poll_recv( & Codec , cx) ) {
33- Ok ( Some ( item) ) => {
33+ Ok ( item) => {
3434 if let Some ( tx) = queue2. borrow_mut ( ) . pop_front ( ) {
3535 let _ = tx. send ( Ok ( item) ) ;
3636 } else {
3737 log:: error!( "Unexpected redis response: {:?}" , item) ;
3838 }
3939 continue ;
4040 }
41- Err ( Either :: Left ( e) ) => {
41+ Err ( RecvError :: KeepAlive ) | Err ( RecvError :: Stop ) => {
42+ unreachable ! ( )
43+ }
44+ Err ( RecvError :: WriteBackpressure ) => {
45+ if ready ! ( io. poll_flush( cx, false ) ) . is_err ( ) {
46+ return Poll :: Ready ( ( ) ) ;
47+ } else {
48+ continue ;
49+ }
50+ }
51+ Err ( RecvError :: Decoder ( e) ) => {
4252 if let Some ( tx) = queue2. borrow_mut ( ) . pop_front ( ) {
4353 let _ = tx. send ( Err ( e) ) ;
4454 }
4555 queue2. borrow_mut ( ) . clear ( ) ;
4656 let _ = ready ! ( io. poll_shutdown( cx) ) ;
4757 return Poll :: Ready ( ( ) ) ;
4858 }
49- Err ( Either :: Right ( e) ) => {
50- if let Some ( tx) = queue2. borrow_mut ( ) . pop_front ( ) {
51- let _ = tx. send ( Err ( e. into ( ) ) ) ;
52- }
59+ Err ( RecvError :: PeerGone ( e) ) => {
60+ log:: info!( "Redis connection is dropped: {:?}" , e) ;
5361 queue2. borrow_mut ( ) . clear ( ) ;
54- let _ = ready ! ( io. poll_shutdown( cx) ) ;
5562 return Poll :: Ready ( ( ) ) ;
5663 }
57- Ok ( None ) => return Poll :: Ready ( ( ) ) ,
5864 }
5965 } )
6066 . await
@@ -80,7 +86,7 @@ impl Client {
8086
8187 async move {
8288 if !is_open {
83- Err ( CommandError :: Protocol ( Error :: Disconnected ) )
89+ Err ( CommandError :: Protocol ( Error :: PeerGone ( None ) ) )
8490 } else {
8591 fut. await
8692 . map_err ( CommandError :: Protocol )
@@ -108,7 +114,7 @@ impl Service<Request> for Client {
108114
109115 fn poll_ready ( & self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
110116 if self . disconnect . poll_ready ( cx) . is_ready ( ) {
111- Poll :: Ready ( Err ( Error :: Disconnected ) )
117+ Poll :: Ready ( Err ( Error :: PeerGone ( None ) ) )
112118 } else {
113119 Poll :: Ready ( Ok ( ( ) ) )
114120 }
@@ -143,7 +149,7 @@ impl Future for CommandResult {
143149 fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
144150 match Pin :: new ( & mut self . rx ) . poll ( cx) {
145151 Poll :: Ready ( Ok ( res) ) => Poll :: Ready ( res) ,
146- Poll :: Ready ( Err ( _) ) => Poll :: Ready ( Err ( Error :: Disconnected ) ) ,
152+ Poll :: Ready ( Err ( _) ) => Poll :: Ready ( Err ( Error :: PeerGone ( None ) ) ) ,
147153 Poll :: Pending => Poll :: Pending ,
148154 }
149155 }
0 commit comments