Protect the timer to avoid "cancel" and "refresh" happens at the same time.

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2022-10-24 10:07:53 +08:00
parent c7f17cdf0d
commit 8395e821fc
2 changed files with 6 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include <chrono>
#include <exception>
#include <functional>
#include <mutex>
#include <string>
#include <thread>
@ -117,6 +118,9 @@ namespace etcd
int ttl;
int64_t lease_id;
// protect the initializing status of `timer`.
std::mutex mutex_for_refresh_;
std::atomic_bool continue_next;
// grpc timeout in `refresh()`

View File

@ -124,6 +124,7 @@ etcd::KeepAlive::~KeepAlive()
void etcd::KeepAlive::Cancel()
{
std::lock_guard<std::mutex> scope_lock(mutex_for_refresh_);
if (!continue_next.exchange(false)) {
return;
}
@ -142,6 +143,7 @@ void etcd::KeepAlive::Check() {
void etcd::KeepAlive::refresh()
{
std::lock_guard<std::mutex> scope_lock(mutex_for_refresh_);
if (!continue_next.load()) {
return;
}