knien 0.0.10

Typed RabbitMQ interfacing for async Rust
Documentation
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use std::{any::type_name, marker::PhantomData};

use futures::{Future, Stream, StreamExt};
use lapin::BasicProperties;
use serde::{Deserialize, Serialize};
use tracing::debug;
use uuid::Uuid;

use crate::{
    error::Error, Bus, Channel, Consumer, Delivery, DirectBus, Publisher, ReplyError, Result,
    RpcBus, RpcChannel,
};

/// An RPC-based communication bus that defines an `InitialPayload`,
/// a `BackPayload` and `ForthPayload`, and supports flows where a single
/// initial message is sent, after which back-and-forth a communication
/// over which multiple messages can be sent is setup.
pub trait RpcCommBus: Unpin {
    /// The arguments used to format the queue
    type Args;

    /// Method with which queue names are built
    fn queue(args: Self::Args) -> String;

    /// The type of payload of the message that is initally sent onto the bus
    type InitialPayload;

    /// The type of payload of the messages that the receiver of the initial message sends back
    type BackPayload;
    /// The type of payload of the messages that the initiator of the communication sends forth
    type ForthPayload;

    /// Serialize [RpcCommBus::InitialPayload] into a [Vec<u8>]
    fn serialize_initial(payload: &Self::InitialPayload) -> Result<Vec<u8>>;
    /// Deserialize a byte slice into a [RpcCommBus::InitialPayload]
    fn deserialize_initial(bytes: &[u8]) -> Result<Self::InitialPayload>;

    /// Serialize [RpcCommBus::BackPayload] into a [Vec<u8>]
    fn serialize_back(payload: &Self::BackPayload) -> Result<Vec<u8>>;
    /// Deserialize a byte slice into a [RpcCommBus::BackPayload]
    fn deserialize_back(bytes: &[u8]) -> Result<Self::BackPayload>;

    /// Serialize [RpcCommBus::ForthPayload] into a [Vec<u8>]
    fn serialize_forth(payload: &Self::ForthPayload) -> Result<Vec<u8>>;
    /// Deserialize a byte slice into a [RpcCommBus::ForthPayload]
    fn deserialize_forth(bytes: &[u8]) -> Result<Self::ForthPayload>;
}

impl<B: RpcCommBus> Bus for B {
    type Chan = RpcChannel;
    type PublishPayload = B::InitialPayload;

    fn serialize_payload(payload: &Self::PublishPayload) -> Result<Vec<u8>> {
        B::serialize_initial(payload)
    }

    fn deserialize_payload(bytes: &[u8]) -> Result<Self::PublishPayload> {
        B::deserialize_initial(bytes)
    }
}

impl<B: RpcCommBus> DirectBus for B {
    type Args = B::Args;

    fn queue(args: Self::Args) -> String {
        B::queue(args)
    }
}

#[derive(Debug)]
/// A reply on an [RpcCommBus].
pub struct BackReply<B> {
    _marker: PhantomData<B>,
}

impl<B: RpcCommBus> Bus for BackReply<B> {
    type Chan = RpcChannel;
    type PublishPayload = B::BackPayload;

    fn serialize_payload(payload: &Self::PublishPayload) -> Result<Vec<u8>> {
        B::serialize_back(payload)
    }

    fn deserialize_payload(bytes: &[u8]) -> Result<Self::PublishPayload> {
        B::deserialize_back(bytes)
    }
}

impl<B: RpcCommBus> DirectBus for BackReply<B> {
    type Args = B::Args;

    fn queue(args: Self::Args) -> String {
        B::queue(args)
    }
}

impl<B: RpcCommBus> RpcBus for BackReply<B> {
    type ReplyPayload = B::ForthPayload;

    fn serialize_reply(payload: &Self::ReplyPayload) -> Result<Vec<u8>> {
        B::serialize_forth(payload)
    }

    fn deserialize_reply(bytes: &[u8]) -> Result<Self::ReplyPayload> {
        B::deserialize_forth(bytes)
    }
}

#[derive(Debug)]
/// A reply on an [RpcCommBus].
pub struct ForthReply<B> {
    _marker: PhantomData<B>,
}

impl<B: RpcCommBus> Bus for ForthReply<B> {
    type Chan = RpcChannel;
    type PublishPayload = B::ForthPayload;

    fn serialize_payload(payload: &Self::PublishPayload) -> Result<Vec<u8>> {
        B::serialize_forth(payload)
    }

    fn deserialize_payload(bytes: &[u8]) -> Result<Self::PublishPayload> {
        B::deserialize_forth(bytes)
    }
}

impl<B: RpcCommBus> DirectBus for ForthReply<B> {
    type Args = B::Args;

    fn queue(args: Self::Args) -> String {
        B::queue(args)
    }
}

impl RpcChannel {
    /// Create a new [Consumer] for the [RpcCommBus] that declares
    /// a direct queue with the name produced by [RpcCommBus::queue]
    /// given the passed [RpcCommBus::Args]
    pub async fn comm_consumer<B: RpcCommBus>(
        &self,
        args: B::Args,
        consumer_tag: &str,
    ) -> Result<Consumer<B>> {
        let queue = B::queue(args);
        self.inner
            .queue_declare(&queue, Default::default(), Default::default())
            .await?;
        let consumer = self
            .inner
            .basic_consume(&queue, consumer_tag, Default::default(), Default::default())
            .await?;

        debug!(
            "Created consumer for RPC comm bus {} for queue {queue} with consumer tag {consumer_tag}",
            type_name::<B>()
        );

        Ok(Consumer {
            inner: consumer,
            _marker: PhantomData,
        })
    }

    /// Setup a new [Publisher] associated wit the [RpcCommBus].
    pub fn comm_publisher<B: RpcCommBus>(&self) -> Publisher<B> {
        debug!("Created comm publisher for RPC bus {}", type_name::<B>());
        Publisher { chan: self.clone() }
    }
}

impl<'i, 'b, 'f, B> Publisher<B>
where
    B: RpcCommBus,
    B::InitialPayload: Deserialize<'i> + Serialize,
    B::BackPayload: Deserialize<'b> + Serialize,
    B::ForthPayload: Deserialize<'f> + Serialize,
{
    /// Publish an initial message onto the [RpcCommBus], and receive the replies with
    /// with [RpcCommBus::BackPayload] payloads onto the returned [Stream].
    /// The replies can be obtained by calling [futures::StreamExt::next] on the returned [Stream].
    pub async fn publish_recv_comm(
        &self,
        args: B::Args,
        payload: &B::InitialPayload,
    ) -> Result<impl Stream<Item = Delivery<BackReply<B>>>> {
        let correlation_uuid = Uuid::new_v4();

        let rx = self.chan.register_pending_reply(correlation_uuid);

        let properties = BasicProperties::default().with_reply_to("amq.rabbitmq.reply-to".into());
        debug!("Publishing message with correlation UUID {correlation_uuid}, setting up back-and-forth communication");
        self.publish_with_properties(&B::queue(args), payload, properties, correlation_uuid, None)
            .await?;
        Ok(rx)
    }
}

impl<'i, 'b, 'f, B> Delivery<B>
where
    B: RpcCommBus,
    B::InitialPayload: Deserialize<'i> + Serialize,
    B::BackPayload: Deserialize<'b> + Serialize,
    B::ForthPayload: Deserialize<'f> + Serialize,
{
    /// Reply to a message that was sent by the receiver of the initial message and await
    /// multiple replies from the receiver.
    /// The messages can be obtained by calling [futures::StreamExt::next] on the returned [Stream].
    pub async fn reply_recv_many(
        &self,
        back_payload: &B::BackPayload,
        rpc_chan: &RpcChannel,
    ) -> Result<impl Stream<Item = Delivery<ForthReply<B>>>> {
        let Some(correlation_uuid) = self.get_uuid() else {
            return Err(Error::Reply(ReplyError::NoCorrelationUuid));
        };
        let Some(reply_to) = self.inner.properties.reply_to().as_ref().map(|r | r.as_str()) else {
            return Err(Error::Reply(ReplyError::NoReplyToConfigured))
        };

        let reply_uuid = correlation_uuid?;
        let correlation_uuid = Uuid::new_v4();
        let bytes = B::serialize_back(back_payload)?;

        let rx = rpc_chan.register_pending_reply(correlation_uuid);

        let properties = BasicProperties::default().with_reply_to("amq.rabbitmq.reply-to".into());

        debug!("Replying back to message with correlation UUID {reply_uuid}");

        rpc_chan
            .publish_with_properties(
                &bytes,
                reply_to,
                properties,
                correlation_uuid,
                Some(reply_uuid),
            )
            .await?;
        Ok(rx)
    }

    /// Reply to a message that was sent by the receiver of the initial message and await
    /// one reply from the receiver.
    /// The message can be obtained by `await`ing the returned [Future].
    pub async fn reply_recv(
        &self,
        back_payload: &B::BackPayload,
        rpc_chan: &RpcChannel,
    ) -> Result<impl Future<Output = Delivery<ForthReply<B>>>> {
        let rx = self.reply_recv_many(back_payload, rpc_chan).await?;
        Ok(async move {
            // As `rx` won't be dropped, we can assume that either
            // this future never resolves, or it resolves to a `Some`
            rx.take(1).next().await.unwrap()
        })
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use futures::StreamExt;
    use tokio::time::timeout;
    use tracing::info;
    use uuid::Uuid;

    use crate::{
        chan::tests::FramePayload, rpc_comm_bus, setup_test_logging, Connection, Consumer,
        FrameSendError, Publisher, RpcChannel, RABBIT_MQ_URL,
    };

    rpc_comm_bus!(FrameCommBus, FramePayload, FramePayload, Result<(), FrameSendError>, u32, |args| format!(
        "frame_comm_{}",
        args
    ), serde_json::to_vec, serde_json::from_slice);

    #[tokio::test]
    async fn publish_recv_comm() -> crate::Result<()> {
        let _log = setup_test_logging();
        let connection = Connection::connect(RABBIT_MQ_URL).await?;
        let uuid = Uuid::new_v4();

        let handle = tokio::task::spawn({
            let channel = RpcChannel::new(&connection).await?;
            let mut consumer: Consumer<FrameCommBus> = channel
                .comm_consumer(123, &Uuid::new_v4().to_string())
                .await?;

            async move {
                let initial = consumer.next().await.unwrap().unwrap();
                initial.ack(true).await.unwrap();
                info!("Got initial");
                for i in 0..3 {
                    info!("Sending back reply {i}");
                    let response_fut = initial
                        .reply_recv(
                            &FramePayload {
                                message: i.to_string(),
                            },
                            &channel,
                        )
                        .await
                        .unwrap();

                    let response = timeout(Duration::from_secs(5), response_fut).await.unwrap();
                    info!("Got forth reply {i}");
                    assert_eq!(response.get_payload().unwrap(), Ok(()))
                }
            }
        });

        let channel = RpcChannel::new(&connection).await?;
        let publisher: Publisher<FrameCommBus> = channel.comm_publisher();

        let mut rx = publisher
            .publish_recv_comm(
                123,
                &FramePayload {
                    message: uuid.to_string(),
                },
            )
            .await?;
        info!("Sent initial");
        for i in 0..3 {
            let back_reply = timeout(Duration::from_secs(5), rx.next())
                .await
                .unwrap()
                .unwrap();
            info!("Got back reply {i}, sending forth reply {i}");
            back_reply.reply(&Ok(()), &channel).await?;
        }
        handle.await.unwrap();
        Ok(())
    }
}

#[macro_export]
/// Declare a new [RpcCommBus].
macro_rules! rpc_comm_bus {
    ($doc:literal, $bus:ident, $initial_payload:ty, $back_payload:ty, $forth_payload:ty, $args:ty, $queue:expr, $serialize:expr, $deserialize:expr) => {
        #[doc = $doc]
        #[derive(Clone, Copy, Debug)]
        pub enum $bus {}

        $crate::rpc_comm_bus_impl!(
            $bus,
            $initial_payload,
            $back_payload,
            $forth_payload,
            $args,
            $queue,
            $serialize,
            $deserialize
        );
    };
    (doc = $doc:literal, bus = $bus:ident, initial = $initial_payload:ty, back = $back_payload:ty, forth = $forth_payload:ty, args = $args:ty, queue = $queue:expr, serialize = $serialize:expr, deserialize = $deserialize:expr) => {
        $crate::rpc_comm_bus!(
            $doc,
            $bus,
            $initial_payload,
            $back_payload,
            $forth_payload,
            $args,
            $queue,
            $serialize,
            $deserialize
        );
    };
    ($bus:ident, $initial_payload:ty, $back_payload:ty, $forth_payload:ty, $args:ty, $queue:expr, $serialize:expr, $deserialize:expr) => {
        $crate::rpc_comm_bus!(
            "",
            $bus,
            $initial_payload,
            $back_payload,
            $forth_payload,
            $args,
            $queue,
            $serialize,
            $deserialize
        );
    };
    (bus = $bus:ident, initial = $initial_payload:ty, back = $back_payload:ty, forth = $forth_payload:ty, args = $args:ty, queue = $queue:expr, serialize = $serialize:expr, deserialize = $deserialize:expr) => {
        $crate::rpc_comm_bus!(
            $bus,
            $initial_payload,
            $back_payload,
            $forth_payload,
            $args,
            $queue,
            $serialize,
            $deserialize
        );
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! rpc_comm_bus_impl {
    ($bus:ty, $initial_payload:ty, $back_payload:ty, $forth_payload:ty, $args:ty, $queue:expr, $serialize:expr, $deserialize:expr) => {
        impl $crate::RpcCommBus for $bus {
            type InitialPayload = $initial_payload;
            type BackPayload = $back_payload;
            type ForthPayload = $forth_payload;
            type Args = $args;

            fn queue(args: Self::Args) -> String {
                #[allow(clippy::redundant_closure_call)]
                ($queue)(args)
            }

            fn serialize_initial(payload: &Self::InitialPayload) -> $crate::Result<Vec<u8>> {
                #[allow(clippy::redundant_closure_call)]
                ($serialize)(payload).map_err(|e| $crate::Error::Serde(Box::new(e)))
            }

            fn deserialize_initial(bytes: &[u8]) -> $crate::Result<Self::InitialPayload> {
                #[allow(clippy::redundant_closure_call)]
                ($deserialize)(bytes).map_err(|e| $crate::Error::Serde(Box::new(e)))
            }


            fn serialize_back(payload: &Self::BackPayload) -> $crate::Result<Vec<u8>> {
                #[allow(clippy::redundant_closure_call)]
                ($serialize)(payload).map_err(|e| $crate::Error::Serde(Box::new(e)))
            }

            fn deserialize_back(bytes: &[u8]) -> $crate::Result<Self::BackPayload> {
                #[allow(clippy::redundant_closure_call)]
                ($deserialize)(bytes).map_err(|e| $crate::Error::Serde(Box::new(e)))
            }


            fn serialize_forth(payload: &Self::ForthPayload) -> $crate::Result<Vec<u8>> {
                #[allow(clippy::redundant_closure_call)]
                ($serialize)(payload).map_err(|e| $crate::Error::Serde(Box::new(e)))
            }

            fn deserialize_forth(bytes: &[u8]) -> $crate::Result<Self::ForthPayload> {
                #[allow(clippy::redundant_closure_call)]
                ($deserialize)(bytes).map_err(|e| $crate::Error::Serde(Box::new(e)))
            }
        }
    };
}