From fdd2b9f2e466c9e3ea1c5bbb126dc960de440075 Mon Sep 17 00:00:00 2001 From: acevedop Date: Mon, 24 Jun 2019 08:23:34 +0200 Subject: [PATCH] Add max_concurrent_streams configuration to asio server --- src/asio_server.cc | 6 ++++-- src/asio_server.h | 3 +++ src/asio_server_connection.h | 7 +++++-- src/asio_server_http2.cc | 19 ++++++++++++------- src/asio_server_http2_handler.cc | 6 ++++-- src/asio_server_http2_handler.h | 2 ++ src/asio_server_http2_impl.cc | 6 ++++-- src/asio_server_http2_impl.h | 3 ++- 8 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/asio_server.cc b/src/asio_server.cc index 74c92276..ec032606 100644 --- a/src/asio_server.cc +++ b/src/asio_server.cc @@ -45,9 +45,11 @@ namespace asio_http2 { namespace server { server::server(std::size_t io_service_pool_size, + uint32_t max_concurrent_streams, const boost::posix_time::time_duration &tls_handshake_timeout, const boost::posix_time::time_duration &read_timeout) : io_service_pool_(io_service_pool_size), + max_concurrent_streams(max_concurrent_streams), tls_handshake_timeout_(tls_handshake_timeout), read_timeout_(read_timeout) {} @@ -130,7 +132,7 @@ void server::start_accept(boost::asio::ssl::context &tls_context, } auto new_connection = std::make_shared>( - mux, tls_handshake_timeout_, read_timeout_, + mux, tls_handshake_timeout_, read_timeout_, max_concurrent_streams_, io_service_pool_.get_io_service(), tls_context); acceptor.async_accept( @@ -169,7 +171,7 @@ void server::start_accept(tcp::acceptor &acceptor, serve_mux &mux) { } auto new_connection = std::make_shared>( - mux, tls_handshake_timeout_, read_timeout_, + mux, tls_handshake_timeout_, read_timeout_, max_concurrent_streams_, io_service_pool_.get_io_service()); acceptor.async_accept( diff --git a/src/asio_server.h b/src/asio_server.h index 1190e322..f86fe188 100644 --- a/src/asio_server.h +++ b/src/asio_server.h @@ -64,6 +64,7 @@ using ssl_socket = boost::asio::ssl::stream; class server : private boost::noncopyable { public: explicit server(std::size_t io_service_pool_size, + uint32_t max_concurrent_streams, const boost::posix_time::time_duration &tls_handshake_timeout, const boost::posix_time::time_duration &read_timeout); @@ -104,6 +105,8 @@ private: std::unique_ptr ssl_ctx_; + uint32_t max_concurrent_streams_; + boost::posix_time::time_duration tls_handshake_timeout_; boost::posix_time::time_duration read_timeout_; }; diff --git a/src/asio_server_connection.h b/src/asio_server_connection.h index daf9a664..364f7398 100644 --- a/src/asio_server_connection.h +++ b/src/asio_server_connection.h @@ -75,9 +75,10 @@ public: serve_mux &mux, const boost::posix_time::time_duration &tls_handshake_timeout, const boost::posix_time::time_duration &read_timeout, - SocketArgs &&... args) + uint32_t max_concurrent_streams, SocketArgs &&... args) : socket_(std::forward(args)...), mux_(mux), + max_concurrent_streams_(max_concurrent_streams), deadline_(GET_IO_SERVICE(socket_)), tls_handshake_timeout_(tls_handshake_timeout), read_timeout_(read_timeout), @@ -89,7 +90,8 @@ public: boost::system::error_code ec; handler_ = std::make_shared( - GET_IO_SERVICE(socket_), socket_.lowest_layer().remote_endpoint(ec), + GET_IO_SERVICE(socket_), max_concurrent_streams_, + socket_.lowest_layer().remote_endpoint(ec), [this]() { do_write(); }, mux_); if (handler_->start() != 0) { stop(); @@ -229,6 +231,7 @@ private: serve_mux &mux_; + uint32_t max_concurrent_streams_; std::shared_ptr handler_; /// Buffer for incoming data. diff --git a/src/asio_server_http2.cc b/src/asio_server_http2.cc index 02d3d197..90f92ce4 100644 --- a/src/asio_server_http2.cc +++ b/src/asio_server_http2.cc @@ -52,17 +52,22 @@ http2 &http2::operator=(http2 &&other) noexcept { return *this; } -boost::system::error_code http2::listen_and_serve(boost::system::error_code &ec, - const std::string &address, - const std::string &port, - bool asynchronous) { - return impl_->listen_and_serve(ec, nullptr, address, port, asynchronous); +boost::system::error_code http2::listen_and_serve( + boost::system::error_code &ec, + const std::string &address, + const std::string &port, + bool asynchronous, + uint32_t max_concurrent_streams) { + return impl_->listen_and_serve(ec, nullptr, address, port, + max_concurrent_streams, asynchronous); } boost::system::error_code http2::listen_and_serve( boost::system::error_code &ec, boost::asio::ssl::context &tls_context, - const std::string &address, const std::string &port, bool asynchronous) { - return impl_->listen_and_serve(ec, &tls_context, address, port, asynchronous); + const std::string &address, const std::string &port, bool asynchronous, + uint32_t max_concurrent_streams) { + return impl_->listen_and_serve(ec, &tls_context, address, port, + max_concurrent_streams, asynchronous); } void http2::num_threads(size_t num_threads) { impl_->num_threads(num_threads); } diff --git a/src/asio_server_http2_handler.cc b/src/asio_server_http2_handler.cc index c1fc195f..32c1a03d 100644 --- a/src/asio_server_http2_handler.cc +++ b/src/asio_server_http2_handler.cc @@ -235,9 +235,11 @@ int on_frame_not_send_callback(nghttp2_session *session, } // namespace http2_handler::http2_handler(boost::asio::io_service &io_service, + uint32_t max_concurrent_streams, boost::asio::ip::tcp::endpoint ep, connection_write writefun, serve_mux &mux) - : writefun_(writefun), + : max_concurrent_streams_(max_concurrent_streams), + writefun_(writefun), mux_(mux), io_service_(io_service), remote_ep_(ep), @@ -298,7 +300,7 @@ int http2_handler::start() { return -1; } - nghttp2_settings_entry ent{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100}; + nghttp2_settings_entry ent{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, max_concurrent_streams_}; nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, &ent, 1); return 0; diff --git a/src/asio_server_http2_handler.h b/src/asio_server_http2_handler.h index 12064499..e9af3b97 100644 --- a/src/asio_server_http2_handler.h +++ b/src/asio_server_http2_handler.h @@ -54,6 +54,7 @@ using connection_write = std::function; class http2_handler : public std::enable_shared_from_this { public: http2_handler(boost::asio::io_service &io_service, + uint32_t max_concurrent_streams, boost::asio::ip::tcp::endpoint ep, connection_write writefun, serve_mux &mux); @@ -152,6 +153,7 @@ public: private: std::map> streams_; + uint32_t max_concurrent_streams_; connection_write writefun_; serve_mux &mux_; boost::asio::io_service &io_service_; diff --git a/src/asio_server_http2_impl.cc b/src/asio_server_http2_impl.cc index 00afdd65..f79062d2 100644 --- a/src/asio_server_http2_impl.cc +++ b/src/asio_server_http2_impl.cc @@ -45,9 +45,11 @@ http2_impl::http2_impl() boost::system::error_code http2_impl::listen_and_serve( boost::system::error_code &ec, boost::asio::ssl::context *tls_context, - const std::string &address, const std::string &port, bool asynchronous) { + const std::string &address, const std::string &port, + uint32_t max_concurrent_streams, bool asynchronous) { server_.reset( - new server(num_threads_, tls_handshake_timeout_, read_timeout_)); + new server(num_threads_, max_concurrent_streams, tls_handshake_timeout_, + read_timeout_)); return server_->listen_and_serve(ec, tls_context, address, port, backlog_, mux_, asynchronous); } diff --git a/src/asio_server_http2_impl.h b/src/asio_server_http2_impl.h index 93a6d2cc..54cbf30d 100644 --- a/src/asio_server_http2_impl.h +++ b/src/asio_server_http2_impl.h @@ -44,7 +44,8 @@ public: http2_impl(); boost::system::error_code listen_and_serve( boost::system::error_code &ec, boost::asio::ssl::context *tls_context, - const std::string &address, const std::string &port, bool asynchronous); + const std::string &address, const std::string &port, + uint32_t max_concurrent_streams, bool asynchronous); void num_threads(size_t num_threads); void backlog(int backlog); void tls_handshake_timeout(const boost::posix_time::time_duration &t);