Optimize sync watchers and key serializations.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-07-25 20:23:00 +00:00
parent e09a2c0e0f
commit ce30f83052
2 changed files with 63 additions and 90 deletions

View File

@@ -6,10 +6,11 @@ use std::{
};
use futures::pin_mut;
use serde::Serialize;
use tokio::sync::watch::{Sender, channel};
use tuwunel_core::implement;
use crate::keyval::KeyBuf;
use crate::keyval::{KeyBuf, serialize_key};
type Watchers = Mutex<BTreeMap<KeyBuf, Sender<()>>>;
@@ -19,9 +20,18 @@ pub(super) struct Watch {
}
#[implement(super::Map)]
pub fn watch_raw_prefix<K>(&self, prefix: &K) -> impl Future<Output = ()> + Send + use<K>
pub fn watch_prefix<K>(&self, prefix: K) -> impl Future<Output = ()> + Send + '_
where
K: AsRef<[u8]> + ?Sized,
K: Serialize,
{
let prefix = serialize_key(prefix).expect("failed to serialize watch prefix key");
self.watch_raw_prefix(&prefix)
}
#[implement(super::Map)]
pub fn watch_raw_prefix<'a, K>(&self, prefix: &'a K) -> impl Future<Output = ()> + Send + use<K>
where
K: AsRef<[u8]> + ?Sized + 'a,
{
let rx = match self
.watch