lynn_tcp 1.2.5

Lightweight asynchronous TCP framework
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use std::net::{SocketAddr, ToSocketAddrs};

use crate::const_config::{
    DEFAULT_ADDR, DEFAULT_CHECK_HEART_INTERVAL, DEFAULT_CHECK_HEART_TIMEOUT_TIME,
    DEFAULT_MAX_CONNECTIONS, DEFAULT_MAX_REACTOR_TASKPOOL_SIZE, DEFAULT_MESSAGE_HEADER_MARK,
    DEFAULT_MESSAGE_TAIL_MARK, DEFAULT_PROCESS_PERMIT_SIZE, DEFAULT_MAX_CONNECTIONS_PER_IP,
    DEFAULT_CONNECTION_RATE_LIMIT, DEFAULT_TCP_NODELAY, DEFAULT_TCP_KEEPALIVE_ENABLED,
    DEFAULT_TCP_KEEPALIVE_TIME_SECS, DEFAULT_READ_TIMEOUT_SECS, DEFAULT_WRITE_TIMEOUT_SECS,
    DEFAULT_RECV_BUFFER_SIZE, DEFAULT_SEND_BUFFER_SIZE,
};
use crate::{LynnError, Result};

/// Represents the configuration for the Lynn server.
///
/// This struct contains various configuration parameters for the server,
/// such as the IP address, channel size, maximum connections, thread pool size, etc.
#[cfg(feature = "server")]
pub struct LynnServerConfig<'a> {
    // The address of the server.
    pub(super) server_addr: SocketAddr,
    // The maximum number of connections for the server.
    server_max_connections: Option<&'a usize>,
    // The maximum number of threads for the server.
    server_max_reactor_taskpool_size: &'a usize,
    // The permit size for a single process.
    server_single_processs_permit: &'a usize,
    // The interval for checking heartbeats.
    server_check_heart_interval: &'a u64,
    // The timeout time for checking heartbeats.
    server_check_heart_timeout_time: &'a u64,
    // The mark for the message header.
    message_header_mark: &'a u16,
    // The mark for the message tail.
    message_tail_mark: &'a u16,
    // The maximum number of connections per IP address.
    server_max_connections_per_ip: &'a usize,
    // The connection rate limit (connections per second).
    server_connection_rate_limit: &'a u64,
    // The TCP_NODELAY setting.
    tcp_nodelay: &'a bool,
    // The TCP keep-alive enabled setting.
    tcp_keepalive_enabled: &'a bool,
    // The TCP keep-alive time in seconds.
    tcp_keepalive_time_secs: &'a u64,
    // The read timeout in seconds.
    read_timeout_secs: &'a u64,
    // The write timeout in seconds.
    write_timeout_secs: &'a u64,
    // The receive buffer size in bytes.
    recv_buffer_size: &'a usize,
    // The send buffer size in bytes.
    send_buffer_size: &'a usize,
}

/// Implementation for LynnServerConfig, providing methods to create and get the configuration.
///
/// This implementation includes a constructor for the default configuration and methods to get the configuration parameters.
impl<'a> LynnServerConfig<'a> {
    /// Creates a default LynnServerConfig instance.
    ///
    /// # Returns
    ///
    /// A default LynnServerConfig instance.
    pub(crate) fn default() -> Self {
        Self {
            server_addr: *DEFAULT_ADDR,
            server_max_connections: Some(&DEFAULT_MAX_CONNECTIONS),
            server_max_reactor_taskpool_size: &DEFAULT_MAX_REACTOR_TASKPOOL_SIZE,
            server_single_processs_permit: &DEFAULT_PROCESS_PERMIT_SIZE,
            server_check_heart_interval: &DEFAULT_CHECK_HEART_INTERVAL,
            server_check_heart_timeout_time: &DEFAULT_CHECK_HEART_TIMEOUT_TIME,
            message_header_mark: &DEFAULT_MESSAGE_HEADER_MARK,
            message_tail_mark: &DEFAULT_MESSAGE_TAIL_MARK,
            server_max_connections_per_ip: &DEFAULT_MAX_CONNECTIONS_PER_IP,
            server_connection_rate_limit: &DEFAULT_CONNECTION_RATE_LIMIT,
            tcp_nodelay: &DEFAULT_TCP_NODELAY,
            tcp_keepalive_enabled: &DEFAULT_TCP_KEEPALIVE_ENABLED,
            tcp_keepalive_time_secs: &DEFAULT_TCP_KEEPALIVE_TIME_SECS,
            read_timeout_secs: &DEFAULT_READ_TIMEOUT_SECS,
            write_timeout_secs: &DEFAULT_WRITE_TIMEOUT_SECS,
            recv_buffer_size: &DEFAULT_RECV_BUFFER_SIZE,
            send_buffer_size: &DEFAULT_SEND_BUFFER_SIZE,
        }
    }

    /// Gets the address of the server.
    ///
    /// # Returns
    ///
    /// The address of the server.
    pub(crate) fn get_server_addr(&self) -> String {
        self.server_addr.to_string()
    }

    /// Gets the permit size for a single process.
    ///
    /// # Returns
    ///
    /// The permit size for a single process.
    pub(crate) fn get_server_single_processs_permit(&self) -> &usize {
        self.server_single_processs_permit
    }

    /// Gets the interval for checking heartbeats.
    ///
    /// # Returns
    ///
    /// The interval for checking heartbeats.
    pub(crate) fn get_server_check_heart_interval(&self) -> &u64 {
        self.server_check_heart_interval
    }

    /// Gets the timeout time for checking heartbeats.
    ///
    /// # Returns
    ///
    /// The timeout time for checking heartbeats.
    pub(crate) fn get_server_check_heart_timeout_time(&self) -> &u64 {
        self.server_check_heart_timeout_time
    }

    /// Gets the maximum number of connections for the server.
    ///
    /// # Returns
    ///
    /// The maximum number of connections for the server.
    pub(crate) fn get_server_max_connections(&self) -> Option<&usize> {
        self.server_max_connections
    }

    pub(crate) fn get_server_max_reactor_taskpool_size(&self) -> &usize {
        self.server_max_reactor_taskpool_size
    }

    /// Gets the mark for the message header.
    ///
    /// # Returns
    ///
    /// The mark for the message header.
    pub(crate) fn get_message_header_mark(&self) -> &u16 {
        self.message_header_mark
    }

    /// Gets the mark for the message tail.
    ///
    /// # Returns
    ///
    /// The mark for the message tail.
    pub(crate) fn get_message_tail_mark(&self) -> &u16 {
        self.message_tail_mark
    }

    /// Gets the maximum number of connections per IP address.
    ///
    /// # Returns
    ///
    /// The maximum number of connections per IP address.
    pub(crate) fn get_server_max_connections_per_ip(&self) -> &usize {
        self.server_max_connections_per_ip
    }

    /// Gets the connection rate limit (connections per second).
    ///
    /// # Returns
    ///
    /// The connection rate limit.
    pub(crate) fn get_server_connection_rate_limit(&self) -> &u64 {
        self.server_connection_rate_limit
    }

    /// Gets the TCP_NODELAY setting.
    ///
    /// # Returns
    ///
    /// The TCP_NODELAY setting.
    pub(crate) fn get_tcp_nodelay(&self) -> &bool {
        self.tcp_nodelay
    }

    /// Gets the TCP keep-alive enabled setting.
    ///
    /// # Returns
    ///
    /// Whether TCP keep-alive is enabled.
    pub(crate) fn get_tcp_keepalive_enabled(&self) -> &bool {
        self.tcp_keepalive_enabled
    }

    /// Gets the TCP keep-alive time in seconds.
    ///
    /// # Returns
    ///
    /// The TCP keep-alive time.
    pub(crate) fn get_tcp_keepalive_time_secs(&self) -> &u64 {
        self.tcp_keepalive_time_secs
    }

    /// Gets the read timeout in seconds.
    ///
    /// # Returns
    ///
    /// The read timeout.
    pub(crate) fn get_read_timeout_secs(&self) -> &u64 {
        self.read_timeout_secs
    }

    /// Gets the write timeout in seconds.
    ///
    /// # Returns
    ///
    /// The write timeout.
    pub(crate) fn get_write_timeout_secs(&self) -> &u64 {
        self.write_timeout_secs
    }

    /// Gets the receive buffer size in bytes.
    ///
    /// # Returns
    ///
    /// The receive buffer size.
    pub(crate) fn get_recv_buffer_size(&self) -> &usize {
        self.recv_buffer_size
    }

    /// Gets the send buffer size in bytes.
    ///
    /// # Returns
    ///
    /// The send buffer size.
    pub(crate) fn get_send_buffer_size(&self) -> &usize {
        self.send_buffer_size
    }
}

/// A builder for creating a LynnServerConfig instance.
///
/// This struct provides a fluent interface for setting the configuration parameters.
#[cfg(feature = "server")]
pub struct LynnServerConfigBuilder<'a> {
    // The LynnServerConfig instance being built.
    pub lynn_config: LynnServerConfig<'a>,
}

/// Implementation for LynnServerConfigBuilder, providing methods to set the configuration parameters.
///
/// This implementation includes methods to set each configuration parameter and a method to build the final LynnServerConfig instance.
impl<'a> LynnServerConfigBuilder<'a> {
    /// Creates a new LynnServerConfigBuilder instance with the default configuration.
    ///
    /// # Returns
    ///
    /// A new LynnServerConfigBuilder instance.
    pub fn new() -> Self {
        Self {
            lynn_config: LynnServerConfig::default(),
        }
    }
    /// Sets the server's IPv4 address.
    ///
    /// # Parameters
    ///
    /// * `server_ipv4` - The IPv4 address of the server.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    #[deprecated(note = "use `with_addr`", since = "1.1.7")]
    pub fn with_server_ipv4(mut self, server_ipv4: &'a str) -> Result<Self> {
        let mut addr = server_ipv4
            .to_socket_addrs()
            .map_err(|e| LynnError::invalid_address(format!("Failed to parse address: {}", e)))?;
        self.lynn_config.server_addr = addr
            .next()
            .ok_or_else(|| LynnError::invalid_address("No addresses found"))?;
        Ok(self)
    }

    pub fn with_addr<T>(mut self, addr: T) -> Result<Self>
    where
        T: ToSocketAddrs,
    {
        self.lynn_config.server_addr = addr
            .to_socket_addrs()
            .map_err(|e| LynnError::invalid_address(format!("Failed to parse address: {}", e)))?
            .next()
            .ok_or_else(|| LynnError::invalid_address("No addresses found"))?;
        Ok(self)
    }

    /// Sets the permit size for a single process.
    ///
    /// # Parameters
    ///
    /// * `server_single_processs_permit` - The permit size for a single process.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_server_single_processs_permit(
        mut self,
        server_single_processs_permit: &'a usize,
    ) -> Self {
        self.lynn_config.server_single_processs_permit = server_single_processs_permit;
        self
    }

    /// Sets the interval for checking heartbeats.
    ///
    /// # Parameters
    ///
    /// * `server_check_heart_interval` - The interval for checking heartbeats.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_server_check_heart_interval(
        mut self,
        server_check_heart_interval: &'a u64,
    ) -> Self {
        self.lynn_config.server_check_heart_interval = server_check_heart_interval;
        self
    }

    /// Sets the timeout time for checking heartbeats.
    ///
    /// # Parameters
    ///
    /// * `server_check_heart_timeout_time` - The timeout time for checking heartbeats.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_server_check_heart_timeout_time(
        mut self,
        server_check_heart_timeout_time: &'a u64,
    ) -> Self {
        self.lynn_config.server_check_heart_timeout_time = server_check_heart_timeout_time;
        self
    }

    /// Sets the maximum number of connections for the server.
    ///
    /// # Parameters
    ///
    /// * `server_max_connections` - The maximum number of connections for the server.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_server_max_connections(
        mut self,
        server_max_connections: Option<&'a usize>,
    ) -> Self {
        self.lynn_config.server_max_connections = server_max_connections;
        self
    }

    /// Sets the maximum number of threads for the server.
    ///
    /// # Parameters
    ///
    /// * `server_max_threadpool_size` - The maximum number of threads for the server.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    #[deprecated(note = "use `with_server_max_taskpool_size`", since = "1.1.12")]
    pub fn with_server_max_threadpool_size(
        mut self,
        server_max_threadpool_size: &'a usize,
    ) -> Self {
        self.lynn_config.server_max_reactor_taskpool_size = server_max_threadpool_size;
        self
    }

    /// Sets the maximum number of taskpool for the server.
    /// This value determines the throughput of the entire service.
    ///
    /// # Parameters
    ///
    /// * `server_max_taskpool_size` - The maximum number of taskpool for the server.
    /// * `default` - The default value is 512.
    /// * `suggestion` - If the known user base is small and there is little data interaction,
    /// it can be set to <100.
    /// It is necessary to find a suitable value in the actual application environment,
    /// but it is not recommended to set it too small
    ///
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_server_max_taskpool_size(
        mut self,
        server_max_reactor_taskpool_size: &'a usize,
    ) -> Self {
        self.lynn_config.server_max_reactor_taskpool_size = server_max_reactor_taskpool_size;
        self
    }

    /// Builds the `LynnServerConfig` instance.
    ///
    /// # Returns
    ///
    /// The built `LynnServerConfig` instance.
    pub fn build(self) -> LynnServerConfig<'a> {
        self.lynn_config
    }

    /// Sets the mark for the message header.
    ///
    /// # Parameters
    ///
    /// * `msg_header_mark` - The mark for the message header.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_message_header_mark(mut self, msg_header_mark: &'a u16) -> Self {
        self.lynn_config.message_header_mark = msg_header_mark;
        self
    }

    /// Sets the mark for the message tail.
    ///
    /// # Parameters
    ///
    /// * `msg_tail_mark` - The mark for the message tail.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_message_tail_mark(mut self, msg_tail_mark: &'a u16) -> Self {
        self.lynn_config.message_tail_mark = msg_tail_mark;
        self
    }

    /// Sets the maximum number of connections per IP address.
    ///
    /// # Parameters
    ///
    /// * `max_connections_per_ip` - The maximum number of connections per IP.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_max_connections_per_ip(mut self, max_connections_per_ip: &'a usize) -> Self {
        self.lynn_config.server_max_connections_per_ip = max_connections_per_ip;
        self
    }

    /// Sets the connection rate limit (connections per second).
    /// Set to 0 to disable rate limiting.
    ///
    /// # Parameters
    ///
    /// * `connection_rate_limit` - The maximum number of connections per second.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_connection_rate_limit(mut self, connection_rate_limit: &'a u64) -> Self {
        self.lynn_config.server_connection_rate_limit = connection_rate_limit;
        self
    }

    /// Sets the TCP_NODELAY setting.
    ///
    /// # Parameters
    ///
    /// * `tcp_nodelay` - Whether to enable TCP_NODELAY.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_tcp_nodelay(mut self, tcp_nodelay: &'a bool) -> Self {
        self.lynn_config.tcp_nodelay = tcp_nodelay;
        self
    }

    /// Sets the TCP keep-alive enabled setting.
    ///
    /// # Parameters
    ///
    /// * `tcp_keepalive_enabled` - Whether to enable TCP keep-alive.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_tcp_keepalive_enabled(mut self, tcp_keepalive_enabled: &'a bool) -> Self {
        self.lynn_config.tcp_keepalive_enabled = tcp_keepalive_enabled;
        self
    }

    /// Sets the TCP keep-alive time in seconds.
    ///
    /// # Parameters
    ///
    /// * `tcp_keepalive_time_secs` - The TCP keep-alive time in seconds.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_tcp_keepalive_time_secs(mut self, tcp_keepalive_time_secs: &'a u64) -> Self {
        self.lynn_config.tcp_keepalive_time_secs = tcp_keepalive_time_secs;
        self
    }

    /// Sets the read timeout in seconds. Set to 0 to disable timeout.
    ///
    /// # Parameters
    ///
    /// * `read_timeout_secs` - The read timeout in seconds.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_read_timeout_secs(mut self, read_timeout_secs: &'a u64) -> Self {
        self.lynn_config.read_timeout_secs = read_timeout_secs;
        self
    }

    /// Sets the write timeout in seconds. Set to 0 to disable timeout.
    ///
    /// # Parameters
    ///
    /// * `write_timeout_secs` - The write timeout in seconds.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_write_timeout_secs(mut self, write_timeout_secs: &'a u64) -> Self {
        self.lynn_config.write_timeout_secs = write_timeout_secs;
        self
    }

    /// Sets the receive buffer size in bytes.
    ///
    /// # Parameters
    ///
    /// * `recv_buffer_size` - The receive buffer size in bytes.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_recv_buffer_size(mut self, recv_buffer_size: &'a usize) -> Self {
        self.lynn_config.recv_buffer_size = recv_buffer_size;
        self
    }

    /// Sets the send buffer size in bytes.
    ///
    /// # Parameters
    ///
    /// * `send_buffer_size` - The send buffer size in bytes.
    ///
    /// # Returns
    ///
    /// The updated `LynnServerConfigBuilder` instance.
    pub fn with_send_buffer_size(mut self, send_buffer_size: &'a usize) -> Self {
        self.lynn_config.send_buffer_size = send_buffer_size;
        self
    }
}