Stop deadline timer once start to read response.

master
Chunting Gu 6 years ago
parent 99e02927b7
commit 5853f39ac3

@ -21,29 +21,13 @@
class BookClientBase { class BookClientBase {
public: public:
BookClientBase(const std::string& url, int timeout_seconds) : url_(url) { BookClientBase(webcc::HttpClientSession& session, const std::string& url)
//session_.SetTimeout(timeout_seconds); : session_(session), url_(url) {
//session_.set_content_type("application/json");
session_.set_charset("utf-8");
} }
virtual ~BookClientBase() = default; virtual ~BookClientBase() = default;
protected: protected:
// Helper function to make a request.
//webcc::HttpRequestPtr MakeRequest(const std::string& method,
// const std::string& url,
// std::string&& content = "") {
// auto request = webcc::HttpRequest::New(method, url, host_, port_);
// request->AcceptAppJson();
// if (!content.empty()) {
// request->SetContentInAppJsonUtf8(JsonToString(content), true);
// }
// request->Prepare();
// return request;
//}
// Check HTTP response status. // Check HTTP response status.
bool CheckStatus(webcc::HttpResponsePtr response, int expected_status) { bool CheckStatus(webcc::HttpResponsePtr response, int expected_status) {
int status = response->status(); int status = response->status();
@ -58,15 +42,15 @@ protected:
protected: protected:
std::string url_; std::string url_;
webcc::HttpClientSession session_; webcc::HttpClientSession& session_;
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
class BookListClient : public BookClientBase { class BookListClient : public BookClientBase {
public: public:
BookListClient(const std::string& url, int timeout_seconds) BookListClient(webcc::HttpClientSession& session, const std::string& url)
: BookClientBase(url, timeout_seconds) { : BookClientBase(session, url) {
} }
bool ListBooks(std::list<Book>* books) { bool ListBooks(std::list<Book>* books) {
@ -124,13 +108,13 @@ public:
class BookDetailClient : public BookClientBase { class BookDetailClient : public BookClientBase {
public: public:
BookDetailClient(const std::string& url, int timeout_seconds) BookDetailClient(webcc::HttpClientSession& session, const std::string& url)
: BookClientBase(url, timeout_seconds) { : BookClientBase(session, url) {
} }
bool GetBook(const std::string& id, Book* book) { bool GetBook(const std::string& id, Book* book) {
try { try {
auto r = session_.Get(url_ + "/books" + id); auto r = session_.Get(url_ + "/books/" + id);
if (!CheckStatus(r, webcc::http::Status::kOK)) { if (!CheckStatus(r, webcc::http::Status::kOK)) {
return false; return false;
@ -151,7 +135,7 @@ public:
json["price"] = price; json["price"] = price;
try { try {
auto r = session_.Put(url_ + "/books" + id, JsonToString(json), true); auto r = session_.Put(url_ + "/books/" + id, JsonToString(json), true);
if (!CheckStatus(r, webcc::http::Status::kOK)) { if (!CheckStatus(r, webcc::http::Status::kOK)) {
return false; return false;
@ -218,13 +202,22 @@ int main(int argc, char* argv[]) {
std::string url = argv[1]; std::string url = argv[1];
int timeout_seconds = -1; int timeout = 0;
if (argc > 2) { if (argc > 2) {
timeout_seconds = std::atoi(argv[2]); timeout = std::atoi(argv[2]);
} }
BookListClient list_client(url, timeout_seconds); // Share the same session.
BookDetailClient detail_client(url, timeout_seconds); webcc::HttpClientSession session;
// Session-level settings.
session.set_timeout(timeout);
// TODO
//session.set_content_type("application/json");
//session.set_charset("utf-8");
BookListClient list_client(session, url);
BookDetailClient detail_client(session, url);
PrintSeparator(); PrintSeparator();
@ -233,50 +226,50 @@ int main(int argc, char* argv[]) {
PrintBookList(books); PrintBookList(books);
} }
//PrintSeparator(); PrintSeparator();
//std::string id; std::string id;
//if (list_client.CreateBook("1984", 12.3, &id)) { if (list_client.CreateBook("1984", 12.3, &id)) {
// std::cout << "Book ID: " << id << std::endl; std::cout << "Book ID: " << id << std::endl;
//} else { } else {
// id = "1"; id = "1";
// std::cout << "Book ID: " << id << " (faked)"<< std::endl; std::cout << "Book ID: " << id << " (faked)"<< std::endl;
//} }
//PrintSeparator(); PrintSeparator();
//books.clear(); books.clear();
//if (list_client.ListBooks(&books)) { if (list_client.ListBooks(&books)) {
// PrintBookList(books); PrintBookList(books);
//} }
//PrintSeparator(); PrintSeparator();
//Book book; Book book;
//if (detail_client.GetBook(id, &book)) { if (detail_client.GetBook(id, &book)) {
// PrintBook(book); PrintBook(book);
//} }
//PrintSeparator(); PrintSeparator();
//detail_client.UpdateBook(id, "1Q84", 32.1); detail_client.UpdateBook(id, "1Q84", 32.1);
//PrintSeparator(); PrintSeparator();
//if (detail_client.GetBook(id, &book)) { if (detail_client.GetBook(id, &book)) {
// PrintBook(book); PrintBook(book);
//} }
//PrintSeparator(); PrintSeparator();
//detail_client.DeleteBook(id); detail_client.DeleteBook(id);
//PrintSeparator(); PrintSeparator();
//books.clear(); books.clear();
//if (list_client.ListBooks(&books)) { if (list_client.ListBooks(&books)) {
// PrintBookList(books); PrintBookList(books);
//} }
return 0; return 0;
} }

@ -9,12 +9,13 @@ using boost::asio::ip::tcp;
namespace webcc { namespace webcc {
HttpClient::HttpClient(std::size_t buffer_size, bool ssl_verify) HttpClient::HttpClient(bool ssl_verify, std::size_t buffer_size)
: buffer_size_(buffer_size == 0 ? kBufferSize : buffer_size), : timer_(io_context_),
timer_(io_context_),
ssl_verify_(ssl_verify), ssl_verify_(ssl_verify),
buffer_size_(buffer_size == 0 ? kBufferSize : buffer_size),
timeout_(kMaxReadSeconds), timeout_(kMaxReadSeconds),
closed_(false), closed_(false),
timer_canceled_(false),
timed_out_(false), timed_out_(false),
error_(kNoError) { error_(kNoError) {
} }
@ -26,6 +27,7 @@ bool HttpClient::Request(const HttpRequest& request, bool connect) {
response_parser_.reset(new HttpResponseParser(response_.get())); response_parser_.reset(new HttpResponseParser(response_.get()));
closed_ = false; closed_ = false;
timer_canceled_ = false;
timed_out_ = false; timed_out_ = false;
error_ = kNoError; error_ = kNoError;
@ -160,8 +162,10 @@ void HttpClient::DoReadResponse(Error* error) {
LOG_VERB("Socket async read handler."); LOG_VERB("Socket async read handler.");
// Stop the deadline timer once the read has started.
CancelTimer();
if (ec || length == 0) { if (ec || length == 0) {
StopTimer();
Close(); Close();
*error = kSocketReadError; *error = kSocketReadError;
LOG_ERRO("Socket read error (%s).", ec.message().c_str()); LOG_ERRO("Socket read error (%s).", ec.message().c_str());
@ -172,7 +176,7 @@ void HttpClient::DoReadResponse(Error* error) {
// Parse the response piece just read. // Parse the response piece just read.
if (!response_parser_->Parse(buffer_.data(), length)) { if (!response_parser_->Parse(buffer_.data(), length)) {
StopTimer(); //CancelTimer();
Close(); Close();
*error = kHttpError; *error = kHttpError;
LOG_ERRO("Failed to parse HTTP response."); LOG_ERRO("Failed to parse HTTP response.");
@ -183,7 +187,7 @@ void HttpClient::DoReadResponse(Error* error) {
// Stop trying to read once all content has been received, because // Stop trying to read once all content has been received, because
// some servers will block extra call to read_some(). // some servers will block extra call to read_some().
StopTimer(); //CancelTimer();
if (response_->IsConnectionKeepAlive()) { if (response_->IsConnectionKeepAlive()) {
// Close the timer but keep the socket connection. // Close the timer but keep the socket connection.
@ -243,10 +247,15 @@ void HttpClient::OnTimer(boost::system::error_code ec) {
DoWaitTimer(); DoWaitTimer();
} }
void HttpClient::StopTimer() { void HttpClient::CancelTimer() {
// Cancel any asynchronous operations that are waiting on the timer. if (timer_canceled_) {
return;
}
LOG_INFO("Cancel deadline timer..."); LOG_INFO("Cancel deadline timer...");
timer_.cancel(); timer_.cancel();
timer_canceled_ = true;
} }
} // namespace webcc } // namespace webcc

@ -27,7 +27,7 @@ typedef std::shared_ptr<HttpClient> HttpClientPtr;
// Please don't use the same client object in multiple threads. // Please don't use the same client object in multiple threads.
class HttpClient { class HttpClient {
public: public:
explicit HttpClient(std::size_t buffer_size = 0, bool ssl_verify = true); explicit HttpClient(bool ssl_verify = true, std::size_t buffer_size = 0);
virtual ~HttpClient() = default; virtual ~HttpClient() = default;
@ -87,7 +87,8 @@ private:
void DoWaitTimer(); void DoWaitTimer();
void OnTimer(boost::system::error_code ec); void OnTimer(boost::system::error_code ec);
void StopTimer(); // Cancel any async-operations waiting on the timer.
void CancelTimer();
private: private:
boost::asio::io_context io_context_; boost::asio::io_context io_context_;
@ -104,14 +105,13 @@ private:
// The buffer for reading response. // The buffer for reading response.
std::vector<char> buffer_; std::vector<char> buffer_;
// Verify the certificate of the peer or not (for HTTPS).
bool ssl_verify_;
// The size of the buffer for reading response. // The size of the buffer for reading response.
// Set 0 for using default value (e.g., 1024). // Set 0 for using default value (e.g., 1024).
std::size_t buffer_size_; std::size_t buffer_size_;
// Verify the certificate of the peer (remote server) or not.
// HTTPS only.
bool ssl_verify_;
// Maximum seconds to wait before the client cancels the operation. // Maximum seconds to wait before the client cancels the operation.
// Only for reading response from server. // Only for reading response from server.
int timeout_; int timeout_;
@ -119,9 +119,13 @@ private:
// Connection closed. // Connection closed.
bool closed_; bool closed_;
// If the error was caused by timeout or not. // Deadline timer canceled.
bool timer_canceled_;
// Timeout occurred.
bool timed_out_; bool timed_out_;
// Error code.
Error error_; Error error_;
}; };

@ -42,7 +42,7 @@ HttpResponsePtr HttpClientSession::Request(HttpRequestArgs&& args) {
assert(args.parameters_.size() % 2 == 0); assert(args.parameters_.size() % 2 == 0);
assert(args.headers_.size() % 2 == 0); assert(args.headers_.size() % 2 == 0);
HttpRequest request{ args.method_, args.url_ }; HttpRequest request{args.method_, args.url_};
for (std::size_t i = 1; i < args.parameters_.size(); i += 2) { for (std::size_t i = 1; i < args.parameters_.size(); i += 2) {
request.AddParameter(args.parameters_[i - 1], args.parameters_[i]); request.AddParameter(args.parameters_[i - 1], args.parameters_[i]);
@ -83,7 +83,7 @@ HttpResponsePtr HttpClientSession::Request(HttpRequestArgs&& args) {
HttpClientPtr client = pool_.Get(key); HttpClientPtr client = pool_.Get(key);
if (!client) { if (!client) {
client.reset(new HttpClient{buffer_size, ssl_verify}); client.reset(new HttpClient{ssl_verify, buffer_size});
reuse = false; reuse = false;
} else { } else {
client->set_buffer_size(buffer_size); client->set_buffer_size(buffer_size);

@ -38,11 +38,6 @@ public:
return std::move(*this); return std::move(*this);
} }
HttpRequestArgs&& url(std::string&& url) {
url_ = std::move(url);
return std::move(*this);
}
HttpRequestArgs&& parameters(const std::vector<std::string>& parameters) { HttpRequestArgs&& parameters(const std::vector<std::string>& parameters) {
parameters_ = parameters; parameters_ = parameters;
return std::move(*this); return std::move(*this);

Loading…
Cancel
Save