Watcher: add constructors to accept the wait callback.

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2022-09-29 14:57:44 +08:00
parent 1108d986a7
commit 9e519ce18b
3 changed files with 316 additions and 60 deletions

View File

@ -6,7 +6,6 @@
#include <string>
#include <thread>
#include "etcd/SyncClient.hpp"
#include "etcd/Response.hpp"
namespace etcd
@ -18,9 +17,11 @@ namespace etcd
{
public:
Watcher(Client const &client, std::string const & key,
std::function<void(Response)> callback, bool recursive=false);
std::function<void(Response)> callback,
bool recursive=false);
Watcher(SyncClient const &client, std::string const & key,
std::function<void(Response)> callback, bool recursive=false);
std::function<void(Response)> callback,
bool recursive=false);
Watcher(Client const &client, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback);
@ -28,9 +29,11 @@ namespace etcd
std::string const &range_end,
std::function<void(Response)> callback);
Watcher(Client const &client, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive=false);
std::function<void(Response)> callback,
bool recursive=false);
Watcher(SyncClient const &client, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive=false);
std::function<void(Response)> callback,
bool recursive=false);
Watcher(Client const &client, std::string const & key,
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback);
@ -38,19 +41,22 @@ namespace etcd
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback);
Watcher(std::string const & address, std::string const & key,
std::function<void(Response)> callback, bool recursive=false);
std::function<void(Response)> callback,
bool recursive=false);
Watcher(std::string const & address, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback);
Watcher(std::string const & address, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive=false);
std::function<void(Response)> callback,
bool recursive=false);
Watcher(std::string const & address, std::string const & key,
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback);
Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key,
std::function<void(Response)> callback, bool recursive=false,
std::function<void(Response)> callback,
bool recursive=false,
int const auth_token_ttl = 300);
Watcher(std::string const & address,
std::string const & username, std::string const & password,
@ -60,7 +66,8 @@ namespace etcd
Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive=false,
std::function<void(Response)> callback,
bool recursive=false,
int const auth_token_ttl = 300);
Watcher(std::string const & address,
std::string const & username, std::string const & password,
@ -82,6 +89,98 @@ namespace etcd
std::function<void(Response)> callback,
std::string const & target_name_override = "");
Watcher(Client const &client, std::string const & key,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive=false);
Watcher(SyncClient const &client, std::string const & key,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive=false);
Watcher(Client const &client, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback);
Watcher(SyncClient const &client, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback);
Watcher(Client const &client, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive=false);
Watcher(SyncClient const &client, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive=false);
Watcher(Client const &client, std::string const & key,
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback);
Watcher(SyncClient const &client, std::string const & key,
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback);
Watcher(std::string const & address, std::string const & key,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive=false);
Watcher(std::string const & address, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback);
Watcher(std::string const & address, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive=false);
Watcher(std::string const & address, std::string const & key,
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback);
Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive=false,
int const auth_token_ttl = 300);
Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key, std::string const &range_end,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
int const auth_token_ttl = 300);
Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive=false,
int const auth_token_ttl = 300);
Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key, std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
int const auth_token_ttl = 300);
Watcher(std::string const & address,
std::string const & ca,
std::string const & cert,
std::string const & privkey,
std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive=false,
std::string const & target_name_override = "");
Watcher(std::string const & address,
std::string const & ca,
std::string const & cert,
std::string const & privkey,
std::string const & key, std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
std::string const & target_name_override = "");
Watcher(Watcher const &) = delete;
Watcher(Watcher &&) = delete;

View File

@ -663,7 +663,36 @@ etcd::SyncClient* etcd::Client::sync_client() const {
// watchers from the async client
etcd::Watcher::Watcher(Client const &client, std::string const & key,
std::function<void(Response)> callback, bool recursive):
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive):
Watcher(*client.sync_client(), key, callback, wait_callback, recursive) {
}
etcd::Watcher::Watcher(Client const &client, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback):
Watcher(*client.sync_client(), key, range_end, callback, wait_callback) {
}
etcd::Watcher::Watcher(Client const &client, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive):
Watcher(*client.sync_client(), key, fromIndex, callback, wait_callback, recursive) {
}
etcd::Watcher::Watcher(Client const &client, std::string const & key,
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback):
Watcher(*client.sync_client(), key, range_end, fromIndex, callback, wait_callback) {
}
etcd::Watcher::Watcher(Client const &client, std::string const & key,
std::function<void(Response)> callback,
bool recursive):
Watcher(*client.sync_client(), key, callback, recursive) {
}
@ -674,7 +703,8 @@ etcd::Watcher::Watcher(Client const &client, std::string const & key,
}
etcd::Watcher::Watcher(Client const &client, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive):
std::function<void(Response)> callback,
bool recursive):
Watcher(*client.sync_client(), key, fromIndex, callback, recursive) {
}

View File

@ -1,4 +1,7 @@
#include "etcd/Watcher.hpp"
#include "etcd/SyncClient.hpp"
#include "etcd/v3/AsyncWatchAction.hpp"
struct etcd::Watcher::EtcdServerStubs {
@ -13,19 +16,24 @@ void etcd::Watcher::EtcdServerStubsDeleter::operator()(etcd::Watcher::EtcdServer
}
etcd::Watcher::Watcher(SyncClient const &client, std::string const & key,
std::function<void(Response)> callback, bool recursive):
Watcher(client, key, -1, callback, recursive) {
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive):
Watcher(client, key, -1, callback, wait_callback, recursive) {
}
etcd::Watcher::Watcher(SyncClient const &client, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback):
Watcher(client, key, range_end, -1, callback) {
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback):
Watcher(client, key, range_end, -1, callback, wait_callback) {
}
etcd::Watcher::Watcher(SyncClient const &client, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive):
fromIndex(fromIndex), recursive(recursive) {
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive):
wait_callback(wait_callback), fromIndex(fromIndex), recursive(recursive) {
stubs.reset(new EtcdServerStubs{});
stubs->watchServiceStub = Watch::NewStub(client.channel);
doWatch(key, "", client.current_auth_token(), callback);
@ -33,41 +41,158 @@ etcd::Watcher::Watcher(SyncClient const &client, std::string const & key, int64_
etcd::Watcher::Watcher(SyncClient const &client, std::string const & key,
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback):
fromIndex(fromIndex), recursive(false) {
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback):
wait_callback(wait_callback), fromIndex(fromIndex), recursive(false) {
stubs.reset(new EtcdServerStubs{});
stubs->watchServiceStub = Watch::NewStub(client.channel);
doWatch(key, range_end, client.current_auth_token(), callback);
}
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::function<void(Response)> callback, bool recursive):
Watcher(address, key, -1, callback, recursive) {
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive):
Watcher(address, key, -1, callback, wait_callback, recursive) {
}
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::string const & range_end,
std::function<void(Response)> callback):
Watcher(address, key, range_end, -1, callback) {
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback):
Watcher(address, key, range_end, -1, callback, wait_callback) {
}
etcd::Watcher::Watcher(std::string const & address, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive):
Watcher(SyncClient(address), key, fromIndex, callback, recursive) {
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive):
Watcher(SyncClient(address), key, fromIndex, callback, wait_callback, recursive) {
}
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::string const & range_end, int64_t fromIndex,
std::function<void(Response)> callback):
Watcher(SyncClient(address), key, range_end, fromIndex, callback) {
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback):
Watcher(SyncClient(address), key, range_end, fromIndex, callback, wait_callback) {
}
etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key,
std::function<void(Response)> callback, bool recursive,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive,
int const auth_token_ttl):
Watcher(address, username, password, key, -1, callback, recursive, auth_token_ttl) {
Watcher(address, username, password, key, -1, callback, wait_callback, recursive, auth_token_ttl) {
}
etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key, std::string const & range_end,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
int const auth_token_ttl):
Watcher(address, username, password, key, range_end, -1, callback, wait_callback, auth_token_ttl) {
}
etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive,
int const auth_token_ttl):
Watcher(SyncClient(address, username, password, auth_token_ttl), key, fromIndex, callback, wait_callback, recursive) {
}
etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key, std::string const & range_end, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
int const auth_token_ttl):
Watcher(SyncClient(address, username, password, auth_token_ttl), key, range_end, fromIndex, callback, wait_callback) {
}
etcd::Watcher::Watcher(std::string const & address,
std::string const & ca,
std::string const & cert,
std::string const & privkey,
std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive,
std::string const & target_name_override):
Watcher(SyncClient(address, ca, cert, privkey, target_name_override), key, fromIndex, callback, wait_callback, recursive) {
}
etcd::Watcher::Watcher(std::string const & address,
std::string const & ca,
std::string const & cert,
std::string const & privkey,
std::string const & key, std::string const & range_end, int64_t fromIndex,
std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
std::string const & target_name_override):
Watcher(SyncClient(address, ca, cert, privkey, target_name_override), key, range_end, fromIndex, callback, wait_callback) {
}
etcd::Watcher::Watcher(SyncClient const &client, std::string const & key,
std::function<void(Response)> callback,
bool recursive):
Watcher(client, key, callback, nullptr, recursive) {
}
etcd::Watcher::Watcher(SyncClient const &client, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback):
Watcher(client, key, range_end, callback, nullptr) {
}
etcd::Watcher::Watcher(SyncClient const &client, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
bool recursive):
Watcher(client, key, fromIndex, callback, nullptr, recursive) {
}
etcd::Watcher::Watcher(SyncClient const &client, std::string const & key,
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback):
Watcher(client, key, range_end, fromIndex, callback, nullptr) {
}
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::function<void(Response)> callback,
bool recursive):
Watcher(address, key, callback, nullptr, recursive) {
}
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback):
Watcher(address, key, range_end, callback, nullptr) {
}
etcd::Watcher::Watcher(std::string const & address, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback,
bool recursive):
Watcher(address, key, fromIndex, callback, nullptr, recursive) {
}
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback):
Watcher(address, key, range_end, fromIndex, callback, nullptr) {
}
etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key,
std::function<void(Response)> callback,
bool recursive,
int const auth_token_ttl):
Watcher(address, username, password, key, callback, nullptr, recursive, auth_token_ttl) {
}
etcd::Watcher::Watcher(std::string const & address,
@ -75,15 +200,16 @@ etcd::Watcher::Watcher(std::string const & address,
std::string const & key, std::string const &range_end,
std::function<void(Response)> callback,
int const auth_token_ttl):
Watcher(address, username, password, key, range_end, -1, callback, auth_token_ttl) {
Watcher(address, username, password, key, range_end, callback, nullptr, auth_token_ttl) {
}
etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password,
std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive,
std::function<void(Response)> callback,
bool recursive,
int const auth_token_ttl):
Watcher(SyncClient(address, username, password, auth_token_ttl), key, fromIndex, callback, recursive) {
Watcher(address, username, password, key, fromIndex, callback, nullptr, recursive, auth_token_ttl) {
}
etcd::Watcher::Watcher(std::string const & address,
@ -91,7 +217,7 @@ etcd::Watcher::Watcher(std::string const & address,
std::string const & key, std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback,
int const auth_token_ttl):
Watcher(SyncClient(address, username, password, auth_token_ttl), key, range_end, fromIndex, callback) {
Watcher(address, username, password, key, range_end, fromIndex, callback, nullptr, auth_token_ttl) {
}
etcd::Watcher::Watcher(std::string const & address,
@ -99,9 +225,10 @@ etcd::Watcher::Watcher(std::string const & address,
std::string const & cert,
std::string const & privkey,
std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive,
std::function<void(Response)> callback,
bool recursive,
std::string const & target_name_override):
Watcher(SyncClient(address, ca, cert, privkey, target_name_override), key, fromIndex, callback, recursive) {
Watcher(address, ca, cert, privkey, key, fromIndex, callback, nullptr, recursive, target_name_override) {
}
etcd::Watcher::Watcher(std::string const & address,
@ -111,7 +238,7 @@ etcd::Watcher::Watcher(std::string const & address,
std::string const & key, std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback,
std::string const & target_name_override):
Watcher(SyncClient(address, ca, cert, privkey, target_name_override), key, range_end, fromIndex, callback) {
Watcher(address, ca, cert, privkey, key, range_end, fromIndex, callback, nullptr, target_name_override) {
}
etcd::Watcher::~Watcher()