add keepalive_timeout flag

This commit is contained in:
xin.wang 2023-08-11 17:30:07 +08:00
parent db7946d762
commit f2fef35f27
8 changed files with 38 additions and 13 deletions

View File

@ -1,6 +1,7 @@
# Unreleased # Unreleased
- Changes from 5.27.1 - Changes from 5.27.1
- Features - Features
- ADDED: Add support for a keepalive_timeout flag.
- ADDED: Add support for a default_radius flag. [#6575](https://github.com/Project-OSRM/osrm-backend/pull/6575) - ADDED: Add support for a default_radius flag. [#6575](https://github.com/Project-OSRM/osrm-backend/pull/6575)
- ADDED: Add support for disabling feature datasets. [#6666](https://github.com/Project-OSRM/osrm-backend/pull/6666) - ADDED: Add support for disabling feature datasets. [#6666](https://github.com/Project-OSRM/osrm-backend/pull/6666)
- Build: - Build:

View File

@ -40,6 +40,7 @@ var osrm = new OSRM('network.osrm');
- `options.max_results_nearest` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Max. results supported in nearest query (default: unlimited). - `options.max_results_nearest` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Max. results supported in nearest query (default: unlimited).
- `options.max_alternatives` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Max. number of alternatives supported in alternative routes query (default: 3). - `options.max_alternatives` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Max. number of alternatives supported in alternative routes query (default: 3).
- `options.default_radius` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Default radius for queries (default: unlimited). - `options.default_radius` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Default radius for queries (default: unlimited).
- `options.keepalive_timeout` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Seconds used as the server-side keepalive_timeout (default: 5).
### route ### route

View File

@ -23,6 +23,7 @@ Feature: osrm-routed command line options: help
And stdout should contain "--max-table-size" And stdout should contain "--max-table-size"
And stdout should contain "--max-matching-size" And stdout should contain "--max-matching-size"
And stdout should contain "--default-radius" And stdout should contain "--default-radius"
And stdout should contain "--keepalive-timeout"
And it should exit successfully And it should exit successfully
Scenario: osrm-routed - Help, short Scenario: osrm-routed - Help, short
@ -44,6 +45,7 @@ Feature: osrm-routed command line options: help
And stdout should contain "--max-table-size" And stdout should contain "--max-table-size"
And stdout should contain "--max-matching-size" And stdout should contain "--max-matching-size"
And stdout should contain "--default-radius" And stdout should contain "--default-radius"
And stdout should contain "--keepalive-timeout"
And it should exit successfully And it should exit successfully
Scenario: osrm-routed - Help, long Scenario: osrm-routed - Help, long
@ -65,4 +67,5 @@ Feature: osrm-routed command line options: help
And stdout should contain "--max-table-size" And stdout should contain "--max-table-size"
And stdout should contain "--max-matching-size" And stdout should contain "--max-matching-size"
And stdout should contain "--default-radius" And stdout should contain "--default-radius"
And stdout should contain "--keepalive-timeout"
And it should exit successfully And it should exit successfully

View File

@ -35,7 +35,7 @@ class RequestHandler;
class Connection : public std::enable_shared_from_this<Connection> class Connection : public std::enable_shared_from_this<Connection>
{ {
public: public:
explicit Connection(boost::asio::io_context &io_context, RequestHandler &handler); explicit Connection(boost::asio::io_context &io_context, RequestHandler &handler, short keepalive_timeout);
Connection(const Connection &) = delete; Connection(const Connection &) = delete;
Connection &operator=(const Connection &) = delete; Connection &operator=(const Connection &) = delete;

View File

@ -32,17 +32,17 @@ class Server
public: public:
// Note: returns a shared instead of a unique ptr as it is captured in a lambda somewhere else // Note: returns a shared instead of a unique ptr as it is captured in a lambda somewhere else
static std::shared_ptr<Server> static std::shared_ptr<Server>
CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads) CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads, short keepalive_timeout)
{ {
util::Log() << "http 1.1 compression handled by zlib version " << zlibVersion(); util::Log() << "http 1.1 compression handled by zlib version " << zlibVersion();
const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency()); const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency());
const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads); const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads);
return std::make_shared<Server>(ip_address, ip_port, real_num_threads); return std::make_shared<Server>(ip_address, ip_port, real_num_threads, keepalive_timeout);
} }
explicit Server(const std::string &address, const int port, const unsigned thread_pool_size) explicit Server(const std::string &address, const int port, const unsigned thread_pool_size, const short keepalive_timeout)
: thread_pool_size(thread_pool_size), acceptor(io_context), : thread_pool_size(thread_pool_size), keepalive_timeout(keepalive_timeout), acceptor(io_context),
new_connection(std::make_shared<Connection>(io_context, request_handler)) new_connection(std::make_shared<Connection>(io_context, request_handler, keepalive_timeout))
{ {
const auto port_string = std::to_string(port); const auto port_string = std::to_string(port);
@ -94,7 +94,7 @@ class Server
if (!e) if (!e)
{ {
new_connection->start(); new_connection->start();
new_connection = std::make_shared<Connection>(io_context, request_handler); new_connection = std::make_shared<Connection>(io_context, request_handler, keepalive_timeout);
acceptor.async_accept( acceptor.async_accept(
new_connection->socket(), new_connection->socket(),
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error)); boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
@ -107,6 +107,7 @@ class Server
RequestHandler request_handler; RequestHandler request_handler;
unsigned thread_pool_size; unsigned thread_pool_size;
short keepalive_timeout;
boost::asio::io_context io_context; boost::asio::io_context io_context;
boost::asio::ip::tcp::acceptor acceptor; boost::asio::ip::tcp::acceptor acceptor;
std::shared_ptr<Connection> new_connection; std::shared_ptr<Connection> new_connection;

View File

@ -8,13 +8,14 @@
#include <boost/iostreams/filtering_stream.hpp> #include <boost/iostreams/filtering_stream.hpp>
#include <vector> #include <vector>
#include <fmt/format.h>
namespace osrm::server namespace osrm::server
{ {
Connection::Connection(boost::asio::io_context &io_context, RequestHandler &handler) Connection::Connection(boost::asio::io_context &io_context, RequestHandler &handler, short keepalive_timeout)
: strand(boost::asio::make_strand(io_context)), TCP_socket(strand), timer(strand), : strand(boost::asio::make_strand(io_context)), TCP_socket(strand), timer(strand),
request_handler(handler) request_handler(handler), keepalive_timeout(keepalive_timeout)
{ {
} }
@ -88,7 +89,7 @@ void Connection::handle_read(const boost::system::error_code &error, std::size_t
{ {
keep_alive = true; keep_alive = true;
current_reply.headers.emplace_back("Connection", "keep-alive"); current_reply.headers.emplace_back("Connection", "keep-alive");
current_reply.headers.emplace_back("Keep-Alive", "timeout=5, max=512"); current_reply.headers.emplace_back("Keep-Alive", "timeout="+ fmt::to_string(keepalive_timeout) + ", max=" + fmt::to_string(processed_requests));
} }
// compress the result w/ gzip/deflate if requested // compress the result w/ gzip/deflate if requested

View File

@ -107,7 +107,8 @@ inline unsigned generateServerProgramOptions(const int argc,
int &ip_port, int &ip_port,
bool &trial, bool &trial,
EngineConfig &config, EngineConfig &config,
int &requested_thread_num) int &requested_thread_num,
short &keepalive_timeout)
{ {
using boost::filesystem::path; using boost::filesystem::path;
using boost::program_options::value; using boost::program_options::value;
@ -140,6 +141,9 @@ inline unsigned generateServerProgramOptions(const int argc,
("threads,t", ("threads,t",
value<int>(&requested_thread_num)->default_value(hardware_threads), value<int>(&requested_thread_num)->default_value(hardware_threads),
"Number of threads to use") // "Number of threads to use") //
("keepalive-timeout,k",
value<short>(&keepalive_timeout)->default_value(5),
"Default keepalive-timeout. Default: 5 seconds.") //
("shared-memory,s", ("shared-memory,s",
value<bool>(&config.use_shared_memory)->implicit_value(true)->default_value(false), value<bool>(&config.use_shared_memory)->implicit_value(true)->default_value(false),
"Load data from shared memory") // "Load data from shared memory") //
@ -266,8 +270,9 @@ try
boost::filesystem::path base_path; boost::filesystem::path base_path;
int requested_thread_num = 1; int requested_thread_num = 1;
short keepalive_timeout = 5;
const unsigned init_result = generateServerProgramOptions( const unsigned init_result = generateServerProgramOptions(
argc, argv, base_path, ip_address, ip_port, trial_run, config, requested_thread_num); argc, argv, base_path, ip_address, ip_port, trial_run, config, requested_thread_num, keepalive_timeout);
if (init_result == INIT_OK_DO_NOT_START_ENGINE) if (init_result == INIT_OK_DO_NOT_START_ENGINE)
{ {
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -307,6 +312,7 @@ try
util::Log() << "Threads: " << requested_thread_num; util::Log() << "Threads: " << requested_thread_num;
util::Log() << "IP address: " << ip_address; util::Log() << "IP address: " << ip_address;
util::Log() << "IP port: " << ip_port; util::Log() << "IP port: " << ip_port;
util::Log() << "Keepalive timeout: " << keepalive_timeout;
#ifndef _WIN32 #ifndef _WIN32
int sig = 0; int sig = 0;
@ -319,7 +325,7 @@ try
#endif #endif
auto service_handler = std::make_unique<server::ServiceHandler>(config); auto service_handler = std::make_unique<server::ServiceHandler>(config);
auto routing_server = server::Server::CreateServer(ip_address, ip_port, requested_thread_num); auto routing_server = server::Server::CreateServer(ip_address, ip_port, requested_thread_num, keepalive_timeout);
routing_server->RegisterServiceHandler(std::move(service_handler)); routing_server->RegisterServiceHandler(std::move(service_handler));

View File

@ -131,6 +131,18 @@ test('constructor: throws if default_radius is not a number', function(assert) {
assert.ok(new OSRM({algorithm: 'MLD', path: monaco_mld_path, default_radius: 'unlimited'}), 'Does accept unlimited'); assert.ok(new OSRM({algorithm: 'MLD', path: monaco_mld_path, default_radius: 'unlimited'}), 'Does accept unlimited');
}); });
test('constructor: takes a keepalive_timeout argument', function(assert) {
assert.plan(1);
var osrm = new OSRM({algorithm: 'MLD', path: monaco_mld_path, keepalive_timeout: 10});
assert.ok(osrm);
});
test('constructor: throws if keepalive_timeout is not a number', function(assert) {
assert.plan(2);
assert.throws(function() { new OSRM({algorithm: 'MLD', path: monaco_mld_path, keepalive_timeout: 'abc'}); }, /keepalive_timeout must be an integral number/, 'Does not accept invalid string');
assert.ok(new OSRM({algorithm: 'MLD', path: monaco_mld_path, keepalive_timeout: 1}), 'Does accept number');
});
test('constructor: parses custom limits', function(assert) { test('constructor: parses custom limits', function(assert) {
assert.plan(1); assert.plan(1);
var osrm = new OSRM({ var osrm = new OSRM({