From 3566b1d98bdb0bcf29ea5bac25bba2feceba414c Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Tue, 19 Mar 2019 12:37:15 +0800 Subject: [PATCH] Add timeout setting HttpClientSession. --- webcc/http_client_session.cc | 2 ++ webcc/http_client_session.h | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/webcc/http_client_session.cc b/webcc/http_client_session.cc index 485844a..a1bb8d0 100644 --- a/webcc/http_client_session.cc +++ b/webcc/http_client_session.cc @@ -92,6 +92,8 @@ HttpResponsePtr HttpClientSession::Request(HttpRequestArgs&& args) { LOG_VERB("Reuse an existing connection."); } + client->set_timeout(timeout_); + if (!client->Request(request, !reuse)) { throw Exception(client->error(), client->timed_out()); } diff --git a/webcc/http_client_session.h b/webcc/http_client_session.h index b190333..4653df3 100644 --- a/webcc/http_client_session.h +++ b/webcc/http_client_session.h @@ -27,10 +27,6 @@ public: charset_ = charset; } - void AddHeader(const std::string& key, const std::string& value) { - headers_.Add(key, value); - } - void set_ssl_verify(bool ssl_verify) { ssl_verify_.emplace(ssl_verify); } @@ -39,6 +35,16 @@ public: buffer_size_ = buffer_size; } + void set_timeout(int timeout) { + if (timeout > 0) { + timeout_ = timeout; + } + } + + void AddHeader(const std::string& key, const std::string& value) { + headers_.Add(key, value); + } + HttpResponsePtr Request(HttpRequestArgs&& args); HttpResponsePtr Get(const std::string& url, @@ -68,16 +74,19 @@ private: // E.g., "utf-8". std::string charset_; - // Headers for each request sent from this session. + // Additional headers for each request. HttpHeaderDict headers_; // Verify the certificate of the peer or not. boost::optional ssl_verify_; - // The bytes of the buffer for reading response. + // The size of the buffer for reading response. // 0 means default value will be used. std::size_t buffer_size_ = 0; + // Timeout in seconds for receiving response. + int timeout_ = 0; + // Connection pool for keep-alive. HttpClientPool pool_; };