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
//! Handler configuration.
use std::time::Duration;
use lapin::options::QueueDeclareOptions;
use lapin::types::{AMQPValue, FieldTable};
/// Detailed configuration of a handler.
#[derive(Clone, Debug)]
pub struct HandlerConfig {
/// Queue name to bind to. By default, this will be the same as whatever routing key is used for the handler.
pub(crate) queue: Option<String>,
/// The exchange that the queue will be bound to.
pub(crate) exchange: String,
/// Prefetch for the queue.
pub(crate) prefetch: u16,
/// Queue declare options.
pub(crate) options: QueueDeclareOptions,
/// Queue arguments (aka. x-arguments).
pub(crate) arguments: FieldTable,
/// True indicates that the handler should reply to messages (the default).
/// False indicates that the handler should *not* reply to messages.
///
/// Note that using `()` as the response type from a handler is not sufficient for making the handler not respond,
/// as `()` implements [`prost::Message`], making it a valid protobuf response message.
pub(crate) should_reply: bool,
}
impl HandlerConfig {
/// The default value for the prefetch count.
pub const DEFAULT_PREFETCH: u16 = 64;
/// The default exchange is indicated by the empty string in AMQP.
/// Note that the default exchange is actually just a direct exchange with no name.
pub const DEFAULT_EXCHANGE: &'static str = "";
/// The direct exchange. See <`https://www.rabbitmq.com/tutorials/tutorial-four-python.html`> for more information.
pub const DIRECT_EXCHANGE: &'static str = "amq.direct";
/// The topic exchange. See <`https://www.rabbitmq.com/tutorials/tutorial-five-python.html`> for more information.
pub const TOPIC_EXCHANGE: &'static str = "amq.topic";
/// Creates a new default [`HandlerConfig`].
pub fn new() -> Self {
Default::default()
}
/// Sets the queue name. Defaults to the same as the routing key.
pub fn with_queue(mut self, queue: impl Into<String>) -> Self {
self.queue = Some(queue.into());
self
}
/// Sets the exchange of the handler. Defaults to the direct exchange, [`HandlerConfig::DIRECT_EXCHANGE`].
pub fn with_exchange(mut self, exchange: impl Into<String>) -> Self {
self.exchange = exchange.into();
self
}
/// Per consumer prefetch count. See [documentation](https://www.rabbitmq.com/confirms.html#channel-qos-prefetch).
pub fn with_prefetch(mut self, prefetch: u16) -> Self {
self.prefetch = prefetch;
self
}
/// Overwrite the `auto-delete` property for the queue (defaults to `true`).
/// See also [documentation](https://www.rabbitmq.com/queues.html#properties).
pub fn with_auto_delete(mut self, auto_delete: bool) -> Self {
self.options.auto_delete = auto_delete;
self
}
/// Set the `durable` property of the queue (defaults to `false`).
/// See also the [documentation](https://www.rabbitmq.com/queues.html#properties).
pub fn with_durable(mut self, durable: bool) -> Self {
self.options.durable = durable;
self
}
/// Queues will expire after a period of time only when they are not used (e.g. do not have consumers).
/// See [documentation](https://www.rabbitmq.com/ttl.html#queue-ttl).
// Panic is extremely unlikely, let's not bother.
#[allow(clippy::missing_panics_doc)]
pub fn with_expires(mut self, expires: Duration) -> Self {
let millis: i64 = expires
.as_millis()
.try_into()
.expect("Duration too long to fit milliseconds in i64");
self.arguments.insert("x-expires".into(), millis.into());
self
}
/// Messages expires if not consumed within `message_ttl`.
/// See [documentation](https://www.rabbitmq.com/ttl.html#message-ttl-using-x-args).
// Panic is extremely unlikely, let's not bother.
#[allow(clippy::missing_panics_doc)]
pub fn with_message_ttl(mut self, message_ttl: Duration) -> Self {
let millis: i64 = message_ttl
.as_millis()
.try_into()
.expect("Duration too long to fit milliseconds in i64");
self.arguments.insert("x-message-ttl".into(), millis.into());
self
}
/// Sets the `x-dead-letter-exchange` argument on the queue. See also [RabbitMQ's documentation](https://www.rabbitmq.com/dlx.html).
pub fn with_dead_letter_exchange(mut self, dead_letter_exchange: impl Into<String>) -> Self {
self.arguments.insert(
"x-dead-letter-exchange".into(),
AMQPValue::LongString(dead_letter_exchange.into().into()),
);
self
}
/// Sets the `x-dead-letter-routing-key` argument on the queue. See also [RabbitMQ's documentation](https://www.rabbitmq.com/dlx.html).
pub fn with_dead_letter_routing_key(
mut self,
dead_letter_routing_key: impl Into<String>,
) -> Self {
self.arguments.insert(
"x-dead-letter-routing-key".into(),
AMQPValue::LongString(dead_letter_routing_key.into().into()),
);
self
}
/// Sets the `x-consumer-timeout` argument on the queue. See also [RabbitMQ's documentation](https://www.rabbitmq.com/consumers.html).
// Panic is extremely unlikely, let's not bother.
#[allow(clippy::missing_panics_doc)]
pub fn with_consumer_timeout(mut self, consumer_timeout: Duration) -> Self {
let millis: i64 = consumer_timeout
.as_millis()
.try_into()
.expect("Duration too long to fit milliseconds in i64");
self.arguments
.insert("x-consumer-timeout".into(), millis.into());
self
}
/// Set any argument with any value.
///
/// Prefer the more specific methods if you can, but you can use this for any specific argument you might want to set.
pub fn with_arg(mut self, arg: impl Into<String>, value: impl Into<AMQPValue>) -> Self {
self.arguments.insert(arg.into().into(), value.into());
self
}
/// Sets whether or not the handler should reply to messages. Defaults to true.
pub fn with_replies(mut self, should_reply: bool) -> Self {
self.should_reply = should_reply;
self
}
}
impl Default for HandlerConfig {
fn default() -> Self {
Self {
queue: None,
exchange: Self::DIRECT_EXCHANGE.to_string(),
prefetch: Self::DEFAULT_PREFETCH,
options: QueueDeclareOptions {
auto_delete: true,
..Default::default()
},
arguments: Default::default(),
should_reply: true,
}
}
}