From 6340a3e162c9229423aa9e9c5b6f326c1e862674 Mon Sep 17 00:00:00 2001 From: "Eduardo Ramos Testillano (ert)" Date: Tue, 25 Jul 2023 20:28:33 +0200 Subject: [PATCH] Migrate deadline_timer and io_service Feature: https://github.com/testillano/http2comm/issues/20 --- include/ert/http2comm/Http2Connection.hpp | 8 ++++---- include/ert/http2comm/Http2Server.hpp | 16 ++++++++-------- include/ert/http2comm/Stream.hpp | 4 ++-- src/Http2Connection.cpp | 14 +++++++------- src/Http2Server.cpp | 6 +++--- src/Stream.cpp | 8 +++----- 6 files changed, 27 insertions(+), 29 deletions(-) diff --git a/include/ert/http2comm/Http2Connection.hpp b/include/ert/http2comm/Http2Connection.hpp index a5bb235..c9269e7 100644 --- a/include/ert/http2comm/Http2Connection.hpp +++ b/include/ert/http2comm/Http2Connection.hpp @@ -79,8 +79,8 @@ class Http2Connection CLOSED }; - // Seccion factory with io_service, host, port and secure inputs: - nghttp2::asio_http2::client::session createSession(boost::asio::io_service &ioService, const std::string &host, const std::string &port, bool secure); + // Seccion factory with io_context, host, port and secure inputs: + nghttp2::asio_http2::client::session createSession(boost::asio::io_context &ioContext, const std::string &host, const std::string &port, bool secure); public: /// Class constructors @@ -226,8 +226,8 @@ class Http2Connection private: /// ASIO attributes - boost::asio::io_service io_service_; - boost::asio::io_service::work work_; + boost::asio::io_context io_context_; + boost::asio::io_context::work work_; nghttp2::asio_http2::client::session session_; /// Class attributes diff --git a/include/ert/http2comm/Http2Server.hpp b/include/ert/http2comm/Http2Server.hpp index 608487d..681f783 100644 --- a/include/ert/http2comm/Http2Server.hpp +++ b/include/ert/http2comm/Http2Server.hpp @@ -69,7 +69,7 @@ class Http2Server // Metric names should not be too long or too short. std::string api_name_{}; std::string api_version_{}; - boost::asio::io_service *timers_io_service_; + boost::asio::io_context *timers_io_context_; ert::queuedispatcher::QueueDispatcher *queue_dispatcher_; int queue_dispatcher_max_size_{}; @@ -114,12 +114,12 @@ class Http2Server * @param name Server name (lower case, as it is used to name prometheus metrics). * @param workerThreads number of worker threads. * @param maxWorkerThreads number of maximum worker threads which internal processing could grow to. Defaults to '0' which means that maximum equals to provided worker threads. - * @param timerIoService Optional io service to manage response delays + * @param timerIoContext Optional io context to manage response delays * @param queueDispatcherMaxSize This library implements a simple congestion control algorithm which will indicate congestion status when queue dispatcher (when used) has no * idle consumer threads, and queue dispatcher size is over this value. Defaults to -1 which means 'no limit' to grow the queue (this probably implies response time degradation). * So, to enable the described congestion control algorithm, provide a non-negative value. */ - Http2Server(const std::string& name, size_t workerThreads, size_t maxWorkerThreads = 0, boost::asio::io_service *timerIoService = nullptr, int queueDispatcherMaxSize = -1 /* no limit */); + Http2Server(const std::string& name, size_t workerThreads, size_t maxWorkerThreads = 0, boost::asio::io_context *timerIoContext = nullptr, int queueDispatcherMaxSize = -1 /* no limit */); virtual ~Http2Server(); // setters @@ -320,9 +320,9 @@ class Http2Server * @param key secure key for HTTP/2 communication. * @param cert secure cert for HTTP/2 communication. * @param numberThreads nghttp2 server threads (multi client). - * @param read activity keep alive period. If server does not receive data in this - * period, then it will close the connection. Default value is 1 minute. * @param asynchronous boolean for non-blocking server start. + * @param readKeepAlive read activity keep alive period. If server does not receive data in this + * period, then it will close the connection. Default value is 1 minute. * * @return exit code (EXIT_SUCCESS|EXIT_FAILURE) */ @@ -335,10 +335,10 @@ class Http2Server const boost::posix_time::time_duration &readKeepAlive = boost::posix_time::seconds(60)); /** - * Gets the timers io service used to manage response delays + * Gets the timers io context used to manage response delays */ - boost::asio::io_service *getTimersIoService() const { - return timers_io_service_; + boost::asio::io_context *getTimersIoContext() const { + return timers_io_context_; } /** diff --git a/include/ert/http2comm/Stream.hpp b/include/ert/http2comm/Stream.hpp index ea44578..40136f2 100644 --- a/include/ert/http2comm/Stream.hpp +++ b/include/ert/http2comm/Stream.hpp @@ -65,7 +65,7 @@ class Http2Server; * and launch the working thread with the stream (or insert in a concurrent queue): * *
- *    auto stream = std::make_shared(req, res, res.io_service, requestStream, server);
+ *    auto stream = std::make_shared(req, res, res.io_context, requestStream, server);
  *    res.on_close([stream](uint32_t error_code) { stream->close(true); });
  *    auto thread = std::thread([stream]()
  *    {
@@ -90,7 +90,7 @@ class Stream : public ert::queuedispatcher::StreamIf
     unsigned int status_code_{};
     nghttp2::asio_http2::header_map response_headers_{};
     std::string response_body_{};
-    boost::asio::deadline_timer *timer_{};
+    boost::asio::steady_timer *timer_{};
 
     // For metrics:
     std::chrono::microseconds reception_timestamp_us_{}; // timestamp in microsecods
diff --git a/src/Http2Connection.cpp b/src/Http2Connection.cpp
index 0dbf50e..ad24b4d 100644
--- a/src/Http2Connection.cpp
+++ b/src/Http2Connection.cpp
@@ -56,17 +56,17 @@ namespace ert
 {
 namespace http2comm
 {
-nghttp2::asio_http2::client::session Http2Connection::createSession(boost::asio::io_service &ioService, const std::string &host, const std::string &port, bool secure) {
+nghttp2::asio_http2::client::session Http2Connection::createSession(boost::asio::io_context &ioContext, const std::string &host, const std::string &port, bool secure) {
     if (secure)
     {
         boost::system::error_code ec;
         boost::asio::ssl::context tls_ctx(boost::asio::ssl::context::sslv23);
         tls_ctx.set_default_verify_paths();
         nghttp2::asio_http2::client::configure_tls_context(ec, tls_ctx);
-        return nghttp2::asio_http2::client::session(ioService, tls_ctx, host, port);
+        return nghttp2::asio_http2::client::session(ioContext, tls_ctx, host, port);
     }
 
-    return nghttp2::asio_http2::client::session(ioService, host, port);
+    return nghttp2::asio_http2::client::session(ioContext, host, port);
 }
 
 void Http2Connection::configureSession() {
@@ -87,15 +87,15 @@ void Http2Connection::configureSession() {
 Http2Connection::Http2Connection(const std::string& host,
                                  const std::string& port,
                                  bool secure) :
-    work_(boost::asio::io_service::work(io_service_)),
+    work_(boost::asio::io_context::work(io_context_)),
     status_(Status::NOT_OPEN),
     host_(host),
     port_(port),
     secure_(secure),
-    session_(nghttp2::asio_http2::client::session(createSession(io_service_, host, port, secure)))
+    session_(nghttp2::asio_http2::client::session(createSession(io_context_, host, port, secure)))
 {
     configureSession();
-    thread_ = std::thread([&] { io_service_.run(); });
+    thread_ = std::thread([&] { io_context_.run(); });
 }
 
 Http2Connection::~Http2Connection()
@@ -117,7 +117,7 @@ void Http2Connection::notifyClose()
 
 void Http2Connection::closeImpl()
 {
-    io_service_.stop();
+    io_context_.stop();
 
     if (thread_.joinable())
     {
diff --git a/src/Http2Server.cpp b/src/Http2Server.cpp
index ebc4f2b..d9f15a2 100644
--- a/src/Http2Server.cpp
+++ b/src/Http2Server.cpp
@@ -58,8 +58,8 @@ namespace ert
 namespace http2comm
 {
 
-Http2Server::Http2Server(const std::string& name, size_t workerThreads, size_t maxWorkerThreads, boost::asio::io_service *timersIoService, int queueDispatcherMaxSize):
-    name_(name), timers_io_service_(timersIoService), reception_id_(0), maximum_request_body_size_(0), queue_dispatcher_max_size_(queueDispatcherMaxSize) {
+Http2Server::Http2Server(const std::string& name, size_t workerThreads, size_t maxWorkerThreads, boost::asio::io_context *timersIoContext, int queueDispatcherMaxSize):
+    name_(name), timers_io_context_(timersIoContext), reception_id_(0), maximum_request_body_size_(0), queue_dispatcher_max_size_(queueDispatcherMaxSize) {
 
     queue_dispatcher_ = (workerThreads > 1) ? new ert::queuedispatcher::QueueDispatcher(name + "_queueDispatcher", workerThreads, maxWorkerThreads) : nullptr;
 }
@@ -326,7 +326,7 @@ int Http2Server::stop()
 {
     try
     {
-        // stop internal io services
+        // stop internal io contexts
         for (auto &ii: server_.io_services()) if (!ii->stopped()) ii->stop();
         server_.stop();
     }
diff --git a/src/Stream.cpp b/src/Stream.cpp
index 8f0e22b..3985b08 100644
--- a/src/Stream.cpp
+++ b/src/Stream.cpp
@@ -38,8 +38,6 @@ SOFTWARE.
 */
 #include 
 
-#include 
-
 #include 
 
 #include 
@@ -113,7 +111,7 @@ void Stream::reception(bool congestion)
 
     // Optional reponse delay
     if (responseDelayMs != 0) { // optional delay:
-        if (server_->getTimersIoService()) {
+        if (server_->getTimersIoContext()) {
             auto responseDelayUs = 1000*responseDelayMs;
             LOGDEBUG(
                 std::string msg = ert::tracing::Logger::asString("Planned delay before response: %d us", responseDelayUs);
@@ -131,10 +129,10 @@ void Stream::reception(bool congestion)
             }
             );
 
-            timer_ = new boost::asio::deadline_timer(*(server_->getTimersIoService()), boost::posix_time::microseconds(responseDelayUs));
+            timer_ = new boost::asio::steady_timer(*(server_->getTimersIoContext()), std::chrono::microseconds(responseDelayUs));
         }
         else {
-            LOGWARNING(ert::tracing::Logger::warning("You must provide an 'io service for timers' in order to manage delays in http2 server", ERT_FILE_LOCATION));
+            LOGWARNING(ert::tracing::Logger::warning("You must provide an 'io context for timers' in order to manage delays in http2 server", ERT_FILE_LOCATION));
         }
     }
 }