Document the `client.observe()` API.

Resolves #148.

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2022-10-24 14:53:10 +08:00
parent 2f15c45d4e
commit 30c880dd05
2 changed files with 21 additions and 4 deletions

View File

@ -863,14 +863,31 @@ pplx::task<Response> proclaim(std::string const &name, int64_t lease_id,
pplx::task<Response> leader(std::string const &name); pplx::task<Response> leader(std::string const &name);
std::unique_ptr<Observer> observe(std::string const &name, std::unique_ptr<Observer> observe(std::string const &name);
std::function<void(Response)> callback,
const bool once = false);
pplx::task<Response> resign(std::string const &name, int64_t lease_id, pplx::task<Response> resign(std::string const &name, int64_t lease_id,
std::string const &key, int64_t revision); std::string const &key, int64_t revision);
``` ```
The `Observer` returned by `observe()` can be use to monitor the changes of election ownership.
The observer stream will be canceled when been destructed.
```c++
std::unique_ptr<etcd::Observer> observer = etcd.observe("test");
// wait one change event, blocked execution
etcd::Response resp = observer->WaitOnce();
// wait many change events, blocked execution
for (size_t i = 0; i < ...; ++i) {
etcd::Response resp = observer->WaitOnce();
...
}
// cancel the observer
observer.reset(nullptr);
```
for more details, please refer to [etcd/Client.hpp](./etcd/Client.hpp). for more details, please refer to [etcd/Client.hpp](./etcd/Client.hpp).
### TODO ### TODO

View File

@ -642,7 +642,7 @@ namespace etcd
class Observer { class Observer {
public: public:
~Observer(); ~Observer();
// wait a at least one response from the observer. // wait at least *one* response from the observer.
Response WaitOnce(); Response WaitOnce();
private: private:
std::shared_ptr<etcdv3::AsyncObserveAction> action = nullptr; std::shared_ptr<etcdv3::AsyncObserveAction> action = nullptr;