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
use connection::Connection;
use error::Error;
use futures::Future;
use serde::de::DeserializeOwned;
use serde_json;
use message::{Message, Payload, proto::{self, command_subscribe::SubType}};
use rand;
use tokio::runtime::TaskExecutor;
use futures::{Stream, sync::mpsc, Async};
use std::sync::Arc;
pub struct Consumer<T> {
connection: Arc<Connection>,
id: u64,
messages: mpsc::UnboundedReceiver<Message>,
deserialize: Box<dyn Fn(Payload) -> Result<T, Error> + Send>,
batch_size: u32,
remaining_messages: u32,
}
impl<T: DeserializeOwned> Consumer<T> {
pub fn new(
addr: String,
topic: String,
subscription: String,
sub_type: SubType,
consumer_id: Option<u64>,
consumer_name: Option<String>,
executor: TaskExecutor,
deserialize: Box<dyn Fn(Payload) -> Result<T, Error> + Send>,
batch_size: Option<u32>,
) -> impl Future<Item=Consumer<T>, Error=Error> {
let consumer_id = consumer_id.unwrap_or_else(rand::random);
let (resolver, messages) = mpsc::unbounded();
let batch_size = batch_size.unwrap_or(1000);
Connection::new(addr, executor.clone())
.and_then(move |conn|
conn.sender().subscribe(resolver, topic, subscription, sub_type, consumer_id, consumer_name)
.map(move |resp| (resp, conn)))
.and_then(move |(_, conn)| {
conn.sender().send_flow(consumer_id, batch_size)
.map(move |()| conn)
})
.map(move |connection| {
Consumer {
connection: Arc::new(connection),
id: consumer_id,
messages,
deserialize,
batch_size,
remaining_messages: batch_size
}
})
}
}
pub struct Ack {
consumer_id: u64,
message_id: Vec<proto::MessageIdData>,
connection: Arc<Connection>,
}
impl Ack {
pub fn new(consumer_id: u64, msg: proto::MessageIdData, connection: Arc<Connection>) -> Ack {
Ack { consumer_id, message_id: vec![msg], connection }
}
pub fn join(mut self, other: Ack) -> Self {
self.message_id.extend(other.message_id);
self
}
pub fn extend<I: IntoIterator<Item=Ack>>(mut self, others: I) -> Self {
self.message_id.extend(others.into_iter().flat_map(|ack| ack.message_id));
self
}
pub fn ack(self) {
let _ = self.connection.sender().send_ack(self.consumer_id, self.message_id);
}
}
impl<T> Stream for Consumer<T> {
type Item = Result<(T, Ack), Error>;
type Error = Error;
fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Self::Error> {
if !self.connection.is_valid() {
if let Some(err) = self.connection.error() {
return Err(err);
}
}
if self.remaining_messages <= self.batch_size / 2 {
self.connection.sender().send_flow(self.id, self.batch_size - self.remaining_messages)?;
self.remaining_messages = self.batch_size;
}
let message: Option<Option<(proto::CommandMessage, Payload)>> = try_ready!(self.messages.poll().map_err(|_| Error::Disconnected))
.map(| Message { command, payload }: Message|
command.message
.and_then(move |msg| payload
.map(move |payload| (msg, payload))));
if message.is_some() {
self.remaining_messages -= 1;
}
match message {
Some(Some((message, payload))) => {
Ok(Async::Ready(Some({
let ack = Ack::new(self.id, message.message_id, self.connection.clone());
match (&self.deserialize)(payload) {
Ok(data) => Ok((data, ack)),
Err(e) => Err(e)
}
})))
},
Some(None) => Ok(Async::Ready(Some(Err(Error::Unexpected(format!("Missing payload for message {:?}", message)))))),
None => Ok(Async::Ready(None))
}
}
}
impl<T> Drop for Consumer<T> {
fn drop(&mut self) {
let _ = self.connection.sender().close_consumer(self.id);
}
}
pub struct Set<T>(T);
pub struct Unset;
pub struct ConsumerBuilder<Topic, Subscription, SubscriptionType, DataType> {
addr: String,
topic: Topic,
subscription: Subscription,
subscription_type: SubscriptionType,
consumer_id: Option<u64>,
consumer_name: Option<String>,
executor: TaskExecutor,
deserialize: Option<Box<dyn Fn(Payload) -> Result<DataType, Error> + Send>>,
batch_size: Option<u32>,
}
impl ConsumerBuilder<Unset, Unset, Unset, Unset> {
pub fn new<S: Into<String>>(addr: S, executor: TaskExecutor) -> Self {
ConsumerBuilder {
addr: addr.into(),
topic: Unset,
subscription: Unset,
subscription_type: Unset,
consumer_id: None,
consumer_name: None,
executor,
deserialize: None,
batch_size: None,
}
}
}
impl<Subscription, SubscriptionType, DataType> ConsumerBuilder<Unset, Subscription, SubscriptionType, DataType> {
pub fn with_topic<S: Into<String>>(self, topic: S) -> ConsumerBuilder<Set<String>, Subscription, SubscriptionType, DataType> {
ConsumerBuilder {
topic: Set(topic.into()),
addr: self.addr,
subscription: self.subscription,
subscription_type: self.subscription_type,
consumer_id: self.consumer_id,
consumer_name: self.consumer_name,
executor: self.executor,
deserialize: self.deserialize,
batch_size: self.batch_size,
}
}
}
impl<Topic, SubscriptionType, DataType> ConsumerBuilder<Topic, Unset, SubscriptionType, DataType> {
pub fn with_subscription<S: Into<String>>(self, subscription: S) -> ConsumerBuilder<Topic, Set<String>, SubscriptionType, DataType> {
ConsumerBuilder {
subscription: Set(subscription.into()),
topic: self.topic,
addr: self.addr,
subscription_type: self.subscription_type,
consumer_id: self.consumer_id,
consumer_name: self.consumer_name,
executor: self.executor,
deserialize: self.deserialize,
batch_size: self.batch_size,
}
}
}
impl<Topic, Subscription, DataType> ConsumerBuilder<Topic, Subscription, Unset, DataType> {
pub fn with_subscription_type(self, subscription_type: SubType) -> ConsumerBuilder<Topic, Subscription, Set<SubType>, DataType> {
ConsumerBuilder {
subscription_type: Set(subscription_type),
topic: self.topic,
addr: self.addr,
subscription: self.subscription,
consumer_id: self.consumer_id,
consumer_name: self.consumer_name,
executor: self.executor,
deserialize: self.deserialize,
batch_size: self.batch_size,
}
}
}
impl<Topic, Subscription, SubscriptionType> ConsumerBuilder<Topic, Subscription, SubscriptionType, Unset> {
pub fn with_deserializer<T, F>(self, deserializer: F) -> ConsumerBuilder<Topic, Subscription, SubscriptionType, T>
where F: Fn(Payload) -> Result<T, Error> + Send + 'static
{
ConsumerBuilder {
deserialize: Some(Box::new(deserializer)),
topic: self.topic,
addr: self.addr,
subscription: self.subscription,
subscription_type: self.subscription_type,
consumer_name: self.consumer_name,
consumer_id: self.consumer_id,
executor: self.executor,
batch_size: self.batch_size,
}
}
}
impl<Topic, Subscription, SubscriptionType, DataType> ConsumerBuilder<Topic, Subscription, SubscriptionType, DataType> {
pub fn with_consumer_id(self, consumer_id: u64) -> ConsumerBuilder<Topic, Subscription, SubscriptionType, DataType> {
ConsumerBuilder {
consumer_id: Some(consumer_id),
topic: self.topic,
addr: self.addr,
subscription: self.subscription,
subscription_type: self.subscription_type,
consumer_name: self.consumer_name,
executor: self.executor,
deserialize: self.deserialize,
batch_size: self.batch_size,
}
}
pub fn with_consumer_name<S: Into<String>>(self, consumer_name: S) -> ConsumerBuilder<Topic, Subscription, SubscriptionType, DataType> {
ConsumerBuilder {
consumer_name: Some(consumer_name.into()),
topic: self.topic,
addr: self.addr,
subscription: self.subscription,
subscription_type: self.subscription_type,
consumer_id: self.consumer_id,
executor: self.executor,
deserialize: self.deserialize,
batch_size: self.batch_size,
}
}
pub fn with_batch_size(self, batch_size: u32) -> ConsumerBuilder<Topic, Subscription, SubscriptionType, DataType> {
ConsumerBuilder {
batch_size: Some(batch_size),
topic: self.topic,
addr: self.addr,
subscription: self.subscription,
subscription_type: self.subscription_type,
consumer_name: self.consumer_name,
consumer_id: self.consumer_id,
executor: self.executor,
deserialize: self.deserialize,
}
}
}
impl ConsumerBuilder<Set<String>, Set<String>, Set<SubType>, Unset> {
pub fn build<T: DeserializeOwned>(self) -> impl Future<Item=Consumer<T>, Error=Error> {
let deserialize = Box::new(|payload: Payload| {
serde_json::from_slice(&payload.data).map_err(|e| e.into())
});
let ConsumerBuilder {
addr,
topic: Set(topic),
subscription: Set(subscription),
subscription_type: Set(sub_type),
consumer_id,
consumer_name,
executor,
batch_size,
..
} = self;
Consumer::new(addr, topic, subscription, sub_type, consumer_id, consumer_name, executor, deserialize, batch_size)
}
}
impl<T: DeserializeOwned> ConsumerBuilder<Set<String>, Set<String>, Set<SubType>, T> {
pub fn build(self) -> impl Future<Item=Consumer<T>, Error=Error> {
let ConsumerBuilder {
addr,
topic: Set(topic),
subscription: Set(subscription),
subscription_type: Set(sub_type),
consumer_id,
consumer_name,
executor,
deserialize,
batch_size,
} = self;
let deserialize = deserialize.unwrap();
Consumer::new(addr, topic, subscription, sub_type, consumer_id, consumer_name, executor, deserialize, batch_size)
}
}