remove the "write_timer"

This commit is contained in:
Eric Mertens 2025-01-24 16:57:34 -08:00
parent 3b48ff7c7e
commit 1a1deb03b7
2 changed files with 19 additions and 37 deletions

View File

@ -12,13 +12,13 @@ using namespace std::literals;
Connection::Connection(boost::asio::io_context & io) Connection::Connection(boost::asio::io_context & io)
: stream_{io} : stream_{io}
, write_timer_{io, std::chrono::steady_clock::time_point::max()}
, watchdog_timer_{io} , watchdog_timer_{io}
, write_posted_{false}
, stalled_{false} , stalled_{false}
{ {
} }
auto Connection::writer_immediate() -> void auto Connection::write_buffers() -> void
{ {
std::vector<boost::asio::const_buffer> buffers; std::vector<boost::asio::const_buffer> buffers;
buffers.reserve(write_strings_.size()); buffers.reserve(write_strings_.size());
@ -31,38 +31,17 @@ auto Connection::writer_immediate() -> void
buffers, buffers,
[this, strings = std::move(write_strings_)](boost::system::error_code const& error, std::size_t) [this, strings = std::move(write_strings_)](boost::system::error_code const& error, std::size_t)
{ {
if (not error) if (not error) {
{ if (write_strings_.empty()) {
writer(); write_posted_ = false;
} else {
write_buffers();
}
} }
}); });
write_strings_.clear(); write_strings_.clear();
} }
auto Connection::writer() -> void
{
if (write_strings_.empty())
{
write_timer_.async_wait([weak = weak_from_this()](auto){
// This wait will always trigger on a cancellation. That
// cancellation might be from write_line or it might be from
// the connection being destroyed. The weak pointer will fail
// to lock in the case that the object is being destructed.
if (auto self = weak.lock())
{
if (not self->write_strings_.empty())
{
self->writer_immediate();
}
}
});
}
else
{
writer_immediate();
}
}
auto Connection::connect( auto Connection::connect(
boost::asio::io_context & io, boost::asio::io_context & io,
std::string host, std::string host,
@ -83,7 +62,6 @@ auto Connection::connect(
} }
// Start the queue writer after connection // Start the queue writer after connection
writer();
watchdog(); watchdog();
for(LineBuffer buffer{32'768};;) for(LineBuffer buffer{32'768};;)
@ -178,11 +156,16 @@ auto Connection::write_line(std::string message) -> void
{ {
BOOST_LOG_TRIVIAL(debug) << "SEND: " << message; BOOST_LOG_TRIVIAL(debug) << "SEND: " << message;
message += "\r\n"; message += "\r\n";
auto const need_cancel = write_strings_.empty();
write_strings_.push_back(std::move(message)); write_strings_.push_back(std::move(message));
if (need_cancel)
if (not write_posted_) {
write_posted_ = true;
boost::asio::post(stream_.get_executor(), [weak = weak_from_this()](){
if (auto self = weak.lock())
{ {
write_timer_.cancel_one(); self->write_buffers();
}
});
} }
} }

View File

@ -15,16 +15,15 @@ class Connection : public std::enable_shared_from_this<Connection>
{ {
private: private:
boost::asio::ip::tcp::socket stream_; boost::asio::ip::tcp::socket stream_;
boost::asio::steady_timer write_timer_;
boost::asio::steady_timer watchdog_timer_; boost::asio::steady_timer watchdog_timer_;
std::list<std::string> write_strings_; std::list<std::string> write_strings_;
bool write_posted_;
// Set true when watchdog triggers. // Set true when watchdog triggers.
// Set false when message received. // Set false when message received.
bool stalled_; bool stalled_;
auto writer() -> void; auto write_buffers() -> void;
auto writer_immediate() -> void;
auto dispatch_line(char * line) -> void; auto dispatch_line(char * line) -> void;
static constexpr std::chrono::seconds watchdog_duration = std::chrono::seconds{30}; static constexpr std::chrono::seconds watchdog_duration = std::chrono::seconds{30};