Add tool to get sibling connections for a device.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-10-21 17:48:08 +00:00
parent 5b620a2c37
commit 312eb69450

View File

@@ -2,6 +2,7 @@ mod watch;
use std::{
collections::BTreeMap,
ops::Bound::Included,
sync::{Arc, Mutex as StdMutex},
};
@@ -13,7 +14,7 @@ use ruma::{
},
};
use tokio::sync::Mutex as TokioMutex;
use tuwunel_core::{Result, err, implement, is_equal_to};
use tuwunel_core::{Result, at, err, implement, is_equal_to, smallvec::SmallVec};
use tuwunel_database::Map;
pub struct Service {
@@ -241,16 +242,6 @@ pub fn drop_connection(&self, key: &ConnectionKey) {
.remove(key);
}
#[implement(Service)]
pub fn list_connections(&self) -> Vec<ConnectionKey> {
self.connections
.lock()
.expect("locked")
.keys()
.cloned()
.collect()
}
#[implement(Service)]
pub fn init_connection(&self, key: &ConnectionKey) -> ConnectionVal {
self.connections
@@ -262,6 +253,38 @@ pub fn init_connection(&self, key: &ConnectionKey) -> ConnectionVal {
.clone()
}
#[implement(Service)]
pub fn device_connections(
&self,
user_id: &UserId,
device_id: &DeviceId,
exclude: Option<&ConnectionId>,
) -> impl Iterator<Item = ConnectionVal> + Send {
type Siblings = SmallVec<[ConnectionVal; 4]>;
let key = into_connection_key(user_id, device_id, None::<ConnectionId>);
self.connections
.lock()
.expect("locked")
.range((Included(&key), Included(&key)))
.filter(|((_, _, id), _)| id.as_ref() != exclude)
.map(at!(1))
.cloned()
.collect::<Siblings>()
.into_iter()
}
#[implement(Service)]
pub fn list_connections(&self) -> Vec<ConnectionKey> {
self.connections
.lock()
.expect("locked")
.keys()
.cloned()
.collect()
}
#[implement(Service)]
pub fn find_connection(&self, key: &ConnectionKey) -> Result<ConnectionVal> {
self.connections