19 releases (5 breaking)

0.6.2 Jan 17, 2024
0.6.1 Sep 1, 2023
0.6.0 Aug 31, 2023
0.4.0 Jul 27, 2023
0.1.3 Feb 15, 2022

#107 in Asynchronous

Download history 20150/week @ 2023-10-26 19410/week @ 2023-11-02 19498/week @ 2023-11-09 22400/week @ 2023-11-16 13229/week @ 2023-11-23 45643/week @ 2023-11-30 32827/week @ 2023-12-07 29195/week @ 2023-12-14 10444/week @ 2023-12-21 16077/week @ 2023-12-28 30769/week @ 2024-01-04 34127/week @ 2024-01-11 37400/week @ 2024-01-18 38361/week @ 2024-01-25 17255/week @ 2024-02-01 4441/week @ 2024-02-08

106,422 downloads per month
Used in 2 crates

MIT license

235KB
5.5K SLoC

ZooKeeper client in async rust

crates.io docs.rs github-ci mit-license codecov

ZooKeeper client writes in async rust.

Features

  • No callbacks.
  • No catch-all watcher.
  • StateWatcher trackes session state updates.
  • OneshotWatcher tracks oneshot ZooKeeper node event.
  • PersistentWatcher tracks persistent and recursive persistent ZooKeeper node events.
  • No event type XyzWatchRemoved as there is no way to receive such event after watchers dropped.
  • Cloneable Client and Client::chroot enables session sharing cross multiple different rooted clients.

Examples

Basics

use zookeeper_client as zk;

let path = "/abc";
let data = "path_data".as_bytes().to_vec();
let child_path = "/abc/efg";
let child_data = "child_path_data".as_bytes().to_vec();
let create_options = zk::CreateMode::Persistent.with_acls(zk::Acls::anyone_all());

let cluster = "localhost:2181";
let client = zk::Client::connect(cluster).await.unwrap();
let (_, stat_watcher) = client.check_and_watch_stat(path).await.unwrap();

let (stat, _) = client.create(path, &data, &create_options).await.unwrap();
assert_eq!((data.clone(), stat), client.get_data(path).await.unwrap());

let event = stat_watcher.changed().await;
assert_eq!(event.event_type, zk::EventType::NodeCreated);
assert_eq!(event.path, path);

let path_client = client.clone().chroot(path).unwrap();
assert_eq!((data, stat), path_client.get_data("/").await.unwrap());

let (_, _, child_watcher) = client.get_and_watch_children(path).await.unwrap();

let (child_stat, _) = client.create(child_path, &child_data, &create_options).await.unwrap();

let child_event = child_watcher.changed().await;
assert_eq!(child_event.event_type, zk::EventType::NodeChildrenChanged);
assert_eq!(child_event.path, path);

let relative_child_path = child_path.strip_prefix(path).unwrap();
assert_eq!((child_data.clone(), child_stat), path_client.get_data(relative_child_path).await.unwrap());

let (_, _, event_watcher) = client.get_and_watch_data("/").await.unwrap();
drop(client);
drop(path_client);

let session_event = event_watcher.changed().await;
assert_eq!(session_event.event_type, zk::EventType::Session);
assert_eq!(session_event.session_state, zk::SessionState::Closed);

Recipes

use zookeeper_client as zk;

let cluster = "localhost:2181";
let client = zk::Client::connect(cluster).await.unwrap();

let prefix = zk::LockPrefix::new_curator("/app/locks", "latch-").unwrap();
let options = zk::LockOptions::new(zk::Acls::anyone_all())
    .with_ancestor_options(zk::CreateMode::Persistent.with_acls(zk::Acls::anyone_all()))
    .unwrap();
let latch = client.lock(prefix, b"", options).await.unwrap();
latch.create("/app/data", b"data", &zk::CreateMode::Ephemeral.with_acls(zk::Acls::anyone_all())).await.unwrap();

For more examples, see zookeeper.rs.

TODO

  • Sasl authentication

License

The MIT License (MIT). See LICENSE for the full license text.

References

Dependencies

~7–14MB
~148K SLoC