msg-socket 0.1.6

Sockets for msg-rs
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use std::{io, time::Duration};

use bytes::Bytes;
use msg_common::constants::KiB;
use thiserror::Error;

mod driver;

mod session;

mod socket;
pub use socket::*;

mod stats;
use crate::{Profile, stats::SocketStats};
use stats::PubStats;

mod trie;

use msg_wire::{
    compression::{CompressionType, Compressor},
    pubsub,
};

/// The default high water mark for the socket.
const DEFAULT_HWM: usize = 1024;

/// Errors that can occur when using a publisher socket.
#[derive(Debug, Error)]
pub enum PubError {
    #[error("IO error: {0:?}")]
    Io(#[from] io::Error),
    #[error("Wire protocol error: {0:?}")]
    Wire(#[from] msg_wire::reqrep::Error),
    #[error("Socket closed")]
    SocketClosed,
    #[error("Topic already exists")]
    TopicExists,
    #[error("Unknown topic: {0}")]
    UnknownTopic(String),
    #[error("Could not connect to any valid endpoints")]
    NoValidEndpoints,
}

#[derive(Debug)]
pub struct PubOptions {
    /// The maximum number of concurrent clients.
    max_clients: Option<usize>,
    /// The maximum number of outgoing messages that can be buffered per session.
    high_water_mark: usize,
    /// The size of the write buffer in bytes.
    pub write_buffer_size: usize,
    /// The linger duration for the write buffer (how long to wait before flushing).
    pub write_buffer_linger: Option<Duration>,
    /// Minimum payload size in bytes for compression to be used. If the payload is smaller than
    /// this threshold, it will not be compressed.
    min_compress_size: usize,
}

impl PubOptions {
    /// Creates new options based on the given profile.
    pub fn new(profile: Profile) -> Self {
        match profile {
            Profile::Balanced => Self::balanced(),
            Profile::Latency => Self::low_latency(),
            Profile::Throughput => Self::high_throughput(),
        }
    }
}

impl PubOptions {
    /// Creates options optimized for low latency.
    pub fn low_latency() -> Self {
        Self {
            write_buffer_size: 8 * KiB as usize,
            write_buffer_linger: Some(Duration::from_micros(50)),
            ..Default::default()
        }
    }

    /// Creates options optimized for high throughput.
    pub fn high_throughput() -> Self {
        Self {
            write_buffer_size: 256 * KiB as usize,
            write_buffer_linger: Some(Duration::from_micros(200)),
            ..Default::default()
        }
    }

    /// Creates options optimized for a balanced trade-off between latency and throughput.
    pub fn balanced() -> Self {
        Self {
            write_buffer_size: 32 * KiB as usize,
            write_buffer_linger: Some(Duration::from_micros(100)),
            ..Default::default()
        }
    }
}

impl PubOptions {
    /// Sets the maximum number of concurrent clients.
    pub fn with_max_clients(mut self, max_clients: usize) -> Self {
        self.max_clients = Some(max_clients);
        self
    }

    /// Sets the high-water mark per session. This is the amount of messages that can be buffered
    /// per session before messages start being dropped.
    pub fn with_high_water_mark(mut self, hwm: usize) -> Self {
        self.high_water_mark = hwm;
        self
    }

    /// Sets the minimum payload size in bytes for compression to be used. If the payload is smaller
    /// than this threshold, it will not be compressed.
    pub fn with_min_compress_size(mut self, min_compress_size: usize) -> Self {
        self.min_compress_size = min_compress_size;
        self
    }

    /// Sets the size (max capacity) of the write buffer in bytes.
    /// When the buffer is full, it will be flushed to the underlying transport.
    ///
    /// Default: 8KiB
    pub fn with_write_buffer_size(mut self, size: usize) -> Self {
        self.write_buffer_size = size;
        self
    }

    /// Sets the linger duration for the write buffer. If `None`, the write buffer will only be
    /// flushed when the buffer is full.
    ///
    /// Default: 100µs
    pub fn with_write_buffer_linger(mut self, duration: Option<Duration>) -> Self {
        self.write_buffer_linger = duration;
        self
    }
}

impl Default for PubOptions {
    fn default() -> Self {
        Self {
            max_clients: None,
            high_water_mark: DEFAULT_HWM,
            min_compress_size: 8192,
            write_buffer_size: 8192,
            write_buffer_linger: Some(Duration::from_micros(100)),
        }
    }
}

/// A message received from a publisher.
/// Includes the source, topic, and payload.
#[derive(Debug, Clone)]
pub struct PubMessage {
    /// The compression type used for the message payload.
    compression_type: CompressionType,
    /// The topic of the message.
    topic: String,
    /// The message payload.
    payload: Bytes,
}

#[allow(unused)]
impl PubMessage {
    pub fn new(topic: String, payload: Bytes) -> Self {
        Self {
            // Initialize the compression type to None.
            // The actual compression type will be set in the `compress` method.
            compression_type: CompressionType::None,
            topic,
            payload,
        }
    }

    #[inline]
    pub fn topic(&self) -> &str {
        &self.topic
    }

    #[inline]
    pub fn payload(&self) -> &Bytes {
        &self.payload
    }

    #[inline]
    pub fn into_payload(self) -> Bytes {
        self.payload
    }

    #[inline]
    pub fn into_wire(self, seq: u32) -> pubsub::Message {
        pubsub::Message::new(
            seq,
            Bytes::from(self.topic),
            self.payload,
            self.compression_type as u8,
        )
    }

    #[inline]
    pub fn compress(&mut self, compressor: &dyn Compressor) -> Result<(), io::Error> {
        self.payload = compressor.compress(&self.payload)?;
        self.compression_type = compressor.compression_type();

        Ok(())
    }
}

/// The publisher socket state, shared between the backend task and the socket.
#[derive(Debug, Default)]
pub(crate) struct SocketState {
    pub(crate) stats: SocketStats<PubStats>,
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use futures::StreamExt;
    use msg_transport::{quic::Quic, tcp::Tcp};
    use msg_wire::compression::GzipCompressor;
    use tracing::info;

    use crate::{
        SubOptions, SubSocket,
        hooks::token::{ClientHook, ServerHook},
    };

    use super::*;

    #[tokio::test]
    async fn pubsub_simple() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut pub_socket = PubSocket::new(Tcp::default());

        let mut sub_socket = SubSocket::with_options(Tcp::default(), SubOptions::default());

        pub_socket.bind("0.0.0.0:0").await.unwrap();
        let addr = pub_socket.local_addr().unwrap();

        sub_socket.connect(addr).await.unwrap();
        sub_socket.subscribe("HELLO".to_string()).await.unwrap();
        tokio::time::sleep(Duration::from_millis(100)).await;

        pub_socket.publish("HELLO".to_string(), "WORLD".into()).await.unwrap();

        let msg = sub_socket.next().await.unwrap();
        info!("Received message: {:?}", msg);
        assert_eq!("HELLO", msg.topic());
        assert_eq!("WORLD", msg.payload());
    }

    #[tokio::test]
    async fn pubsub_auth_tcp() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut pub_socket =
            PubSocket::new(Tcp::default()).with_connection_hook(ServerHook::accept_all());

        let mut sub_socket = SubSocket::new(Tcp::default())
            .with_connection_hook(ClientHook::new(Bytes::from("client1")));

        pub_socket.bind("0.0.0.0:0").await.unwrap();
        let addr = pub_socket.local_addr().unwrap();

        sub_socket.connect(addr).await.unwrap();
        sub_socket.subscribe("HELLO".to_string()).await.unwrap();
        tokio::time::sleep(Duration::from_millis(100)).await;

        pub_socket.publish("HELLO".to_string(), "WORLD".into()).await.unwrap();

        let msg = sub_socket.next().await.unwrap();
        info!("Received message: {:?}", msg);
        assert_eq!("HELLO", msg.topic());
        assert_eq!("WORLD", msg.payload());
    }

    #[tokio::test]
    async fn pubsub_auth_quic() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut pub_socket =
            PubSocket::new(Quic::default()).with_connection_hook(ServerHook::accept_all());

        let mut sub_socket = SubSocket::new(Quic::default())
            .with_connection_hook(ClientHook::new(Bytes::from("client1")));

        pub_socket.bind("0.0.0.0:0").await.unwrap();
        let addr = pub_socket.local_addr().unwrap();

        sub_socket.connect(addr).await.unwrap();
        sub_socket.subscribe("HELLO".to_string()).await.unwrap();
        tokio::time::sleep(Duration::from_millis(100)).await;

        pub_socket.publish("HELLO".to_string(), "WORLD".into()).await.unwrap();

        let msg = sub_socket.next().await.unwrap();
        info!("Received message: {:?}", msg);
        assert_eq!("HELLO", msg.topic());
        assert_eq!("WORLD", msg.payload());
    }

    #[tokio::test]
    async fn pubsub_many() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut pub_socket = PubSocket::new(Tcp::default());

        let mut sub1 = SubSocket::new(Tcp::default());

        let mut sub2 = SubSocket::new(Tcp::default());

        pub_socket.bind("0.0.0.0:0").await.unwrap();
        let addr = pub_socket.local_addr().unwrap();

        sub1.connect(addr).await.unwrap();
        sub2.connect(addr).await.unwrap();
        sub1.subscribe("HELLO".to_string()).await.unwrap();
        sub2.subscribe("HELLO".to_string()).await.unwrap();
        tokio::time::sleep(Duration::from_millis(100)).await;

        pub_socket.publish("HELLO".to_string(), Bytes::from("WORLD")).await.unwrap();

        let msg = sub1.next().await.unwrap();
        info!("Received message: {:?}", msg);
        assert_eq!("HELLO", msg.topic());
        assert_eq!("WORLD", msg.payload());

        let msg = sub2.next().await.unwrap();
        info!("Received message: {:?}", msg);
        assert_eq!("HELLO", msg.topic());
        assert_eq!("WORLD", msg.payload());
    }

    #[tokio::test]
    async fn pubsub_many_compressed() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut pub_socket = PubSocket::new(Tcp::default()).with_compressor(GzipCompressor::new(6));

        let mut sub1 = SubSocket::new(Tcp::default());

        let mut sub2 = SubSocket::new(Tcp::default());

        pub_socket.bind("0.0.0.0:0").await.unwrap();
        let addr = pub_socket.local_addr().unwrap();

        sub1.connect(addr).await.unwrap();
        sub2.connect(addr).await.unwrap();
        sub1.subscribe("HELLO".to_string()).await.unwrap();
        sub2.subscribe("HELLO".to_string()).await.unwrap();
        tokio::time::sleep(Duration::from_millis(100)).await;

        let original_msg = Bytes::from(
            "WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOORLD",
        );

        pub_socket.publish("HELLO".to_string(), original_msg.clone()).await.unwrap();

        let msg = sub1.next().await.unwrap();
        info!("Received message: {:?}", msg);
        assert_eq!("HELLO", msg.topic());
        assert_eq!(original_msg, msg.payload());

        let msg = sub2.next().await.unwrap();
        info!("Received message: {:?}", msg);
        assert_eq!("HELLO", msg.topic());
        assert_eq!(original_msg, msg.payload());
    }

    #[tokio::test]
    async fn pubsub_durable_tcp() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut pub_socket = PubSocket::new(Tcp::default());

        let mut sub_socket = SubSocket::new(Tcp::default());

        // Try to connect and subscribe before the publisher is up
        sub_socket.connect("0.0.0.0:6662").await.unwrap();
        sub_socket.subscribe("HELLO".to_string()).await.unwrap();
        tokio::time::sleep(Duration::from_millis(500)).await;

        pub_socket.bind("0.0.0.0:6662").await.unwrap();
        tokio::time::sleep(Duration::from_millis(2000)).await;

        pub_socket.publish("HELLO".to_string(), Bytes::from("WORLD")).await.unwrap();

        let msg = sub_socket.next().await.unwrap();
        info!("Received message: {:?}", msg);
        assert_eq!("HELLO", msg.topic());
        assert_eq!("WORLD", msg.payload());
    }

    #[tokio::test]
    async fn pubsub_durable_quic() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut pub_socket = PubSocket::new(Quic::default());

        let mut sub_socket = SubSocket::new(Quic::default());

        // Try to connect and subscribe before the publisher is up
        sub_socket.connect("0.0.0.0:6662").await.unwrap();
        sub_socket.subscribe("HELLO".to_string()).await.unwrap();
        tokio::time::sleep(Duration::from_millis(1000)).await;

        pub_socket.bind("0.0.0.0:6662").await.unwrap();
        tokio::time::sleep(Duration::from_millis(2000)).await;

        pub_socket.publish("HELLO".to_string(), Bytes::from("WORLD")).await.unwrap();

        let msg = sub_socket.next().await.unwrap();
        info!("Received message: {:?}", msg);
        assert_eq!("HELLO", msg.topic());
        assert_eq!("WORLD", msg.payload());
    }

    #[tokio::test]
    async fn pubsub_max_clients() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut pub_socket =
            PubSocket::with_options(Tcp::default(), PubOptions::default().with_max_clients(1));

        pub_socket.bind("0.0.0.0:0").await.unwrap();

        let mut sub1 = SubSocket::with_options(Tcp::default(), SubOptions::default());

        let mut sub2 = SubSocket::with_options(Tcp::default(), SubOptions::default());

        let addr = pub_socket.local_addr().unwrap();

        sub1.connect(addr).await.unwrap();
        tokio::time::sleep(Duration::from_millis(100)).await;
        assert_eq!(pub_socket.stats().active_clients(), 1);
        sub2.connect(addr).await.unwrap();
        tokio::time::sleep(Duration::from_millis(100)).await;
        assert_eq!(pub_socket.stats().active_clients(), 1);
    }
}