ranvier_runtime/
distributed.rs1use async_trait::async_trait;
2use std::fmt;
3
4#[derive(Debug)]
6pub enum DistributedError {
7 StoreError(String),
8 LockError(String),
9 NotFound(String),
10}
11
12impl fmt::Display for DistributedError {
13 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14 match self {
15 Self::StoreError(e) => write!(f, "Store error: {}", e),
16 Self::LockError(e) => write!(f, "Lock error: {}", e),
17 Self::NotFound(e) => write!(f, "Not found: {}", e),
18 }
19 }
20}
21
22impl std::error::Error for DistributedError {}
23
24#[async_trait]
26pub trait DistributedStore: Send + Sync {
27 async fn get(&self, domain: &str, key: &str) -> Result<Option<Vec<u8>>, DistributedError>;
29
30 async fn set(
32 &self,
33 domain: &str,
34 key: &str,
35 value: &[u8],
36 ttl_sec: Option<u64>,
37 ) -> Result<(), DistributedError>;
38
39 async fn delete(&self, domain: &str, key: &str) -> Result<(), DistributedError>;
41}
42
43#[derive(Debug, Clone)]
45pub struct LockOptions {
46 pub ttl_ms: u64,
48 pub retry_count: u32,
50 pub retry_delay_ms: u64,
52}
53
54impl Default for LockOptions {
55 fn default() -> Self {
56 Self {
57 ttl_ms: 10_000,
58 retry_count: 5,
59 retry_delay_ms: 200,
60 }
61 }
62}
63
64#[derive(Debug, Clone)]
66pub struct Guard {
67 pub resource_key: String,
69 pub token: String,
71}
72
73#[async_trait]
75pub trait DistributedLock: Send + Sync {
76 async fn acquire(
78 &self,
79 resource_key: &str,
80 options: LockOptions,
81 ) -> Result<Guard, DistributedError>;
82
83 async fn release(&self, guard: Guard) -> Result<(), DistributedError>;
85
86 async fn extend(&self, guard: &Guard, additional_ttl_ms: u64) -> Result<(), DistributedError>;
88}