1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
pub mod local;
pub mod redis;

use std::ops::Deref;

use async_trait::async_trait;
use auto_impl::auto_impl;

pub type Result<T> = anyhow::Result<T>;

#[async_trait]
#[auto_impl(&, &mut, Box, Arc)]
pub trait MutexProvider<T, K>: Send + Sync {
    type Mutex: Send + Mutex<T>;
    async fn get(&self, key: K) -> Result<Self::Mutex>
    where
        K: 'async_trait;
}

pub trait GuardLt<'a, T> {
    type Guard: Guard<T>;
}

#[async_trait]
pub trait Mutex<T>: Send + Sync {
    type Guard: for<'a> GuardLt<'a, T>;
    async fn lock<'s>(&'s self) -> Result<<Self::Guard as GuardLt<'s, T>>::Guard>;
}

pub trait DerefLt<'a, T> {
    type Deref: Deref<Target = Option<T>>;
}

#[async_trait]
pub trait Guard<T>: Send + Sync {
    type D: for<'a> DerefLt<'a, T>;
    async fn store(&mut self, data: T) -> Result<()>;
    async fn load<'s>(&'s self) -> Result<<Self::D as DerefLt<'s, T>>::Deref>;
    async fn clear(&mut self) -> Result<()>;
}

pub struct Empty;

#[cfg(test)]
pub mod spec {
    use std::{
        sync::{atomic::AtomicI32, Arc},
        time::Duration,
    };

    use tokio::spawn;

    use crate::Empty;

    use super::{Guard, Mutex, MutexProvider};

    pub async fn check_empty(m: impl MutexProvider<Empty, &str> + 'static) {
        let count = Arc::new(AtomicI32::new(0));
        let m = Arc::new(m);
        let lock_key = "my_lock";
        let mut handles = vec![];
        for _ in 0..100 {
            let count = count.clone();
            let m = m.clone();
            handles.push(spawn(async move {
                let mutex = m.get(lock_key).await.unwrap();
                let _guard = mutex.lock().await.unwrap();
                let copy = count.load(std::sync::atomic::Ordering::Relaxed);
                tokio::time::sleep(Duration::from_millis(1)).await;
                count.store(copy + 1, std::sync::atomic::Ordering::Relaxed);
            }));
        }
        for handle in handles {
            handle.await.unwrap();
        }
        assert_eq!(100, count.load(std::sync::atomic::Ordering::Relaxed));
    }

    pub async fn check_val(m: impl MutexProvider<u64, &str> + 'static) {
        let m = Arc::new(m);
        let lock_key = "my_counter_lock";
        let mut handles = vec![];
        for _ in 0..100 {
            let m = m.clone();
            handles.push(spawn(async move {
                let mutex = m.get(lock_key).await.unwrap();
                let mut guard = mutex.lock().await.unwrap();
                let val = {
                    let val = guard.load().await.unwrap();
                    val.clone()
                };
                if let Some(val) = val {
                    guard.store(val + 1).await.unwrap();
                } else {
                    guard.store(1).await.unwrap();
                }
            }));
        }
        for handle in handles {
            handle.await.unwrap();
        }
        let mutex = m.get(lock_key).await.unwrap();
        let guard = mutex.lock().await.unwrap();
        assert_eq!(
            100,
            *guard
                .load()
                .await
                .unwrap()
                .as_ref()
                .expect("no value found")
        );
    }
}