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
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use futures::{Future, Poll, Sink, Stream};
use futures::future;
use futures::future::Executor;
use futures::sync::{mpsc, oneshot};
use error;
use resp;
use resp::FromResp;
use super::connect::{connect, ClientConnection};
fn err<S>(msg: S) -> Box<Future<Item = (), Error = error::Error> + Send>
where
S: Into<String>,
{
Box::new(future::err(error::internal(msg)))
}
pub fn pubsub_connect<E>(
addr: &SocketAddr,
executor: E,
) -> Box<Future<Item = PubsubConnection, Error = error::Error>>
where
E: Executor<Box<Future<Item = (), Error = ()> + Send>> + 'static,
{
let pubsub_con = connect(addr)
.map_err(|e| e.into())
.and_then(move |connection| {
let ClientConnection { sender, receiver } = connection;
let (out_tx, out_rx) = mpsc::unbounded();
let sender = Box::new(
sender
.sink_map_err(|e| error!("Sending socket error: {}", e))
.send_all(out_rx.map_err(|()| error!("Outgoing message channel error")))
.map(|_| debug!("Send all completed successfully")),
) as Box<Future<Item = (), Error = ()> + Send>;
let subs = Arc::new(Mutex::new(PubsubSubscriptions {
pending: HashMap::new(),
confirmed: HashMap::new(),
}));
let subs_reader = subs.clone();
let receiver_raw = receiver.for_each(move |msg| {
let (message_type, topic, msg) = match msg {
resp::RespValue::Array(mut messages) => match (
messages.pop(),
messages.pop(),
messages.pop(),
messages.pop(),
) {
(Some(msg), Some(topic), Some(message_type), None) => {
match (msg, String::from_resp(topic), message_type) {
(msg, Ok(topic), resp::RespValue::BulkString(bytes)) => {
(bytes, topic, msg)
}
_ => return err("RESP Message structure invalid"),
}
}
_ => {
return err(format!(
"Incorrect number of records in PUBSUB message: {}",
messages.len()
))
}
},
_ => return err("Incorrect message type"),
};
let mut subs = subs_reader.lock().expect("Lock is tainted");
if message_type == b"subscribe" {
if let Some((tx, notification_tx)) = subs.pending.remove(&topic) {
subs.confirmed.insert(topic, tx);
match notification_tx.send(()) {
Ok(()) => {
let ok =
future::ok(()).map_err(|_: ()| error::internal("unreachable"));
Box::new(ok)
}
Err(()) => err("Cannot send notification of subscription"),
}
} else {
err("Received subscribe notification that wasn't expected")
}
} else if message_type == b"unsubscribe" {
if let Some(_) = subs.confirmed.remove(&topic) {
let result = if subs.confirmed.is_empty() {
future::err(error::Error::EndOfStream)
} else {
future::ok(())
};
Box::new(result)
} else {
err("Receieved unsubscription notification from a topic we're not subscribed to")
}
} else if message_type == b"message" {
match subs.confirmed.get(&topic) {
Some(tx) => {
let future = tx.clone().send(msg).map(|_| ()).map_err(|e| e.into());
Box::new(future)
}
None => {
let ok = future::ok(()).map_err(|_: ()| error::internal("unreachable"));
Box::new(ok)
}
}
} else {
return err(format!("Unexpected message type: {:?}", message_type));
}
});
let receiver = Box::new(
receiver_raw
.then(|result| match result {
Ok(_) => future::ok(()),
Err(error::Error::EndOfStream) => future::ok(()),
Err(e) => future::err(e),
})
.map(|_| debug!("End of PUBSUB connection, successful"))
.map_err(|e| error!("PUBSUB Receiver error: {}", e)),
) as Box<Future<Item = (), Error = ()> + Send>;
match executor
.execute(sender)
.and_then(|_| executor.execute(receiver))
{
Ok(()) => future::ok(PubsubConnection {
out_tx: out_tx,
subscriptions: subs,
}),
Err(e) => future::err(error::internal(format!(
"Cannot execute background task: {:?}",
e
))),
}
});
Box::new(pubsub_con)
}
struct PubsubSubscriptions {
pending: HashMap<String, (mpsc::Sender<resp::RespValue>, oneshot::Sender<()>)>,
confirmed: HashMap<String, mpsc::Sender<resp::RespValue>>,
}
#[derive(Clone)]
pub struct PubsubConnection {
out_tx: mpsc::UnboundedSender<resp::RespValue>,
subscriptions: Arc<Mutex<PubsubSubscriptions>>,
}
impl PubsubConnection {
pub fn subscribe<T: Into<String>>(
&self,
topic: T,
) -> Box<Future<Item = PubsubStream, Error = error::Error> + Send> {
let topic = topic.into();
let mut subs = self.subscriptions.lock().expect("Lock is tainted");
let (tx, rx) = mpsc::channel(10);
if subs.confirmed.contains_key(&topic) || subs.pending.contains_key(&topic) {
return Box::new(future::err(error::internal("Already subscribed")));
}
let (notification_tx, notification_rx) = oneshot::channel();
let subscribe_msg = resp_array!["SUBSCRIBE", &topic];
subs.pending.insert(topic.clone(), (tx, notification_tx));
let new_out_tx = self.out_tx.clone();
match self.out_tx.unbounded_send(subscribe_msg) {
Ok(_) => {
let done = notification_rx
.map(|_| PubsubStream::new(topic, rx, new_out_tx))
.map_err(|e| e.into());
Box::new(done)
}
Err(e) => Box::new(future::err(e.into())),
}
}
}
pub struct PubsubStream {
topic: String,
underlying: mpsc::Receiver<resp::RespValue>,
out_tx: mpsc::UnboundedSender<resp::RespValue>,
}
impl PubsubStream {
fn new(
topic: String,
stream: mpsc::Receiver<resp::RespValue>,
out_tx: mpsc::UnboundedSender<resp::RespValue>,
) -> Self {
PubsubStream {
topic: topic,
underlying: stream,
out_tx: out_tx,
}
}
}
impl Stream for PubsubStream {
type Item = resp::RespValue;
type Error = ();
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
self.underlying.poll()
}
}
impl Drop for PubsubStream {
fn drop(&mut self) {
let send_f = self.out_tx
.clone()
.send(resp_array!["UNSUBSCRIBE", &self.topic]);
match send_f.wait() {
Ok(_) => debug!("Send unsubscribe command for topic: {}", self.topic),
Err(e) => error!(
"Failed to send unsubscribe command for topic: {}, error: {}",
self.topic, e
),
}
}
}