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
use crate::error::ClientError;
use crate::{Identifier, StreamIdentifier, Subscription, WSStream};
use futures_util::SinkExt;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::collections::HashMap;
use std::fmt::Debug;
use tokio::sync::mpsc;
use tokio_stream::StreamExt;
use tokio_tungstenite::tungstenite::Message as WSMessage;
use tracing::{debug, error, info, warn};
pub type StreamRoutingId = String;
pub struct ConnectionHandler<Message, Sub> {
pub ws_conn: WSStream,
pub subscription_rx: mpsc::Receiver<(Sub, mpsc::UnboundedSender<Message>)>,
pub exchange_data_txs: HashMap<StreamRoutingId, mpsc::UnboundedSender<Message>>,
}
impl<Message, Sub> ConnectionHandler<Message, Sub>
where
Sub: Debug + Subscription + StreamIdentifier + Serialize + Send + Sync,
Message: Debug + StreamIdentifier + DeserializeOwned + Send + Sync,
{
pub fn new(
ws_conn: WSStream,
subscription_rx: mpsc::Receiver<(Sub, mpsc::UnboundedSender<Message>)>,
) -> Self {
Self {
ws_conn,
subscription_rx,
exchange_data_txs: Default::default(),
}
}
pub async fn manage(mut self) {
loop {
tokio::select! {
Some((sub_request, data_tx)) = self.subscription_rx.recv() => {
self = self.action_subscription_request(sub_request, data_tx).await;
}
Some(ws_message_result) = self.ws_conn.next() => {
let ws_message = match ws_message_result {
Ok(ws_message) => ws_message,
Err(err) => {
warn!("Skipping message Result due to unexpected error: {:?}", err);
continue
},
};
let exchange_message = match ws_message {
WSMessage::Text(text) => {
match serde_json::from_str::<Message>(&*text.clone()) {
Ok(message) => message,
Err(err) => {
error!("Failure to deserialise message: {:?} due to error: {:?}", text, err);
continue;
},
}
},
WSMessage::Binary(binary) => {
warn!("Received unexpected binary message: {:?}", binary);
continue;
},
WSMessage::Close(close_frame) => {
info!("WebSocket connection closed with final frame: {:?}", close_frame);
break;
}
_ => continue,
};
let routing_id = match exchange_message.get_stream_id() {
Identifier::Yes(routing_id) => routing_id,
Identifier::No => {
debug!("Ignoring received message due to no route: {:?}", exchange_message);
continue
},
};
let data_tx = self.retrieve_relevant_data_transmitter(&routing_id);
if data_tx.send(exchange_message).is_err() {
info!("Receiver for: {:?} has been dropped - closing stream", &routing_id);
self.exchange_data_txs.remove_entry(&routing_id);
continue;
}
}
}
}
}
async fn action_subscription_request(
mut self,
sub_request: Sub,
data_tx: mpsc::UnboundedSender<Message>,
) -> Self {
info!(
"Received Subscription request from ExchangeClient: {:?}",
sub_request
);
let routing_id = match sub_request.get_stream_id() {
Identifier::Yes(routing_id) => routing_id,
Identifier::No => {
warn!(
"Ignoring subscription request due to a non-identifiable routing_id: {:?}",
sub_request
);
return self;
}
};
match self.subscribe(sub_request).await {
Ok(_) => {
self.exchange_data_txs.insert(routing_id.clone(), data_tx);
}
Err(err) => {
warn!(
"Failed to subscribe to stream: {:?} due to error: {:?}",
routing_id, err
)
}
}
self
}
pub async fn subscribe(&mut self, subscription: Sub) -> Result<(), ClientError> {
self.ws_conn
.send(WSMessage::text(subscription.as_text()?))
.await
.map_err(|write_err| ClientError::WebSocketWrite(write_err))?;
Ok(())
}
fn retrieve_relevant_data_transmitter(
&mut self,
routing_id: &String,
) -> &mut mpsc::UnboundedSender<Message> {
self.exchange_data_txs.get_mut(routing_id).expect(
format!(
"Message with StreamRoutingId: {:?} has been received \
without a relevant exchange_data_tx in the map to route \
it to",
routing_id
)
.as_str(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::client::binance::{BinanceMessage, BinanceSub};
use crate::connect;
async fn gen_binance_conn() -> WSStream {
connect(&String::from("wss://stream.binance.com:9443/ws"))
.await
.unwrap()
}
fn gen_valid_binance_sub() -> BinanceSub {
BinanceSub::new("@depth20@100ms".to_string(), "ethbtc".to_string())
}
#[tokio::test]
async fn test_binance_subscribe() {
struct TestCase {
conn_handler: ConnectionHandler<BinanceMessage, BinanceSub>,
input_sub: BinanceSub,
expected_can_subscribe: bool,
}
let test_cases = vec![TestCase {
conn_handler: ConnectionHandler::new(gen_binance_conn().await, mpsc::channel(10).1),
input_sub: gen_valid_binance_sub(),
expected_can_subscribe: true,
}];
for (index, mut test) in test_cases.into_iter().enumerate() {
let actual_result = test.conn_handler.subscribe(test.input_sub).await;
assert_eq!(
test.expected_can_subscribe,
actual_result.is_ok(),
"Test case: {:?}",
index
);
}
}
}