How to get the relationship between the watcher and watcher_id for one key multi watcher.
dongzl opened this issue · comments
//! Watch example
use etcd_client::*;
#[tokio::main]
async fn main() -> Result<(), Error> {
let mut client = Client::connect(["localhost:2379"], None).await?;
let (mut watcher, mut stream) = client.watch("foo", None).await?;
watcher.watch("foo", None).await?;
watcher.watch("foo", None).await?;
while let Some(resp) = stream.message().await? {
println!("[{}] receive watch response", resp.watch_id());
for event in resp.events() {
println!("event type: {:?}", event.event_type());
if let Some(kv) = event.kv() {
println!("kv: {{{}: {}}}", kv.key_str()?, kv.value_str()?);
}
if EventType::Delete == event.event_type() {
watcher.cancel_by_id(resp.watch_id()).await?;
}
}
println!();
}
Ok(())
}
I want to register multi watcher for one key at different scenes, so I want to get the watcher_id
fro every watcher registered and build the relationship between the scenes and watcher_id
, because I need cancel the watcher according to the scenes.
According to the example code, I only get the watcher_id
from the stream.message()
.
So how to get the relationship between the watcher and watcher_id for one key multi watcher?
You can use WatchOptions::with_watch_id to specify watch_id
by yourself.
You can use WatchOptions::with_watch_id to specify
watch_id
by yourself.
Hi @davidli2010 Thanks for your reply. I found the with_watch_id
method and try to use it.
But I found if use this method, I must guarantee to be globally unique for watch_id
myself. if not, maybe conflict.
If the distributed system, I should depend on third-party libraries.
Is there a better way to guarantee global uniqueness for watch_id
? Thanks.
Perhaps a node ID plus a monotonically increasing sequence within a node would do the trick.