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
use serde::{Deserialize, Serialize};
/// Represents the configuration options for the server.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerOption {
/// The address of the server.
pub addr: String,
/// The name of the service.
pub service_name: String,
/// The verification key.
pub verify_key: String,
/// The timeout for requests in milliseconds.
pub request_out_time: u32,
/// The time to save the session in milliseconds.
pub session_save_time: u32,
/// Set the maximum number of concurrent connections. When the limit is reached,
/// accept will block until a slot is freed. Set to 0 (default) for unlimited connections.
#[serde(default)]
pub max_connections: usize,
/// Enable or disable TCP_NODELAY (Nagle's algorithm). When enabled,
/// small packets are sent immediately without delay. Default: false (Nagle's algorithm enabled).
#[serde(default)]
pub is_nodelay: bool,
}
impl ServerOption {
/// Creates a new `ServerOption` with the given address, service name, and verify key.
///
/// # Arguments
///
/// * `addr` - A string slice that holds the address of the server.
/// * `service_name` - A string slice that holds the name of the service.
/// * `verify_key` - A string slice that holds the verification key.
///
/// # Returns
///
/// A `ServerOption` instance with default values for `request_out_time` and `session_save_time`.
#[inline]
pub fn new(addr: &str, service_name: &str, verify_key: &str) -> ServerOption {
ServerOption {
addr: addr.to_string(),
service_name: service_name.to_string(),
verify_key: verify_key.to_string(),
request_out_time: 5000,
session_save_time: 5000,
max_connections: 0,
is_nodelay: false,
}
}
/// Sets the request timeout duration.
#[inline]
pub fn set_session_save_time(mut self, session_save_time: u32) -> Self {
self.session_save_time = session_save_time;
self
}
/// Sets the request timeout duration.
#[inline]
pub fn set_request_out_time(mut self, request_out_time: u32) -> Self {
self.request_out_time = request_out_time;
self
}
/// Sets the maximum number of concurrent connections.
///
/// When the limit is reached, accept will block until a slot is freed. Set to 0 (default) for unlimited connections.
#[inline]
pub fn set_max_connections(mut self, max_connections: usize) -> Self {
self.max_connections = max_connections;
self
}
/// Sets whether to enable TCP_NODELAY (Nagle's algorithm).
pub fn set_nodelay(mut self, is_nodelay: bool) -> Self {
self.is_nodelay = is_nodelay;
self
}
}