kitsune2_api/
peer_meta_store.rs1use crate::{builder, config, BoxFut, K2Error, K2Result, Timestamp, Url};
2use bytes::Bytes;
3use futures::future::BoxFuture;
4use std::{collections::HashMap, sync::Arc};
5
6pub const KEY_PREFIX_ROOT: &str = "root";
8
9pub const META_KEY_UNRESPONSIVE: &str = "unresponsive";
11
12pub trait PeerMetaStore: 'static + Send + Sync + std::fmt::Debug {
16 fn put(
18 &self,
19 peer: Url,
20 key: String,
21 value: Bytes,
22 expiry: Option<Timestamp>,
23 ) -> BoxFuture<'_, K2Result<()>>;
24
25 fn get(
27 &self,
28 peer: Url,
29 key: String,
30 ) -> BoxFuture<'_, K2Result<Option<bytes::Bytes>>>;
31
32 fn get_all_by_key(
34 &self,
35 key: String,
36 ) -> BoxFuture<'_, K2Result<HashMap<Url, bytes::Bytes>>>;
37
38 fn set_unresponsive(
46 &self,
47 peer: Url,
48 expiry: Timestamp,
49 when: Timestamp,
50 ) -> BoxFuture<'_, K2Result<()>> {
51 Box::pin(async move {
52 self.put(
53 peer.clone(),
54 format!("{KEY_PREFIX_ROOT}:{META_KEY_UNRESPONSIVE}"),
55 serde_json::to_vec(&when).map_err(K2Error::other)?.into(),
56 Some(expiry),
57 )
58 .await?;
59 Ok(())
60 })
61 }
62
63 fn get_unresponsive(
66 &self,
67 peer: Url,
68 ) -> BoxFuture<'_, K2Result<Option<Timestamp>>> {
69 Box::pin(async move {
70 let maybe_value = self
71 .get(peer, format!("{KEY_PREFIX_ROOT}:{META_KEY_UNRESPONSIVE}"))
72 .await?;
73 match maybe_value {
74 None => Ok(None),
75 Some(value) => {
76 match serde_json::from_slice::<Timestamp>(&value) {
77 Ok(when) => Ok(Some(when)),
78 Err(err) => Err(K2Error::other(err)),
79 }
80 }
81 }
82 })
83 }
84
85 fn delete(&self, peer: Url, key: String) -> BoxFuture<'_, K2Result<()>>;
87}
88
89pub type DynPeerMetaStore = Arc<dyn PeerMetaStore>;
91
92pub trait PeerMetaStoreFactory:
94 'static + Send + Sync + std::fmt::Debug
95{
96 fn default_config(&self, config: &mut config::Config) -> K2Result<()>;
99
100 fn validate_config(&self, config: &config::Config) -> K2Result<()>;
102
103 fn create(
105 &self,
106 builder: Arc<builder::Builder>,
107 space_id: crate::SpaceId,
108 ) -> BoxFut<'static, K2Result<DynPeerMetaStore>>;
109}
110
111pub type DynPeerMetaStoreFactory = Arc<dyn PeerMetaStoreFactory>;