Skip to content

Commit

Permalink
[5649] Manage HTTP client connection timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
msiodelski committed Jun 13, 2018
1 parent 24e93ec commit a93e7c2
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/lib/http/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ class Connection : public boost::enable_shared_from_this<Connection> {
void terminate(const boost::system::error_code& ec,
const std::string& parsing_error = "");

/// @brief This method schedules timer or reschedules existing timer.
///
/// @param request_timeout New timer interval in milliseconds.
void scheduleTimer(const long request_timeout);

/// @brief Asynchronously sends data over the socket.
///
/// The data sent over the socket are stored in the @c buf_.
Expand Down Expand Up @@ -559,6 +564,14 @@ Connection::terminate(const boost::system::error_code& ec,
}
}

void
Connection::scheduleTimer(const long request_timeout) {
if (request_timeout > 0) {
timer_.setup(boost::bind(&Connection::timerCallback, this), request_timeout,
IntervalTimer::ONE_SHOT);
}
}

void
Connection::doSend() {
SocketCallback socket_cb(boost::bind(&Connection::sendCallback, shared_from_this(),
Expand Down Expand Up @@ -598,8 +611,8 @@ Connection::connectCallback(const long request_timeout, const boost::system::err

} else {
// Setup request timer.
timer_.setup(boost::bind(&Connection::timerCallback, this), request_timeout,
IntervalTimer::ONE_SHOT);
scheduleTimer(request_timeout);

// Start sending the request asynchronously.
doSend();
}
Expand All @@ -621,6 +634,9 @@ Connection::sendCallback(const boost::system::error_code& ec, size_t length) {
}
}

// Sending is in progress, so push back the timeout.
scheduleTimer(timer_.getInterval());

// If any data have been sent, remove it from the buffer and only leave the
// portion that still has to be sent.
if (length > 0) {
Expand Down Expand Up @@ -654,6 +670,9 @@ Connection::receiveCallback(const boost::system::error_code& ec, size_t length)
}
}

// Receiving is in progress, so push back the timeout.
scheduleTimer(timer_.getInterval());

// If we have received any data, let's feed the parser with it.
if (length != 0) {
parser_->postBuffer(static_cast<void*>(input_buf_.data()), length);
Expand Down

0 comments on commit a93e7c2

Please sign in to comment.