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
115
116
117
118
119
use async_std::{
    sync::{Mutex, Receiver, RwLock, Sender},
    task,
};
use std::{fmt, mem, time::Duration};

pub mod builder;
struct Container<E, C> {
    /// Whether the timed task has been set
    clock: bool,
    /// Number of container elements
    len: usize,
    container: C,
    /// accumulator function
    accumulator: fn(&mut C, E) -> (),
}

/// Simple `BufferTrigger`
///
/// Set your own container to store in the current service
pub struct AsyncBufferTrigger<E, C>
where
    E: fmt::Debug + Sync + Send,
    C: fmt::Debug + Sync + Send,
{
    name: String,
    /// The default generation function of the container
    defalut_container: fn() -> C,
    container: RwLock<Container<E, C>>,
    /// The function executed after the trigger condition is met.
    consumer: fn(C) -> (),
    /// how many elements are exceeded
    max_len: usize,
    /// The maximum time to wait after an element is saved.
    interval: Option<Duration>,

    sender: Mutex<Sender<()>>,
    receiver: Mutex<Receiver<()>>,
}

impl<E, C> fmt::Debug for AsyncBufferTrigger<E, C>
where
    E: fmt::Debug + Sync + Send,
    C: fmt::Debug + Sync + Send,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "name {}", self.name)
    }
}

impl<E, C> AsyncBufferTrigger<E, C>
where
    E: fmt::Debug + Sync + Send,
    C: fmt::Debug + Sync + Send,
{
    pub async fn len(&self) -> usize {
        self.container.read().await.len
    }

    pub async fn push(&self, value: E) {
        {
            let mut c = self.container.write().await;
            (c.accumulator)(&mut c.container, value);
            c.len += 1;
            if let (false, Some(dur)) = (c.clock, self.interval) {
                c.clock = true;
                let sender = self.sender.lock().await.clone();
                let _ = task::spawn(async move {
                    task::sleep(dur).await;
                    sender.send(()).await
                });
            }
        }
        if self.len().await >= self.max_len {
            self.trigger().await
        }
    }

    pub async fn trigger(&self) {
        if !self.is_empty().await {
            let mut c = self.container.write().await;
            c.len = 0;
            let mut new_cahce = (self.defalut_container)();
            mem::swap(&mut new_cahce, &mut c.container);
            c.clock = false;
            (self.consumer)(new_cahce);
        }
    }

    pub async fn is_empty(&self) -> bool {
        self.len().await == 0
    }
}

impl<E, C> AsyncBufferTrigger<E, C>
where
    E: fmt::Debug + Sync + Send,
    C: fmt::Debug + Sync + Send,
{
    /// start clock trigger listener
    pub async fn listen_clock_trigger(&self) {
        log::info!("{:?} listen_clock_trigger", self);
        while self.receiver.lock().await.recv().await.is_ok() {
            if self.container.read().await.clock {
                self.trigger().await;
            }
        }
    }
}

impl<E, C> Drop for AsyncBufferTrigger<E, C>
where
    E: fmt::Debug + Sync + Send,
    C: fmt::Debug + Sync + Send,
{
    fn drop(&mut self) {
        let _ = self.trigger();
    }
}