Skip to content

Commit

Permalink
Migrate deadline_timer and io_service
Browse files Browse the repository at this point in the history
Feature: #20
  • Loading branch information
testillano authored and Eduardo Ramos Testillano (eramedu) committed Jul 25, 2023
1 parent 6c25ac5 commit 6340a3e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
8 changes: 4 additions & 4 deletions include/ert/http2comm/Http2Connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions include/ert/http2comm/Http2Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_{};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
*/
Expand All @@ -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_;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions include/ert/http2comm/Stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Http2Server;
* and launch the working thread with the stream (or insert in a concurrent queue):
*
* <pre>
* auto stream = std::make_shared<Stream>(req, res, res.io_service, requestStream, server);
* auto stream = std::make_shared<Stream>(req, res, res.io_context, requestStream, server);
* res.on_close([stream](uint32_t error_code) { stream->close(true); });
* auto thread = std::thread([stream]()
* {
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/Http2Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
Expand All @@ -117,7 +117,7 @@ void Http2Connection::notifyClose()

void Http2Connection::closeImpl()
{
io_service_.stop();
io_context_.stop();

if (thread_.joinable())
{
Expand Down
6 changes: 3 additions & 3 deletions src/Http2Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
Expand Down
8 changes: 3 additions & 5 deletions src/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ SOFTWARE.
*/
#include <nghttp2/asio_http2_server.h>

#include <boost/date_time/posix_time/posix_time.hpp>

#include <ert/tracing/Logger.hpp>

#include <ert/http2comm/Stream.hpp>
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}
}
Expand Down

0 comments on commit 6340a3e

Please sign in to comment.