Skip to content

Commit

Permalink
Use flag for timer activation
Browse files Browse the repository at this point in the history
Use boolean flag instead of timer pointer,
and avoid to remove it inside the asyn handler.
  • Loading branch information
testillano authored and Eduardo Ramos Testillano (eramedu) committed Jul 27, 2023
1 parent 6340a3e commit dee44c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/ert/http2comm/Stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class Stream : public ert::queuedispatcher::StreamIf
unsigned int status_code_{};
nghttp2::asio_http2::header_map response_headers_{};
std::string response_body_{};
boost::asio::steady_timer *timer_{};
std::shared_ptr<boost::asio::steady_timer> timer_{};
bool need_timer_{};

// For metrics:
std::chrono::microseconds reception_timestamp_us_{}; // timestamp in microsecods
Expand Down
19 changes: 13 additions & 6 deletions src/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace http2comm
{
Stream::Stream(const nghttp2::asio_http2::server::request& req,
const nghttp2::asio_http2::server::response& res,
Http2Server *server) : req_(req), res_(res), server_(server), closed_(false), error_(false), timer_(nullptr) {
Http2Server *server) : req_(req), res_(res), server_(server), closed_(false), error_(false), timer_(nullptr), need_timer_(false) {

if (server_->preReserveRequestBody()) request_body_.reserve(server_->maximum_request_body_size_.load());
}
Expand Down Expand Up @@ -129,7 +129,8 @@ void Stream::reception(bool congestion)
}
);

timer_ = new boost::asio::steady_timer(*(server_->getTimersIoContext()), std::chrono::microseconds(responseDelayUs));
timer_ = std::make_shared<boost::asio::steady_timer>(*(server_->getTimersIoContext()), std::chrono::microseconds(responseDelayUs));
need_timer_ = true;
}
else {
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 All @@ -139,11 +140,17 @@ void Stream::reception(bool congestion)

void Stream::commit()
{
if (timer_)
if (need_timer_)
{
timer_->async_wait([&] (const boost::system::error_code&) {
delete timer_;
timer_ = nullptr;
/*
timer_->async_wait([&] (const boost::system::error_code&) {
delete timer_;
timer_ = nullptr;
commit();
});
*/
timer_->async_wait([this] (const boost::system::error_code&) {
need_timer_ = false;
commit();
});
return;
Expand Down

0 comments on commit dee44c8

Please sign in to comment.