camel_component/
consumer.rs1use async_trait::async_trait;
2use tokio::sync::{mpsc, oneshot};
3use tokio_util::sync::CancellationToken;
4
5use camel_api::{CamelError, Exchange};
6
7pub struct ExchangeEnvelope {
13 pub exchange: Exchange,
14 pub reply_tx: Option<oneshot::Sender<Result<Exchange, CamelError>>>,
15}
16
17#[derive(Clone)]
19pub struct ConsumerContext {
20 sender: mpsc::Sender<ExchangeEnvelope>,
21 cancel_token: CancellationToken,
22}
23
24impl ConsumerContext {
25 pub fn new(sender: mpsc::Sender<ExchangeEnvelope>, cancel_token: CancellationToken) -> Self {
27 Self {
28 sender,
29 cancel_token,
30 }
31 }
32
33 pub async fn cancelled(&self) {
36 self.cancel_token.cancelled().await
37 }
38
39 pub fn is_cancelled(&self) -> bool {
41 self.cancel_token.is_cancelled()
42 }
43
44 pub fn cancel_token(&self) -> CancellationToken {
49 self.cancel_token.clone()
50 }
51
52 pub fn sender(&self) -> mpsc::Sender<ExchangeEnvelope> {
58 self.sender.clone()
59 }
60
61 pub async fn send(&self, exchange: Exchange) -> Result<(), CamelError> {
63 self.sender
64 .send(ExchangeEnvelope {
65 exchange,
66 reply_tx: None,
67 })
68 .await
69 .map_err(|_| CamelError::ChannelClosed)
70 }
71
72 pub async fn send_and_wait(&self, exchange: Exchange) -> Result<Exchange, CamelError> {
77 let (reply_tx, reply_rx) = oneshot::channel();
78 self.sender
79 .send(ExchangeEnvelope {
80 exchange,
81 reply_tx: Some(reply_tx),
82 })
83 .await
84 .map_err(|_| CamelError::ChannelClosed)?;
85 reply_rx.await.map_err(|_| CamelError::ChannelClosed)?
86 }
87}
88
89#[derive(Debug, Clone, PartialEq, Eq)]
91pub enum ConcurrencyModel {
92 Sequential,
95 Concurrent { max: Option<usize> },
99}
100
101#[async_trait]
103pub trait Consumer: Send + Sync {
104 async fn start(&mut self, context: ConsumerContext) -> Result<(), CamelError>;
106
107 async fn stop(&mut self) -> Result<(), CamelError>;
109
110 fn concurrency_model(&self) -> ConcurrencyModel {
119 ConcurrencyModel::Sequential
120 }
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126
127 #[tokio::test]
128 async fn test_consumer_context_cancelled() {
129 let (tx, _rx) = mpsc::channel(16);
130 let token = CancellationToken::new();
131 let ctx = ConsumerContext::new(tx, token.clone());
132
133 assert!(!ctx.is_cancelled());
134 token.cancel();
135 ctx.cancelled().await;
136 assert!(ctx.is_cancelled());
137 }
138
139 #[test]
140 fn test_concurrency_model_default_is_sequential() {
141 use super::ConcurrencyModel;
142
143 struct DummyConsumer;
144
145 #[async_trait::async_trait]
146 impl super::Consumer for DummyConsumer {
147 async fn start(&mut self, _ctx: super::ConsumerContext) -> Result<(), CamelError> {
148 Ok(())
149 }
150 async fn stop(&mut self) -> Result<(), CamelError> {
151 Ok(())
152 }
153 }
154
155 let consumer = DummyConsumer;
156 assert_eq!(consumer.concurrency_model(), ConcurrencyModel::Sequential);
157 }
158
159 #[test]
160 fn test_concurrency_model_concurrent_override() {
161 use super::ConcurrencyModel;
162
163 struct ConcurrentConsumer;
164
165 #[async_trait::async_trait]
166 impl super::Consumer for ConcurrentConsumer {
167 async fn start(&mut self, _ctx: super::ConsumerContext) -> Result<(), CamelError> {
168 Ok(())
169 }
170 async fn stop(&mut self) -> Result<(), CamelError> {
171 Ok(())
172 }
173 fn concurrency_model(&self) -> ConcurrencyModel {
174 ConcurrencyModel::Concurrent { max: Some(16) }
175 }
176 }
177
178 let consumer = ConcurrentConsumer;
179 assert_eq!(
180 consumer.concurrency_model(),
181 ConcurrencyModel::Concurrent { max: Some(16) }
182 );
183 }
184}