mytheclipse_queue/
in_memory.rs1use std::collections::HashMap;
11use std::sync::{Arc, Mutex, RwLock};
12use std::time::Duration;
13
14use async_trait::async_trait;
15use tokio::sync::Notify;
16
17use crate::error::{JobError, QueueError};
18use crate::job::{Job, JobId};
19use crate::traits::Queue;
20
21struct TopicQueue {
22 jobs: Mutex<Vec<Job>>,
24 notify: Notify,
25}
26
27impl TopicQueue {
28 fn new() -> Self {
29 Self {
30 jobs: Mutex::new(Vec::new()),
31 notify: Notify::new(),
32 }
33 }
34
35 fn push(&self, job: Job) {
37 self.jobs.lock().expect("job queue poisoned").push(job);
38 self.notify.notify_one();
39 }
40}
41
42impl std::fmt::Debug for TopicQueue {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 f.debug_struct("TopicQueue")
45 .field(
46 "jobs_len",
47 &self.jobs.try_lock().map(|j| j.len()).unwrap_or(0),
48 )
49 .finish()
50 }
51}
52
53#[derive(Clone)]
55pub struct InMemoryQueue {
56 topics: Arc<RwLock<HashMap<String, Arc<TopicQueue>>>>,
57}
58
59impl std::fmt::Debug for InMemoryQueue {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 f.debug_struct("InMemoryQueue").finish()
62 }
63}
64
65impl Default for InMemoryQueue {
66 fn default() -> Self {
67 Self::new()
68 }
69}
70
71impl InMemoryQueue {
72 pub fn new() -> Self {
73 Self {
74 topics: Arc::new(RwLock::new(HashMap::new())),
75 }
76 }
77
78 fn topic(&self, topic: &str) -> Arc<TopicQueue> {
81 let existing = self.topics.read().unwrap().get(topic).cloned();
82 if let Some(tq) = existing {
83 return tq;
84 }
85 let mut topics = self.topics.write().unwrap();
86 Arc::clone(
87 topics
88 .entry(topic.to_owned())
89 .or_insert_with(|| Arc::new(TopicQueue::new())),
90 )
91 }
92}
93
94#[async_trait]
95impl Queue for InMemoryQueue {
96 async fn enqueue(&self, topic: &str, payload: Vec<u8>) -> Result<(), QueueError> {
97 let job = Job::new(JobId::generate(), topic, payload);
98 self.topic(topic).push(job);
99 Ok(())
100 }
101
102 async fn dequeue(&self, topic: &str, timeout: Duration) -> Result<Option<Job>, QueueError> {
103 let tq = self.topic(topic);
104 let deadline = tokio::time::Instant::now() + timeout;
105 loop {
106 if let Some(job) = tq.jobs.lock().expect("job queue poisoned").pop() {
107 return Ok(Some(job));
108 }
109 tokio::select! {
112 _ = tq.notify.notified() => {}
113 _ = tokio::time::sleep_until(deadline) => {
114 return Ok(tq.jobs.lock().expect("job queue poisoned").pop());
115 }
116 }
117 }
118 }
119
120 async fn ack(&self, _job: &Job) -> Result<(), JobError> {
121 Ok(())
122 }
123
124 async fn nack(&self, job: &Job, requeue: bool) -> Result<(), JobError> {
125 if requeue {
126 let tq = self.topic(&job.topic);
127 let retried = Job {
131 attempt: job.attempt + 1,
132 delivered_at: Some(std::time::SystemTime::now()),
133 ..job.clone()
134 };
135 tq.push(retried);
136 }
137 Ok(())
138 }
139
140 async fn dlq_move(&self, topic: &str, job: Job) -> Result<(), QueueError> {
141 let dlq_topic = format!("dlq:{topic}");
142 self.topic(&dlq_topic).push(job);
143 Ok(())
144 }
145
146 async fn len(&self, topic: &str) -> Result<u64, QueueError> {
147 let tq = self.topic(topic);
148 let guard = tq.jobs.lock().expect("job queue poisoned");
149 Ok(guard.len() as u64)
150 }
151}
152
153#[cfg(test)]
154mod tests {
155 use super::*;
156
157 #[tokio::test]
158 async fn enqueue_dequeue_roundtrip() {
159 let q = InMemoryQueue::new();
160 q.enqueue("test", b"hello".to_vec()).await.unwrap();
161 let job = q
162 .dequeue("test", Duration::from_millis(500))
163 .await
164 .unwrap()
165 .unwrap();
166 assert_eq!(job.payload, b"hello");
167 assert_eq!(job.topic, "test");
168 }
169
170 #[tokio::test]
171 async fn empty_returns_none() {
172 let q = InMemoryQueue::new();
173 let result = q.dequeue("none", Duration::from_millis(50)).await.unwrap();
174 assert!(result.is_none());
175 }
176
177 #[tokio::test]
178 async fn len_tracks_jobs() {
179 let q = InMemoryQueue::new();
180 q.enqueue("t", vec![1]).await.unwrap();
181 q.enqueue("t", vec![2]).await.unwrap();
182 assert_eq!(q.len("t").await.unwrap(), 2);
183 }
184
185 #[tokio::test]
186 async fn requeue_advances_attempt_on_the_same_job() {
187 let q = InMemoryQueue::new();
188 q.enqueue("t", vec![1]).await.unwrap();
189 let job = q
190 .dequeue("t", Duration::from_millis(50))
191 .await
192 .unwrap()
193 .unwrap();
194 assert_eq!(job.attempt, 0);
195
196 q.nack(&job, true).await.unwrap();
197 let again = q
198 .dequeue("t", Duration::from_millis(50))
199 .await
200 .unwrap()
201 .unwrap();
202 assert_eq!(again.id, job.id, "retry must keep the job identity");
203 assert_eq!(again.attempt, 1, "retry must advance the attempt counter");
204 }
205}