From 92f993e05438c9c989fd32f91d12011bf21add52 Mon Sep 17 00:00:00 2001
From: Nikolay Kim <fafhrd91@gmail.com>
Date: Thu, 10 May 2018 09:37:38 -0700
Subject: [PATCH] Fix client request timeout handling

---
 CHANGES.md             |  2 ++
 src/client/mod.rs      |  1 +
 src/client/pipeline.rs | 12 ++++++------
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index db938c937..fd7f0e2db 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -6,6 +6,8 @@
 
 * Added error response functions for 501,502,503,504
 
+* Fix client request timeout handling
+
 
 ## 0.6.2 (2018-05-09)
 
diff --git a/src/client/mod.rs b/src/client/mod.rs
index 436fcf206..2116ae360 100644
--- a/src/client/mod.rs
+++ b/src/client/mod.rs
@@ -49,6 +49,7 @@ use httpresponse::HttpResponse;
 impl ResponseError for SendRequestError {
     fn error_response(&self) -> HttpResponse {
         match *self {
+            SendRequestError::Timeout => HttpResponse::GatewayTimeout(),
             SendRequestError::Connector(_) => HttpResponse::BadGateway(),
             _ => HttpResponse::InternalServerError(),
         }.into()
diff --git a/src/client/pipeline.rs b/src/client/pipeline.rs
index 60eb4f8c2..6a36bdd23 100644
--- a/src/client/pipeline.rs
+++ b/src/client/pipeline.rs
@@ -194,6 +194,7 @@ impl Future for SendRequest {
                     self.state = State::Send(pl);
                 }
                 State::Send(mut pl) => {
+                    pl.poll_timeout()?;
                     pl.poll_write().map_err(|e| {
                         io::Error::new(io::ErrorKind::Other, format!("{}", e).as_str())
                     })?;
@@ -315,7 +316,7 @@ impl Pipeline {
         {
             Async::NotReady => need_run = true,
             Async::Ready(_) => {
-                let _ = self.poll_timeout().map_err(|e| {
+                self.poll_timeout().map_err(|e| {
                     io::Error::new(io::ErrorKind::Other, format!("{}", e))
                 })?;
             }
@@ -371,16 +372,15 @@ impl Pipeline {
         }
     }
 
-    fn poll_timeout(&mut self) -> Poll<(), SendRequestError> {
+    fn poll_timeout(&mut self) -> Result<(), SendRequestError> {
         if self.timeout.is_some() {
             match self.timeout.as_mut().unwrap().poll() {
-                Ok(Async::Ready(())) => Err(SendRequestError::Timeout),
-                Ok(Async::NotReady) => Ok(Async::NotReady),
+                Ok(Async::Ready(())) => return Err(SendRequestError::Timeout),
+                Ok(Async::NotReady) => (),
                 Err(_) => unreachable!(),
             }
-        } else {
-            Ok(Async::NotReady)
         }
+        Ok(())
     }
 
     #[inline]