Skip to main content

mytheclipse_queue/
in_memory.rs

1//! In-process job queue using `tokio::sync::Mutex` + `Notify`.
2
3use std::sync::Arc;
4use std::time::Duration;
5
6use async_trait::async_trait;
7use tokio::sync::{Mutex, Notify};
8
9use crate::error::{JobError, QueueError};
10use crate::job::{Job, JobId};
11use crate::traits::Queue;
12
13struct TopicQueue {
14    jobs: Mutex<Vec<Job>>,
15    notify: Notify,
16}
17
18impl std::fmt::Debug for TopicQueue {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        f.debug_struct("TopicQueue")
21            .field(
22                "jobs_len",
23                &self.jobs.try_lock().map(|j| j.len()).unwrap_or(0),
24            )
25            .finish()
26    }
27}
28
29struct Inner {
30    topics: std::collections::HashMap<String, Arc<TopicQueue>>,
31}
32
33impl std::fmt::Debug for Inner {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        let keys: Vec<&String> = self.topics.keys().collect();
36        f.debug_struct("Inner").field("topics", &keys).finish()
37    }
38}
39
40/// An in-memory queue. Each topic is a shared mutex-protected vector + Notify.
41#[derive(Clone)]
42pub struct InMemoryQueue {
43    inner: Arc<Mutex<Inner>>,
44}
45
46impl std::fmt::Debug for InMemoryQueue {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        f.debug_struct("InMemoryQueue").finish()
49    }
50}
51
52impl Default for InMemoryQueue {
53    fn default() -> Self {
54        Self::new()
55    }
56}
57
58impl InMemoryQueue {
59    pub fn new() -> Self {
60        Self {
61            inner: Arc::new(Mutex::new(Inner {
62                topics: std::collections::HashMap::new(),
63            })),
64        }
65    }
66
67    async fn get_topic(&self, topic: &str) -> Arc<TopicQueue> {
68        let mut inner = self.inner.lock().await;
69        inner
70            .topics
71            .entry(topic.to_string())
72            .or_insert_with(|| {
73                Arc::new(TopicQueue {
74                    jobs: Mutex::new(Vec::new()),
75                    notify: Notify::new(),
76                })
77            })
78            .clone()
79    }
80}
81
82#[async_trait]
83impl Queue for InMemoryQueue {
84    async fn enqueue(&self, topic: &str, payload: Vec<u8>) -> Result<(), QueueError> {
85        let tq = self.get_topic(topic).await;
86        tq.jobs
87            .lock()
88            .await
89            .push(Job::new(JobId::generate(), topic, payload));
90        tq.notify.notify_one();
91        Ok(())
92    }
93
94    async fn dequeue(&self, topic: &str, timeout: Duration) -> Result<Option<Job>, QueueError> {
95        let tq = self.get_topic(topic).await;
96        let tq2 = Arc::clone(&tq);
97        loop {
98            if let Some(job) = tq.jobs.lock().await.pop() {
99                return Ok(Some(job));
100            }
101            tokio::select! {
102                _ = tq2.notify.notified() => {}
103                _ = tokio::time::sleep(timeout) => {
104                    if let Some(job) = tq.jobs.lock().await.pop() {
105                        return Ok(Some(job));
106                    }
107                    return Ok(None);
108                }
109            }
110        }
111    }
112
113    async fn ack(&self, _job: &Job) -> Result<(), JobError> {
114        Ok(())
115    }
116
117    async fn nack(&self, job: &Job, requeue: bool) -> Result<(), JobError> {
118        if requeue {
119            self.enqueue(&job.topic, job.payload.clone())
120                .await
121                .map_err(|_| JobError::AckFailed("requeue failed".into()))?;
122        }
123        Ok(())
124    }
125
126    async fn dlq_move(&self, topic: &str, job: Job) -> Result<(), QueueError> {
127        let dlq_topic = format!("dlq:{topic}");
128        let tq = self.get_topic(&dlq_topic).await;
129        tq.jobs.lock().await.push(job);
130        tq.notify.notify_one();
131        Ok(())
132    }
133
134    async fn len(&self, topic: &str) -> Result<u64, QueueError> {
135        let tq = self.get_topic(topic).await;
136        let guard = tq.jobs.lock().await;
137        Ok(guard.len() as u64)
138    }
139}
140
141#[cfg(test)]
142mod tests {
143    use super::*;
144
145    #[tokio::test]
146    async fn enqueue_dequeue_roundtrip() {
147        let q = InMemoryQueue::new();
148        q.enqueue("test", b"hello".to_vec()).await.unwrap();
149        let job = q
150            .dequeue("test", Duration::from_millis(500))
151            .await
152            .unwrap()
153            .unwrap();
154        assert_eq!(job.payload, b"hello");
155        assert_eq!(job.topic, "test");
156    }
157
158    #[tokio::test]
159    async fn empty_returns_none() {
160        let q = InMemoryQueue::new();
161        let result = q.dequeue("none", Duration::from_millis(50)).await.unwrap();
162        assert!(result.is_none());
163    }
164
165    #[tokio::test]
166    async fn len_tracks_jobs() {
167        let q = InMemoryQueue::new();
168        q.enqueue("t", vec![1]).await.unwrap();
169        q.enqueue("t", vec![2]).await.unwrap();
170        assert_eq!(q.len("t").await.unwrap(), 2);
171    }
172}