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:
parent
c7f17cdf0d
commit
8395e821fc
|
|
@ -5,6 +5,7 @@
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
|
@ -117,6 +118,9 @@ namespace etcd
|
||||||
|
|
||||||
int ttl;
|
int ttl;
|
||||||
int64_t lease_id;
|
int64_t lease_id;
|
||||||
|
|
||||||
|
// protect the initializing status of `timer`.
|
||||||
|
std::mutex mutex_for_refresh_;
|
||||||
std::atomic_bool continue_next;
|
std::atomic_bool continue_next;
|
||||||
|
|
||||||
// grpc timeout in `refresh()`
|
// grpc timeout in `refresh()`
|
||||||
|
|
|
||||||
|
|
@ -124,6 +124,7 @@ etcd::KeepAlive::~KeepAlive()
|
||||||
|
|
||||||
void etcd::KeepAlive::Cancel()
|
void etcd::KeepAlive::Cancel()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> scope_lock(mutex_for_refresh_);
|
||||||
if (!continue_next.exchange(false)) {
|
if (!continue_next.exchange(false)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -142,6 +143,7 @@ void etcd::KeepAlive::Check() {
|
||||||
|
|
||||||
void etcd::KeepAlive::refresh()
|
void etcd::KeepAlive::refresh()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> scope_lock(mutex_for_refresh_);
|
||||||
if (!continue_next.load()) {
|
if (!continue_next.load()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue