forked from bytebeamio/rumqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iobufs.rs
236 lines (206 loc) · 7.13 KB
/
iobufs.rs
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 std::{collections::VecDeque, sync::Arc};
use flume::{Receiver, Sender};
use parking_lot::Mutex;
use tracing::{error, warn};
use crate::{
protocol::Packet,
router::{FilterIdx, MAX_CHANNEL_CAPACITY},
Cursor, Notification,
};
use super::{Forward, IncomingMeter, OutgoingMeter};
const MAX_INFLIGHT: usize = 100;
const MAX_PKID: u16 = MAX_INFLIGHT as u16;
#[derive(Debug)]
pub struct Incoming {
/// Identifier associated with connected client
pub(crate) client_id: String,
/// Recv buffer
pub(crate) buffer: Arc<Mutex<VecDeque<Packet>>>,
/// incoming metrics
pub(crate) meter: IncomingMeter,
}
impl Incoming {
#[inline]
pub(crate) fn new(client_id: String) -> Self {
Self {
buffer: Arc::new(Mutex::new(VecDeque::with_capacity(MAX_CHANNEL_CAPACITY))),
meter: Default::default(),
client_id,
}
}
#[inline]
pub(crate) fn buffer(&self) -> Arc<Mutex<VecDeque<Packet>>> {
self.buffer.clone()
}
#[inline]
pub(crate) fn exchange(&mut self, mut v: VecDeque<Packet>) -> VecDeque<Packet> {
std::mem::swap(&mut v, &mut self.buffer.lock());
v
}
}
#[derive(Debug)]
pub struct Outgoing {
/// Identifier associated with connected client
pub(crate) client_id: String,
/// Send buffer
pub(crate) data_buffer: Arc<Mutex<VecDeque<Notification>>>,
/// Handle which is given to router to allow router to communicate with this connection
pub(crate) handle: Sender<()>,
/// The buffer to keep track of inflight packets.
inflight_buffer: VecDeque<(u16, FilterIdx, Cursor)>,
/// Last packet id
last_pkid: u16,
/// Metrics of outgoing messages of this connection
pub(crate) meter: OutgoingMeter,
}
impl Outgoing {
#[inline]
pub(crate) fn new(client_id: String) -> (Self, Receiver<()>) {
let (handle, rx) = flume::bounded(MAX_CHANNEL_CAPACITY);
let data_buffer = VecDeque::with_capacity(MAX_CHANNEL_CAPACITY);
let inflight_buffer = VecDeque::with_capacity(MAX_INFLIGHT);
// Ensure that there won't be any new allocations
assert!(MAX_INFLIGHT <= inflight_buffer.capacity());
assert!(MAX_CHANNEL_CAPACITY <= data_buffer.capacity());
let outgoing = Self {
client_id,
data_buffer: Arc::new(Mutex::new(data_buffer)),
inflight_buffer,
handle,
last_pkid: 0,
meter: Default::default(),
};
(outgoing, rx)
}
#[inline]
pub(crate) fn buffer(&self) -> Arc<Mutex<VecDeque<Notification>>> {
self.data_buffer.clone()
}
pub fn free_slots(&self) -> usize {
MAX_INFLIGHT - self.inflight_buffer.len()
}
pub fn push_notification(&mut self, notification: Notification) -> usize {
let mut buffer = self.data_buffer.lock();
buffer.push_back(notification);
buffer.len()
}
/// Push packets to the outgoing buffer.
pub fn push_forwards(
&mut self,
publishes: impl Iterator<Item = Forward>,
qos: u8,
filter_idx: usize,
) -> (usize, usize) {
let mut buffer = self.data_buffer.lock();
let publishes = publishes;
if qos == 0 {
for p in publishes {
self.meter.publish_count += 1;
buffer.push_back(Notification::Forward(p));
// self.meter.total_size += p.len();
}
// self.meter.update_data_rate(total_size);
let buffer_count = buffer.len();
let inflight_count = self.inflight_buffer.len();
return (buffer_count, inflight_count);
}
for mut p in publishes {
// Index and pkid of current outgoing packet
self.last_pkid += 1;
p.publish.pkid = self.last_pkid;
self.inflight_buffer
.push_back((self.last_pkid, filter_idx, p.cursor));
// Place max pkid packet at index 0
if self.last_pkid == MAX_PKID {
self.last_pkid = 0;
}
self.meter.publish_count += 1;
self.meter.total_size += p.publish.topic.len() + p.publish.payload.len();
buffer.push_back(Notification::Forward(p));
}
let buffer_count = buffer.len();
let inflight_count = self.inflight_buffer.len();
if inflight_count > MAX_INFLIGHT {
warn!(
"More inflight publishes than max allowed, inflight count = {}, max allowed = {}",
inflight_count, MAX_INFLIGHT
);
}
(buffer_count, inflight_count)
}
// Returns (unsolicited, outoforder) flags
// Return: Out of order or unsolicited acks
pub fn register_ack(&mut self, pkid: u16) -> Option<()> {
let (head, _filter_idx, _cursor) = match self.inflight_buffer.pop_front() {
Some(v) => v,
None => return None,
};
// We don't support out of order acks
if pkid != head {
error!(pkid, head, "out of order ack.");
return None;
}
Some(())
}
}
#[cfg(test)]
mod test {
// use super::{Outgoing, MAX_INFLIGHT};
// use crate::protocol::{Publish, QoS};
// use crate::router::Forward;
// use crate::Notification;
//
// fn publishes(count: usize) -> impl Iterator<Item = Forward> {
// (1..=count).map(|v| {
// let publish = Publish {
// dup: false,
// retain: false,
// pkid: 0,
// qos: QoS::AtLeastOnce,
// topic: "hello/world".into(),
// payload: vec![1, 2, 3].into(),
// };
//
// Forward {
// cursor: (0, v as u64),
// publish,
// size: 0,
// }
// })
// }
//
// #[test]
// fn inflight_ring_buffer_pushes_correctly() {
// let count = MAX_INFLIGHT as usize;
// let (mut outgoing, _rx) = Outgoing::new("hello".to_owned());
// outgoing.push_forwards(publishes(count), 1, 3);
//
// // Index 1 = (0, 1), Index 99 = (0, 99), Index 0 = (0, 100)
// assert_eq!(outgoing.inflight_buffer[0].unwrap().1, (0, count as u64));
// for i in 1..count {
// assert_eq!(outgoing.inflight_buffer[i].unwrap().1, (0, i as u64));
// }
//
// // Outgoing publish pkids are as expected
// for (i, o) in outgoing.data_buffer.lock().iter().enumerate() {
// let pkid = match &o {
// Notification::Forward(f) => f.publish.pkid,
// _ => unreachable!(),
// };
//
// assert_eq!(pkid, i as u16 + 1);
// }
// }
//
// #[test]
// fn inflight_ring_buffer_pops_correctly() {
// let (mut outgoing, _rx) = Outgoing::new("hello".to_owned());
// outgoing.push_forwards(publishes(MAX_INFLIGHT as usize), 1, 3);
//
// for pkid in 1..=MAX_INFLIGHT {
// let (unsolicited, outoforder) = outgoing.register_ack(pkid);
// assert_eq!(unsolicited, false);
// assert_eq!(outoforder, false);
// }
// }
}