Watcher: add constructors to accept the wait callback. (#169)

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2022-09-29 15:18:15 +08:00 committed by GitHub
parent 1108d986a7
commit c81f3fb211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 316 additions and 60 deletions

View File

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

View File

@ -663,7 +663,36 @@ etcd::SyncClient* etcd::Client::sync_client() const {
// watchers from the async client // watchers from the async client
etcd::Watcher::Watcher(Client const &client, std::string const & key, 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) { 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, 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) { Watcher(*client.sync_client(), key, fromIndex, callback, recursive) {
} }

View File

@ -1,4 +1,7 @@
#include "etcd/Watcher.hpp" #include "etcd/Watcher.hpp"
#include "etcd/SyncClient.hpp"
#include "etcd/v3/AsyncWatchAction.hpp" #include "etcd/v3/AsyncWatchAction.hpp"
struct etcd::Watcher::EtcdServerStubs { 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, etcd::Watcher::Watcher(SyncClient const &client, std::string const & key,
std::function<void(Response)> callback, bool recursive): std::function<void(Response)> callback,
Watcher(client, key, -1, callback, recursive) { 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, etcd::Watcher::Watcher(SyncClient const &client, std::string const & key,
std::string const &range_end, std::string const &range_end,
std::function<void(Response)> callback): std::function<void(Response)> callback,
Watcher(client, key, range_end, -1, 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, etcd::Watcher::Watcher(SyncClient const &client, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive): std::function<void(Response)> callback,
fromIndex(fromIndex), recursive(recursive) { std::function<void(bool)> wait_callback,
bool recursive):
wait_callback(wait_callback), fromIndex(fromIndex), recursive(recursive) {
stubs.reset(new EtcdServerStubs{}); stubs.reset(new EtcdServerStubs{});
stubs->watchServiceStub = Watch::NewStub(client.channel); stubs->watchServiceStub = Watch::NewStub(client.channel);
doWatch(key, "", client.current_auth_token(), callback); doWatch(key, "", client.current_auth_token(), callback);
@ -33,65 +41,78 @@ etcd::Watcher::Watcher(SyncClient const &client, std::string const & key, int64_
etcd::Watcher::Watcher(SyncClient const &client, std::string const & key, etcd::Watcher::Watcher(SyncClient const &client, std::string const & key,
std::string const &range_end, int64_t fromIndex, std::string const &range_end, int64_t fromIndex,
std::function<void(Response)> callback): std::function<void(Response)> callback,
fromIndex(fromIndex), recursive(false) { std::function<void(bool)> wait_callback):
wait_callback(wait_callback), fromIndex(fromIndex), recursive(false) {
stubs.reset(new EtcdServerStubs{}); stubs.reset(new EtcdServerStubs{});
stubs->watchServiceStub = Watch::NewStub(client.channel); stubs->watchServiceStub = Watch::NewStub(client.channel);
doWatch(key, range_end, client.current_auth_token(), callback); doWatch(key, range_end, client.current_auth_token(), callback);
} }
etcd::Watcher::Watcher(std::string const & address, std::string const & key, etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::function<void(Response)> callback, bool recursive): std::function<void(Response)> callback,
Watcher(address, key, -1, callback, recursive) { 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, etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::string const & range_end, std::string const & range_end,
std::function<void(Response)> callback): std::function<void(Response)> callback,
Watcher(address, key, range_end, -1, 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, etcd::Watcher::Watcher(std::string const & address, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive): std::function<void(Response)> callback,
Watcher(SyncClient(address), key, fromIndex, callback, recursive) { 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, etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::string const & range_end, int64_t fromIndex, std::string const & range_end, int64_t fromIndex,
std::function<void(Response)> callback): std::function<void(Response)> callback,
Watcher(SyncClient(address), key, range_end, fromIndex, callback) { std::function<void(bool)> wait_callback):
Watcher(SyncClient(address), key, range_end, fromIndex, callback, wait_callback) {
} }
etcd::Watcher::Watcher(std::string const & address, etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password, std::string const & username, std::string const & password,
std::string const & key, 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): 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, etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password, std::string const & username, std::string const & password,
std::string const & key, std::string const & range_end, std::string const & key, std::string const & range_end,
std::function<void(Response)> callback, std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
int const auth_token_ttl): int const auth_token_ttl):
Watcher(address, username, password, key, range_end, -1, callback, auth_token_ttl) { Watcher(address, username, password, key, range_end, -1, callback, wait_callback, auth_token_ttl) {
} }
etcd::Watcher::Watcher(std::string const & address, etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password, std::string const & username, std::string const & password,
std::string const & key, int64_t fromIndex, std::string const & key, int64_t fromIndex,
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): int const auth_token_ttl):
Watcher(SyncClient(address, username, password, auth_token_ttl), key, fromIndex, callback, recursive) { Watcher(SyncClient(address, username, password, auth_token_ttl), key, fromIndex, callback, wait_callback, recursive) {
} }
etcd::Watcher::Watcher(std::string const & address, etcd::Watcher::Watcher(std::string const & address,
std::string const & username, std::string const & password, std::string const & username, std::string const & password,
std::string const & key, std::string const & range_end, int64_t fromIndex, std::string const & key, std::string const & range_end, int64_t fromIndex,
std::function<void(Response)> callback, std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
int const auth_token_ttl): int const auth_token_ttl):
Watcher(SyncClient(address, username, password, auth_token_ttl), key, range_end, fromIndex, callback) { Watcher(SyncClient(address, username, password, auth_token_ttl), key, range_end, fromIndex, callback, wait_callback) {
} }
etcd::Watcher::Watcher(std::string const & address, etcd::Watcher::Watcher(std::string const & address,
@ -99,9 +120,11 @@ etcd::Watcher::Watcher(std::string const & address,
std::string const & cert, std::string const & cert,
std::string const & privkey, std::string const & privkey,
std::string const & key, int64_t fromIndex, std::string const & key, int64_t fromIndex,
std::function<void(Response)> callback, bool recursive, std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
bool recursive,
std::string const & target_name_override): std::string const & target_name_override):
Watcher(SyncClient(address, ca, cert, privkey, target_name_override), key, fromIndex, callback, recursive) { Watcher(SyncClient(address, ca, cert, privkey, target_name_override), key, fromIndex, callback, wait_callback, recursive) {
} }
etcd::Watcher::Watcher(std::string const & address, etcd::Watcher::Watcher(std::string const & address,
@ -110,8 +133,112 @@ etcd::Watcher::Watcher(std::string const & address,
std::string const & privkey, std::string const & privkey,
std::string const & key, std::string const & range_end, int64_t fromIndex, std::string const & key, std::string const & range_end, int64_t fromIndex,
std::function<void(Response)> callback, std::function<void(Response)> callback,
std::function<void(bool)> wait_callback,
std::string const & target_name_override): std::string const & target_name_override):
Watcher(SyncClient(address, ca, cert, privkey, target_name_override), key, range_end, fromIndex, callback) { 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,
std::string const & username, std::string const & password,
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, 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,
int const auth_token_ttl):
Watcher(address, username, password, key, fromIndex, callback, nullptr, 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, int64_t fromIndex,
std::function<void(Response)> callback,
int const auth_token_ttl):
Watcher(address, username, password, key, range_end, fromIndex, callback, nullptr, auth_token_ttl) {
}
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,
bool recursive,
std::string const & target_name_override):
Watcher(address, ca, cert, privkey, key, fromIndex, callback, nullptr, recursive, target_name_override) {
}
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::string const & target_name_override):
Watcher(address, ca, cert, privkey, key, range_end, fromIndex, callback, nullptr, target_name_override) {
} }
etcd::Watcher::~Watcher() etcd::Watcher::~Watcher()