fix: Fix timer spurious calls (#1700)

Fixes #1634.
I also checked other timers and they don't have the issue.
This commit is contained in:
Sergey Kuznetsov
2024-10-23 14:24:05 +01:00
committed by GitHub
parent f351a4cc79
commit cf081e7e25
2 changed files with 22 additions and 6 deletions

View File

@@ -57,10 +57,16 @@ Retry::Retry(RetryStrategyPtr strategy, boost::asio::strand<boost::asio::io_cont
{
}
Retry::~Retry()
{
*canceled_ = true;
}
void
Retry::cancel()
{
timer_.cancel();
*canceled_ = true;
}
size_t

View File

@@ -24,6 +24,7 @@
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/strand.hpp>
#include <atomic>
#include <chrono>
#include <cstddef>
#include <memory>
@@ -80,6 +81,7 @@ class Retry {
RetryStrategyPtr strategy_;
boost::asio::steady_timer timer_;
size_t attemptNumber_ = 0;
std::shared_ptr<std::atomic_bool> canceled_{std::make_shared<std::atomic_bool>(false)};
public:
/**
@@ -90,6 +92,11 @@ public:
*/
Retry(RetryStrategyPtr strategy, boost::asio::strand<boost::asio::io_context::executor_type> strand);
/**
* @brief Destroy the Retry object
*/
~Retry();
/**
* @brief Schedule a retry
*
@@ -100,15 +107,18 @@ public:
void
retry(Fn&& func)
{
*canceled_ = false;
timer_.expires_after(strategy_->getDelay());
strategy_->increaseDelay();
timer_.async_wait([this, func = std::forward<Fn>(func)](boost::system::error_code const& ec) {
if (ec == boost::asio::error::operation_aborted) {
return;
timer_.async_wait(
[this, canceled = canceled_, func = std::forward<Fn>(func)](boost::system::error_code const& ec) {
if (ec == boost::asio::error::operation_aborted or *canceled) {
return;
}
++attemptNumber_;
func();
}
++attemptNumber_;
func();
});
);
}
/**