Watch on range by specifying `rang_end`. (#52)

Follow-up work on #51, and fixes #50.

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2021-04-02 01:34:58 +08:00 committed by GitHub
parent 1b24751b9d
commit 27e6e2ac11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 1 deletions

View File

@ -13,20 +13,40 @@ 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(Client const &client, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback);
Watcher(Client const &client, std::string const & key, int fromIndex, Watcher(Client const &client, std::string const & key, int 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, int fromIndex,
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,
std::string const &range_end,
std::function<void(Response)> callback);
Watcher(std::string const & address, std::string const & key, int fromIndex, Watcher(std::string const & address, std::string const & key, int 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, int fromIndex,
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);
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);
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, int fromIndex, std::string const & key, int 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 & username, std::string const & password,
std::string const & key, std::string const &range_end, int fromIndex,
std::function<void(Response)> callback);
Watcher(Watcher const &) = delete; Watcher(Watcher const &) = delete;
Watcher(Watcher &&) = delete; Watcher(Watcher &&) = delete;
@ -55,6 +75,7 @@ namespace etcd
protected: protected:
void doWatch(std::string const & key, void doWatch(std::string const & key,
std::string const & range_end,
std::string const & auth_token, std::string const & auth_token,
std::function<void(Response)> callback); std::function<void(Response)> callback);

View File

@ -17,12 +17,27 @@ etcd::Watcher::Watcher(Client const &client, std::string const & key,
Watcher(client, key, -1, callback, recursive) { Watcher(client, key, -1, callback, recursive) {
} }
etcd::Watcher::Watcher(Client const &client, std::string const & key,
std::string const &range_end,
std::function<void(Response)> callback):
Watcher(client, key, range_end, -1, callback) {
}
etcd::Watcher::Watcher(Client const &client, std::string const & key, int fromIndex, etcd::Watcher::Watcher(Client const &client, std::string const & key, int fromIndex,
std::function<void(Response)> callback, bool recursive): std::function<void(Response)> callback, bool recursive):
fromIndex(fromIndex), recursive(recursive) { 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.auth_token, callback); doWatch(key, "", client.auth_token, callback);
}
etcd::Watcher::Watcher(Client const &client, std::string const & key,
std::string const &range_end, int fromIndex,
std::function<void(Response)> callback):
fromIndex(fromIndex), recursive(false) {
stubs.reset(new EtcdServerStubs{});
stubs->watchServiceStub = Watch::NewStub(client.channel);
doWatch(key, range_end, client.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,
@ -30,11 +45,23 @@ etcd::Watcher::Watcher(std::string const & address, std::string const & key,
Watcher(address, key, -1, callback, recursive) { Watcher(address, key, -1, 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) {
}
etcd::Watcher::Watcher(std::string const & address, std::string const & key, int fromIndex, etcd::Watcher::Watcher(std::string const & address, std::string const & key, int fromIndex,
std::function<void(Response)> callback, bool recursive): std::function<void(Response)> callback, bool recursive):
Watcher(Client(address), key, fromIndex, callback, recursive) { Watcher(Client(address), key, fromIndex, callback, recursive) {
} }
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
std::string const & range_end, int fromIndex,
std::function<void(Response)> callback):
Watcher(Client(address), key, range_end, fromIndex, 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,
@ -42,6 +69,13 @@ etcd::Watcher::Watcher(std::string const & address,
Watcher(address, username, password, key, -1, callback, recursive) { Watcher(address, username, password, key, -1, 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,
std::function<void(Response)> callback):
Watcher(address, username, password, key, range_end, -1, 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, int fromIndex, std::string const & key, int fromIndex,
@ -49,6 +83,13 @@ etcd::Watcher::Watcher(std::string const & address,
Watcher(Client(address, username, password), key, fromIndex, callback, recursive) { Watcher(Client(address, username, password), key, fromIndex, 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, int fromIndex,
std::function<void(Response)> callback):
Watcher(Client(address, username, password), key, range_end, fromIndex, callback) {
}
etcd::Watcher::~Watcher() etcd::Watcher::~Watcher()
{ {
stubs->call->CancelWatch(); stubs->call->CancelWatch();
@ -76,12 +117,14 @@ void etcd::Watcher::Cancel()
} }
void etcd::Watcher::doWatch(std::string const & key, void etcd::Watcher::doWatch(std::string const & key,
std::string const & range_end,
std::string const & auth_token, std::string const & auth_token,
std::function<void(Response)> callback) std::function<void(Response)> callback)
{ {
etcdv3::ActionParameters params; etcdv3::ActionParameters params;
params.auth_token.assign(auth_token); params.auth_token.assign(auth_token);
params.key.assign(key); params.key.assign(key);
params.range_end.assign(range_end);
if (fromIndex >= 0) { if (fromIndex >= 0) {
params.revision = fromIndex; params.revision = fromIndex;
} }

View File

@ -46,7 +46,29 @@ TEST_CASE("create watcher with cancel")
CHECK(4 == watcher_called); CHECK(4 == watcher_called);
etcd.rmdir("/test", true); etcd.rmdir("/test", true);
}
TEST_CASE("create watcher on ranges with cancel")
{
etcd::SyncClient etcd(etcd_uri);
etcd.rmdir("/test", true);
watcher_called = 0;
etcd::Watcher watcher(etcd_uri, "/test/key1", "/test/key5", printResponse);
std::this_thread::sleep_for(std::chrono::seconds(3));
etcd.set("/test/key1", "42");
etcd.set("/test/key2", "43");
etcd.rm("/test/key1");
etcd.set("/test/key5", "44");
std::this_thread::sleep_for(std::chrono::seconds(3));
CHECK(3 == watcher_called);
watcher.Cancel();
etcd.set("/test/key3", "50");
etcd.set("/test/key4", "51");
std::this_thread::sleep_for(std::chrono::seconds(3));
CHECK(3 == watcher_called);
etcd.rmdir("/test", true);
} }
TEST_CASE("create watcher") TEST_CASE("create watcher")