mytheclipse_queue/
in_memory.rs1use 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#[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 let tq = self.get_topic(&job.topic).await;
120 let retried = Job {
124 attempt: job.attempt + 1,
125 delivered_at: Some(std::time::SystemTime::now()),
126 ..job.clone()
127 };
128 tq.jobs.lock().await.push(retried);
129 tq.notify.notify_one();
130 }
131 Ok(())
132 }
133
134 async fn dlq_move(&self, topic: &str, job: Job) -> Result<(), QueueError> {
135 let dlq_topic = format!("dlq:{topic}");
136 let tq = self.get_topic(&dlq_topic).await;
137 tq.jobs.lock().await.push(job);
138 tq.notify.notify_one();
139 Ok(())
140 }
141
142 async fn len(&self, topic: &str) -> Result<u64, QueueError> {
143 let tq = self.get_topic(topic).await;
144 let guard = tq.jobs.lock().await;
145 Ok(guard.len() as u64)
146 }
147}
148
149#[cfg(test)]
150mod tests {
151 use super::*;
152
153 #[tokio::test]
154 async fn enqueue_dequeue_roundtrip() {
155 let q = InMemoryQueue::new();
156 q.enqueue("test", b"hello".to_vec()).await.unwrap();
157 let job = q
158 .dequeue("test", Duration::from_millis(500))
159 .await
160 .unwrap()
161 .unwrap();
162 assert_eq!(job.payload, b"hello");
163 assert_eq!(job.topic, "test");
164 }
165
166 #[tokio::test]
167 async fn empty_returns_none() {
168 let q = InMemoryQueue::new();
169 let result = q.dequeue("none", Duration::from_millis(50)).await.unwrap();
170 assert!(result.is_none());
171 }
172
173 #[tokio::test]
174 async fn len_tracks_jobs() {
175 let q = InMemoryQueue::new();
176 q.enqueue("t", vec![1]).await.unwrap();
177 q.enqueue("t", vec![2]).await.unwrap();
178 assert_eq!(q.len("t").await.unwrap(), 2);
179 }
180
181 #[tokio::test]
182 async fn requeue_advances_attempt_on_the_same_job() {
183 let q = InMemoryQueue::new();
184 q.enqueue("t", vec![1]).await.unwrap();
185 let job = q
186 .dequeue("t", Duration::from_millis(50))
187 .await
188 .unwrap()
189 .unwrap();
190 assert_eq!(job.attempt, 0);
191
192 q.nack(&job, true).await.unwrap();
193 let again = q
194 .dequeue("t", Duration::from_millis(50))
195 .await
196 .unwrap()
197 .unwrap();
198 assert_eq!(again.id, job.id, "retry must keep the job identity");
199 assert_eq!(again.attempt, 1, "retry must advance the attempt counter");
200 }
201}