lapin 4.6.0

AMQP client library
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use crate::{
    Connection, Error, PromiseResolver,
    channel::Reply,
    connection_step::ConnectionStep,
    internal_rpc::InternalRPCHandle,
    promise::Cancelable,
    types::{ChannelId, FrameSize},
};
use amq_protocol::{
    frame::AMQPFrame,
    protocol::{AMQPClass, basic::AMQPMethod},
};
use std::{
    collections::{HashMap, VecDeque},
    fmt,
    ops::Deref,
    sync::{Arc, Mutex, MutexGuard},
};
use tracing::trace;

pub(crate) struct ExpectedReply(
    pub(crate) Reply,
    pub(crate) Box<dyn Cancelable + Send + Sync>,
);

impl fmt::Debug for ExpectedReply {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ExpectedReply").field(&self.0).finish()
    }
}

#[derive(Clone, Default)]
pub(crate) struct Frames(Arc<Mutex<Inner>>);

impl Frames {
    pub(crate) fn push(
        &self,
        channel_id: ChannelId,
        frame: AMQPFrame,
        canceler: Box<dyn Cancelable + Send + Sync>,
        expected_reply: Option<ExpectedReply>,
        resolver: Option<PromiseResolver<()>>,
    ) {
        self.lock_inner()
            .push(channel_id, frame, canceler, expected_reply, resolver);
    }

    pub(crate) fn push_frames(
        &self,
        channel_id: ChannelId,
        frames: Vec<AMQPFrame>,
        resolver: PromiseResolver<()>,
    ) {
        self.lock_inner().push_frames(channel_id, frames, resolver);
    }

    pub(crate) fn retry(&self, frame: FrameEntry) {
        self.lock_inner().retry_frames.push_back(frame);
    }

    pub(crate) fn pop(&self, flow: bool) -> Option<FrameEntry> {
        self.lock_inner().pop(flow)
    }

    pub(crate) fn find_connection_step(&self, channel_id: ChannelId) -> Option<ConnectionStep> {
        match self.find_expected_reply(channel_id, |reply| {
            matches!(&reply.0, Reply::ConnectionStep(..))
        }) {
            Some(Reply::ConnectionStep(step)) => Some(step),
            _ => None,
        }
    }

    pub(crate) fn connection_resolver(
        &self,
        channel_id: ChannelId,
    ) -> Option<PromiseResolver<Connection>> {
        let resolver = self
            .find_connection_step(channel_id)
            .map(ConnectionStep::into_connection_resolver);
        // We carry the Connection here to drop the lock() above before dropping the Connection
        resolver.map(|(resolver, _connection)| resolver)
    }

    pub(crate) fn clear_connection_steps(&self, error: Option<&Error>) {
        while let Some(resolver) = self.connection_resolver(0) {
            if let Some(err) = error {
                resolver.reject(err.clone());
            }
        }
    }

    pub(crate) fn find_expected_reply<P: FnMut(&ExpectedReply) -> bool>(
        &self,
        channel_id: ChannelId,
        finder: P,
    ) -> Option<Reply> {
        self.lock_inner()
            .expected_replies
            .get_mut(&channel_id)
            .and_then(|replies| {
                replies
                    .iter()
                    .position(finder)
                    .and_then(|idx| replies.remove(idx))
            })
            .map(|t| t.0)
    }

    pub(crate) fn next_expected_close_ok_reply(
        &self,
        channel_id: ChannelId,
        error: Error,
    ) -> Option<Reply> {
        self.lock_inner()
            .next_expected_close_ok_reply(channel_id, error)
    }

    pub(crate) fn has_pending(&self) -> bool {
        self.lock_inner().has_pending()
    }

    pub(crate) fn drop_pending(&self, error: Error, internal_rpc: &InternalRPCHandle) {
        self.lock_inner().drop_pending(error, internal_rpc);
    }

    pub(crate) fn take_expected_replies(
        &self,
        channel_id: ChannelId,
    ) -> Option<VecDeque<ExpectedReply>> {
        self.lock_inner().expected_replies.remove(&channel_id)
    }

    pub(crate) fn clear_expected_replies(&self, channel_id: ChannelId, error: Error) {
        if let Some(replies) = self.take_expected_replies(channel_id) {
            Self::cancel_expected_replies(replies, error)
        }
    }

    pub(crate) fn clear_all_expected_replies(&self, error: Error) {
        self.lock_inner().clear_all_expected_replies(error)
    }

    pub(crate) fn cancel_expected_replies(replies: VecDeque<ExpectedReply>, error: Error) {
        Inner::cancel_expected_replies(replies, error)
    }

    pub(crate) fn drop_frames_for_channel(&self, channel_id: ChannelId, error: Error) {
        self.lock_inner().drop_frames_for_channel(channel_id, error)
    }

    pub(crate) fn poison_channel(&self, channel_id: ChannelId, error: Error) {
        self.lock_inner().channels_poison.insert(channel_id, error);
    }

    pub(crate) fn poison(&self) -> Option<Error> {
        self.lock_inner().poison.clone()
    }

    pub(crate) fn drop_poison(&self) {
        self.lock_inner().poison.take();
    }

    pub(crate) fn drop_channel_poison(&self, channel_id: ChannelId) {
        self.lock_inner().channels_poison.remove(&channel_id);
    }

    fn lock_inner(&self) -> MutexGuard<'_, Inner> {
        self.0.lock().unwrap_or_else(|e| e.into_inner())
    }
}

pub(crate) struct FrameEntry {
    frame: AMQPFrame,
    sending: FrameSending,
}

impl From<AMQPFrame> for FrameEntry {
    fn from(frame: AMQPFrame) -> Self {
        (frame, None).into()
    }
}

impl From<(AMQPFrame, Option<PromiseResolver<()>>)> for FrameEntry {
    fn from(entry: (AMQPFrame, Option<PromiseResolver<()>>)) -> Self {
        Self::new(entry.0, FrameSending::new(None, entry.1))
    }
}

impl Deref for FrameEntry {
    type Target = AMQPFrame;

    fn deref(&self) -> &Self::Target {
        &self.frame
    }
}

impl FrameEntry {
    fn new(frame: AMQPFrame, sending: FrameSending) -> Self {
        Self { frame, sending }
    }

    fn reject(&self, error: Error) {
        self.sending.reject(error)
    }

    pub(crate) fn into_serialized_frame(self, sz: FrameSize) -> (FrameSize, FrameSending) {
        (sz, self.sending)
    }
}

impl fmt::Display for FrameEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.frame.fmt(f)
    }
}

pub(crate) struct FrameSending {
    canceler: Option<Box<dyn Cancelable + Send + Sync>>,
    resolver: Option<PromiseResolver<()>>,
}

impl FrameSending {
    fn new(
        canceler: Option<Box<dyn Cancelable + Send + Sync>>,
        resolver: Option<PromiseResolver<()>>,
    ) -> Self {
        Self { canceler, resolver }
    }

    pub(crate) fn reject(&self, error: Error) {
        if let Some(resolver) = self.resolver.as_ref() {
            resolver.reject(error);
        } else if let Some(canceler) = self.canceler.as_ref() {
            canceler.cancel(error);
        }
    }

    pub(crate) fn resolve(&self) {
        if let Some(resolver) = self.resolver.as_ref() {
            resolver.resolve(());
        }
    }
}

#[derive(Default)]
struct Inner {
    /* Header frames must follow basic.publish frames directly, otherwise RabbitMQ-server send us an UNEXPECTED_FRAME */
    /* After sending the Header frame, we need to send the associated Body frames before anything else for the same reason */
    publish_frames: VecDeque<FrameEntry>,
    retry_frames: VecDeque<FrameEntry>,
    frames: VecDeque<FrameEntry>,
    low_prio_frames: VecDeque<FrameEntry>,
    expected_replies: HashMap<ChannelId, VecDeque<ExpectedReply>>,
    poison: Option<Error>,
    channels_poison: HashMap<ChannelId, Error>,
}

impl fmt::Debug for Frames {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = f.debug_struct("Frames");
        if let Ok(inner) = self.0.try_lock() {
            debug.field("expected_replies", &inner.expected_replies);
        }
        debug.finish()
    }
}

impl Inner {
    fn push(
        &mut self,
        channel_id: ChannelId,
        frame: AMQPFrame,
        canceler: Box<dyn Cancelable + Send + Sync>,
        expected_reply: Option<ExpectedReply>,
        resolver: Option<PromiseResolver<()>>,
    ) {
        let sending = FrameSending::new(Some(canceler), resolver);
        if let Some(error) = self.check_poison(channel_id) {
            trace!(channel=%channel_id, frame=?frame, "Discarding frame because of poisoning");
            if let Some(reply) = expected_reply {
                Self::cancel_expected_reply(reply, error.clone());
            }
            sending.reject(error);
            return;
        }

        self.frames.push_back(FrameEntry::new(frame, sending));
        if let Some(reply) = expected_reply {
            trace!(
                channel=%channel_id,
                expected_reply=?reply,
                "state is now waiting"
            );
            self.expected_replies
                .entry(channel_id)
                .or_default()
                .push_back(reply);
        }
    }

    fn push_frames(
        &mut self,
        channel_id: ChannelId,
        mut frames: Vec<AMQPFrame>,
        resolver: PromiseResolver<()>,
    ) {
        let last_frame = frames.pop();

        if let Some(error) = self.check_poison(channel_id) {
            trace!(channel=%channel_id, frames=?frames, "Discarding frames because of poisoning");
            resolver.reject(error);
            return;
        }

        for frame in frames {
            self.low_prio_frames.push_back(frame.into());
        }

        if let Some(last_frame) = last_frame {
            self.low_prio_frames
                .push_back((last_frame, Some(resolver)).into());
        } else {
            resolver.resolve(());
        }
    }

    fn check_poison(&self, channel_id: ChannelId) -> Option<Error> {
        self.channels_poison
            .get(&channel_id)
            .or(self.poison.as_ref())
            .cloned()
    }

    fn pop(&mut self, flow: bool) -> Option<FrameEntry> {
        if let Some(frame) = self
            .retry_frames
            .pop_front()
            .or_else(|| self.publish_frames.pop_front())
            .or_else(|| self.frames.pop_front())
        {
            return Some(frame);
        }
        if flow && let Some(frame) = self.low_prio_frames.pop_front() {
            // If the next frame is a header, that means we're a basic.publish
            // Header frame needs to follow directly the basic.publish frame, and Body frames
            // need to be sent just after those or the AMQP server will close the connection.
            // Push the header into publish_frames which is there to handle just that.
            if self
                .low_prio_frames
                .front()
                .map(|frame| frame.is_header())
                .unwrap_or(false)
            {
                while let Some(next_frame) = self.low_prio_frames.pop_front() {
                    match *next_frame {
                        AMQPFrame::Header(..) | AMQPFrame::Body(..) => {
                            self.publish_frames.push_back(next_frame);
                        }
                        _ => {
                            // We've exhausted Body frames for this publish, push back the next one and exit
                            self.low_prio_frames.push_front(next_frame);
                            break;
                        }
                    }
                }
            }
            return Some(frame);
        }
        None
    }

    fn has_pending(&self) -> bool {
        !(self.retry_frames.is_empty()
            && self.publish_frames.is_empty()
            && self.frames.is_empty()
            && self.low_prio_frames.is_empty())
    }

    fn drop_pending(&mut self, error: Error, internal_rpc: &InternalRPCHandle) {
        Self::drop_pending_frames(&mut self.retry_frames, error.clone(), internal_rpc);
        Self::drop_pending_frames(&mut self.publish_frames, error.clone(), internal_rpc);
        Self::drop_pending_frames(&mut self.frames, error.clone(), internal_rpc);
        Self::drop_pending_frames(&mut self.low_prio_frames, error.clone(), internal_rpc);
        self.clear_all_expected_replies(error.clone());
        self.poison = Some(error);
    }

    fn drop_pending_frames(
        frames: &mut VecDeque<FrameEntry>,
        error: Error,
        internal_rpc: &InternalRPCHandle,
    ) {
        for frame in std::mem::take(frames) {
            match frame.frame {
                AMQPFrame::Method(channel_id, AMQPClass::Basic(AMQPMethod::Cancel(cancel))) => {
                    internal_rpc.deregister_consumer(channel_id, cancel.consumer_tag);
                    frame.sending.resolve();
                }
                _ => frame.reject(error.clone()),
            }
        }
    }

    fn drop_frames_for_channel(&mut self, channel_id: ChannelId, error: Error) {
        Self::drop_pending_frames_for_channel(channel_id, &mut self.retry_frames, error.clone());
        Self::drop_pending_frames_for_channel(channel_id, &mut self.publish_frames, error.clone());
        Self::drop_pending_frames_for_channel(channel_id, &mut self.frames, error.clone());
        Self::drop_pending_frames_for_channel(channel_id, &mut self.low_prio_frames, error.clone());
    }

    fn drop_pending_frames_for_channel(
        channel_id: ChannelId,
        frames: &mut VecDeque<FrameEntry>,
        error: Error,
    ) {
        frames.retain(|frame| {
            if frame.channel_id() == channel_id {
                frame.reject(error.clone());
                false
            } else {
                true
            }
        })
    }

    fn next_expected_close_ok_reply(
        &mut self,
        channel_id: ChannelId,
        error: Error,
    ) -> Option<Reply> {
        let expected_replies = self.expected_replies.get_mut(&channel_id)?;
        while let Some(reply) = expected_replies.pop_front() {
            match &reply.0 {
                Reply::ChannelCloseOk(_) => return Some(reply.0),
                Reply::BasicCancelOk(resolver) => resolver.resolve(()), // Channel close means consumer is canceled automatically
                _ => reply.1.cancel(error.clone()),
            }
        }
        None
    }

    fn clear_all_expected_replies(&mut self, error: Error) {
        for (_, replies) in self.expected_replies.drain() {
            Self::cancel_expected_replies(replies, error.clone());
        }
    }

    fn cancel_expected_replies(replies: VecDeque<ExpectedReply>, error: Error) {
        for reply in replies {
            Self::cancel_expected_reply(reply, error.clone());
        }
    }

    fn cancel_expected_reply(reply: ExpectedReply, error: Error) {
        let ExpectedReply(reply, cancel) = reply;
        match reply {
            Reply::BasicCancelOk(resolver) => resolver.resolve(()),
            _ => cancel.cancel(error.clone()),
        }
    }
}