Refine timeout control; refine rest book examples.

This commit is contained in:
Adam Gu
2018-08-23 17:28:02 +08:00
parent 2e2b45dd43
commit e9096d4e53
16 changed files with 313 additions and 201 deletions
+2 -5
View File
@@ -23,11 +23,8 @@ const std::size_t kBufferSize = 1024;
const std::size_t kInvalidLength = static_cast<std::size_t>(-1);
// Timeout seconds.
// TODO
const int kMaxConnectSeconds = 10;
const int kMaxSendSeconds = 30;
const int kMaxReceiveSeconds = 30;
// Default timeout for reading response.
const int kMaxReadSeconds = 30;
extern const std::string kHost;
extern const std::string kContentType;
+24 -36
View File
@@ -9,15 +9,12 @@
namespace webcc {
extern void AdjustBufferSize(std::size_t content_length,
std::vector<char>* buffer);
HttpAsyncClient::HttpAsyncClient(boost::asio::io_context& io_context)
: socket_(io_context),
resolver_(new tcp::resolver(io_context)),
buffer_(kBufferSize),
deadline_(io_context),
timeout_seconds_(kMaxReceiveSeconds),
timeout_seconds_(kMaxReadSeconds),
stopped_(false),
timed_out_(false) {
}
@@ -30,6 +27,8 @@ void HttpAsyncClient::Request(std::shared_ptr<HttpRequest> request,
response_.reset(new HttpResponse());
response_parser_.reset(new HttpResponseParser(response_.get()));
stopped_ = timed_out_ = false;
LOG_VERB("HTTP request:\n%s", request->Dump(4, "> ").c_str());
request_ = request;
@@ -54,6 +53,7 @@ void HttpAsyncClient::Stop() {
LOG_ERRO("Failed to close socket.");
}
LOG_INFO("Cancel deadline timer...");
deadline_.cancel();
}
}
@@ -68,28 +68,19 @@ void HttpAsyncClient::ResolveHandler(boost::system::error_code ec,
// Start the connect actor.
endpoints_ = endpoints;
// Set a deadline for the connect operation.
deadline_.expires_from_now(boost::posix_time::seconds(kMaxConnectSeconds));
// ConnectHandler: void(boost::system::error_code, tcp::endpoint)
boost::asio::async_connect(socket_, endpoints_,
std::bind(&HttpAsyncClient::ConnectHandler,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
// Start the deadline actor. You will note that we're not setting any
// particular deadline here. Instead, the connect and input actors will
// update the deadline prior to each asynchronous operation.
deadline_.async_wait(std::bind(&HttpAsyncClient::CheckDeadline,
shared_from_this()));
}
}
void HttpAsyncClient::ConnectHandler(boost::system::error_code ec,
tcp::endpoint endpoint) {
if (ec) {
LOG_ERRO("Socket connect error: %s", ec.message().c_str());
LOG_ERRO("Socket connect error (%s).", ec.message().c_str());
Stop();
response_handler_(response_, kEndpointConnectError, timed_out_);
return;
@@ -115,8 +106,6 @@ void HttpAsyncClient::AsyncWrite() {
return;
}
deadline_.expires_from_now(boost::posix_time::seconds(kMaxSendSeconds));
boost::asio::async_write(socket_,
request_->ToBuffers(),
std::bind(&HttpAsyncClient::WriteHandler,
@@ -134,6 +123,8 @@ void HttpAsyncClient::WriteHandler(boost::system::error_code ec) {
response_handler_(response_, kSocketWriteError, timed_out_);
} else {
deadline_.expires_from_now(boost::posix_time::seconds(timeout_seconds_));
AsyncWaitDeadline();
AsyncRead();
}
}
@@ -148,15 +139,11 @@ void HttpAsyncClient::AsyncRead() {
void HttpAsyncClient::ReadHandler(boost::system::error_code ec,
std::size_t length) {
if (stopped_) {
return;
}
LOG_VERB("Socket async read handler.");
if (ec || length == 0) {
Stop();
LOG_ERRO("Socket read error.");
LOG_ERRO("Socket read error (%s).", ec.message().c_str());
response_handler_(response_, kSocketReadError, timed_out_);
return;
}
@@ -188,27 +175,28 @@ void HttpAsyncClient::ReadHandler(boost::system::error_code ec,
return;
}
AsyncRead();
if (!stopped_) {
AsyncRead();
}
}
void HttpAsyncClient::CheckDeadline() {
if (stopped_) {
void HttpAsyncClient::AsyncWaitDeadline() {
deadline_.async_wait(std::bind(&HttpAsyncClient::DeadlineHandler,
shared_from_this(), std::placeholders::_1));
}
void HttpAsyncClient::DeadlineHandler(boost::system::error_code ec) {
LOG_VERB("Deadline handler.");
if (ec == boost::asio::error::operation_aborted) {
LOG_VERB("Deadline timer canceled.");
return;
}
if (deadline_.expires_at() <=
boost::asio::deadline_timer::traits_type::now()) {
// The deadline has passed.
// The socket is closed so that any outstanding asynchronous operations
// are canceled.
LOG_WARN("HTTP client timed out.");
Stop();
timed_out_ = true;
}
LOG_WARN("HTTP client timed out.");
timed_out_ = true;
// Put the actor back to sleep.
deadline_.async_wait(std::bind(&HttpAsyncClient::CheckDeadline,
shared_from_this()));
Stop();
}
} // namespace webcc
+2 -1
View File
@@ -52,7 +52,8 @@ class HttpAsyncClient : public std::enable_shared_from_this<HttpAsyncClient> {
void AsyncRead();
void ReadHandler(boost::system::error_code ec, std::size_t length);
void CheckDeadline();
void AsyncWaitDeadline();
void DeadlineHandler(boost::system::error_code ec);
tcp::socket socket_;
std::unique_ptr<tcp::resolver> resolver_;
+32 -71
View File
@@ -1,5 +1,6 @@
#include "webcc/http_client.h"
#include <algorithm> // for min
#include <string>
#include "boost/asio/connect.hpp"
@@ -10,35 +11,17 @@
#include "boost/lambda/lambda.hpp"
#include "webcc/logger.h"
#include "webcc/utility.h"
using boost::asio::ip::tcp;
namespace webcc {
// Adjust buffer size according to content length.
// This is to avoid reading too many times.
// Also used by AsyncHttpClient.
void AdjustBufferSize(std::size_t content_length, std::vector<char>* buffer) {
const std::size_t kMaxTimes = 10;
// According to test, a client never read more than 200000 bytes a time.
// So it doesn't make sense to set any larger size, e.g., 1MB.
const std::size_t kMaxBufferSize = 200000;
LOG_INFO("Adjust buffer size according to content length.");
std::size_t min_buffer_size = content_length / kMaxTimes;
if (min_buffer_size > buffer->size()) {
buffer->resize(std::min(min_buffer_size, kMaxBufferSize));
LOG_INFO("Resize read buffer to %u.", buffer->size());
} else {
LOG_INFO("Keep the current buffer size: %u.", buffer->size());
}
}
HttpClient::HttpClient()
: socket_(io_context_),
buffer_(kBufferSize),
deadline_(io_context_),
timeout_seconds_(kMaxReceiveSeconds),
timeout_seconds_(kMaxReadSeconds),
stopped_(false),
timed_out_(false),
error_(kNoError) {
@@ -48,12 +31,7 @@ bool HttpClient::Request(const HttpRequest& request) {
response_.reset(new HttpResponse());
response_parser_.reset(new HttpResponseParser(response_.get()));
stopped_ = false;
timed_out_ = false;
// Start the persistent actor that checks for deadline expiry.
deadline_.expires_at(boost::posix_time::pos_infin);
CheckDeadline();
stopped_ = timed_out_ = false;
if ((error_ = Connect(request)) != kNoError) {
return false;
@@ -71,8 +49,6 @@ bool HttpClient::Request(const HttpRequest& request) {
}
Error HttpClient::Connect(const HttpRequest& request) {
using boost::asio::ip::tcp;
tcp::resolver resolver(io_context_);
std::string port = request.port(kHttpPort);
@@ -88,8 +64,6 @@ Error HttpClient::Connect(const HttpRequest& request) {
LOG_VERB("Connect to server...");
deadline_.expires_from_now(boost::posix_time::seconds(kMaxConnectSeconds));
ec = boost::asio::error::would_block;
// ConnectHandler: void (boost::system::error_code, tcp::endpoint)
@@ -109,33 +83,28 @@ Error HttpClient::Connect(const HttpRequest& request) {
// Determine whether a connection was successfully established.
if (ec) {
LOG_ERRO("Socket connect error: %s", ec.message().c_str());
LOG_ERRO("Socket connect error (%s).", ec.message().c_str());
Stop();
return kEndpointConnectError;
}
LOG_VERB("Socket connected.");
// The deadline actor may have had a chance to run and close our socket, even
// though the connect operation notionally succeeded.
if (stopped_) {
// |timed_out_| should be true in this case.
LOG_ERRO("Socket connect timed out.");
return kEndpointConnectError;
}
// ISSUE: |async_connect| reports success on failure.
// See the following bugs:
// - https://svn.boost.org/trac10/ticket/8795
// - https://svn.boost.org/trac10/ticket/8995
return kNoError;
}
Error HttpClient::SendReqeust(const HttpRequest& request) {
LOG_VERB("Send request (timeout: %ds)...", kMaxSendSeconds);
LOG_VERB("HTTP request:\n%s", request.Dump(4, "> ").c_str());
// NOTE:
// It doesn't make much sense to set a timeout for socket write.
// I find that it's almost impossible to simulate a situation in the server
// side to test this timeout.
deadline_.expires_from_now(boost::posix_time::seconds(kMaxSendSeconds));
boost::system::error_code ec = boost::asio::error::would_block;
@@ -149,17 +118,11 @@ Error HttpClient::SendReqeust(const HttpRequest& request) {
} while (ec == boost::asio::error::would_block);
if (ec) {
LOG_ERRO("Socket write error: %s", ec.message().c_str());
LOG_ERRO("Socket write error (%s).", ec.message().c_str());
Stop();
return kSocketWriteError;
}
if (stopped_) {
// |timed_out_| should be true in this case.
LOG_ERRO("Socket write timed out.");
return kSocketWriteError;
}
return kNoError;
}
@@ -167,6 +130,7 @@ Error HttpClient::ReadResponse() {
LOG_VERB("Read response (timeout: %ds)...", timeout_seconds_);
deadline_.expires_from_now(boost::posix_time::seconds(timeout_seconds_));
AsyncWaitDeadline();
Error error = kNoError;
DoReadResponse(&error);
@@ -190,14 +154,10 @@ void HttpClient::DoReadResponse(Error* error) {
LOG_VERB("Socket async read handler.");
if (stopped_) {
return;
}
if (inner_ec || length == 0) {
if (ec || length == 0) {
Stop();
*error = kSocketReadError;
LOG_ERRO("Socket read error.");
LOG_ERRO("Socket read error (%s).", ec.message().c_str());
return;
}
@@ -227,7 +187,9 @@ void HttpClient::DoReadResponse(Error* error) {
return;
}
DoReadResponse(error);
if (!stopped_) {
DoReadResponse(error);
}
});
// Block until the asynchronous operation has completed.
@@ -236,25 +198,23 @@ void HttpClient::DoReadResponse(Error* error) {
} while (ec == boost::asio::error::would_block);
}
void HttpClient::CheckDeadline() {
if (stopped_) {
void HttpClient::AsyncWaitDeadline() {
deadline_.async_wait(std::bind(&HttpClient::DeadlineHandler, this,
std::placeholders::_1));
}
void HttpClient::DeadlineHandler(boost::system::error_code ec) {
LOG_VERB("Deadline handler.");
if (ec == boost::asio::error::operation_aborted) {
LOG_VERB("Deadline timer canceled.");
return;
}
LOG_VERB("Check deadline.");
LOG_WARN("HTTP client timed out.");
timed_out_ = true;
if (deadline_.expires_at() <=
boost::asio::deadline_timer::traits_type::now()) {
// The deadline has passed.
// The socket is closed so that any outstanding asynchronous operations
// are canceled.
LOG_WARN("HTTP client timed out.");
Stop();
timed_out_ = true;
}
// Put the actor back to sleep.
deadline_.async_wait(std::bind(&HttpClient::CheckDeadline, this));
Stop();
}
void HttpClient::Stop() {
@@ -269,6 +229,7 @@ void HttpClient::Stop() {
LOG_ERRO("Failed to close socket.");
}
LOG_INFO("Cancel deadline timer...");
deadline_.cancel();
}
}
+3 -2
View File
@@ -46,7 +46,8 @@ class HttpClient {
void DoReadResponse(Error* error);
void CheckDeadline();
void AsyncWaitDeadline();
void DeadlineHandler(boost::system::error_code ec);
void Stop();
@@ -62,7 +63,7 @@ class HttpClient {
boost::asio::deadline_timer deadline_;
// Maximum seconds to wait before the client cancels the operation.
// Only for receiving response from server.
// Only for reading response from server.
int timeout_seconds_;
bool stopped_;
+21
View File
@@ -1,12 +1,33 @@
#include "webcc/utility.h"
#include <algorithm>
#include <ostream>
#include <sstream>
#include "webcc/logger.h"
using tcp = boost::asio::ip::tcp;
namespace webcc {
void AdjustBufferSize(std::size_t content_length, std::vector<char>* buffer) {
const std::size_t kMaxTimes = 10;
// According to test, a client never read more than 200000 bytes a time.
// So it doesn't make sense to set any larger size, e.g., 1MB.
const std::size_t kMaxBufferSize = 200000;
LOG_INFO("Adjust buffer size according to content length.");
std::size_t min_buffer_size = content_length / kMaxTimes;
if (min_buffer_size > buffer->size()) {
buffer->resize(std::min(min_buffer_size, kMaxBufferSize));
LOG_INFO("Resize read buffer to %u.", buffer->size());
} else {
LOG_INFO("Keep the current buffer size: %u.", buffer->size());
}
}
void PrintEndpoint(std::ostream& ostream,
const boost::asio::ip::tcp::endpoint& endpoint) {
ostream << endpoint;
+5
View File
@@ -3,11 +3,16 @@
#include <iosfwd>
#include <string>
#include <vector>
#include "boost/asio/ip/tcp.hpp"
namespace webcc {
// Adjust buffer size according to content length.
// This is to avoid reading too many times.
void AdjustBufferSize(std::size_t content_length, std::vector<char>* buffer);
void PrintEndpoint(std::ostream& ostream,
const boost::asio::ip::tcp::endpoint& endpoint);