Skip to content

Commit

Permalink
Receive timeout should only be active for a second and
Browse files Browse the repository at this point in the history
following requests on a keep-alive connections.
  • Loading branch information
Denis Chaplygin committed Aug 20, 2019
1 parent 30ad221 commit 87ddd11
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
4 changes: 3 additions & 1 deletion include/server/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class Connection : public std::enable_shared_from_this<Connection>
void handle_write(const boost::system::error_code &e);

/// Handle read timeout
void handle_timeout();
void handle_timeout(boost::system::error_code);

void handle_shutdown();

std::vector<char> compress_buffers(const std::vector<char> &uncompressed_data,
const http::compression_type compression_type);
Expand Down
40 changes: 29 additions & 11 deletions src/server/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <iterator>
#include <string>
#include <util/log.hpp>
#include <vector>

namespace osrm
Expand All @@ -35,10 +34,13 @@ void Connection::start()
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));

// init async timer
timer.cancel();
timer.expires_from_now(boost::posix_time::seconds(keepalive_timeout));
timer.async_wait(boost::bind(&Connection::handle_timeout, this->shared_from_this()));
if (keep_alive) {
//Ok, we know it is not a first request, as we switched to keepalive
timer.cancel();
timer.expires_from_now(boost::posix_time::seconds(keepalive_timeout));
timer.async_wait(
std::bind(&Connection::handle_timeout, this->shared_from_this(), std::placeholders::_1));
}
}

void Connection::handle_read(const boost::system::error_code &error, std::size_t bytes_transferred)
Expand All @@ -48,6 +50,11 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
return;
}

if (keep_alive) {
timer.cancel();
timer.expires_from_now(boost::posix_time::seconds(0));
}

// no error detected, let's parse the request
http::compression_type compression_type(http::no_compression);
RequestParser::RequestStatus result;
Expand Down Expand Up @@ -132,8 +139,7 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
/// Handle completion of a write operation.
void Connection::handle_write(const boost::system::error_code &error)
{
if (!error)
{
if (!error) {
if (keep_alive && processed_requests > 0)
{
--processed_requests;
Expand All @@ -143,15 +149,27 @@ void Connection::handle_write(const boost::system::error_code &error)
}
else
{
// Initiate graceful connection closure.
handle_timeout();
handle_shutdown();
}
}
}

/// Handle completion of a write operation.
void Connection::handle_timeout()
/// Handle completion of a timeout timer..
void Connection::handle_timeout(boost::system::error_code ec)
{
// We can get there for 3 reasons: spurious wakeup by timer.cancel(), which should be ignored
// Slow client with a delayed _first_ request, which should be ignored too
// Absent next request during waiting time in the keepalive mode - should stop right there.
if (ec != boost::asio::error::operation_aborted)
{
TCP_socket.cancel();
handle_shutdown();
}
}

void Connection::handle_shutdown()
{
// Initiate graceful connection closure.
boost::system::error_code ignore_error;
TCP_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignore_error);
}
Expand Down

0 comments on commit 87ddd11

Please sign in to comment.