1use std::time::Duration;
2
3use tokio_tungstenite::tungstenite::protocol::WebSocketConfig;
4
5use crate::{
6 WsIoServer,
7 config::WsIoServerConfig,
8 core::packet::codecs::WsIoPacketCodec,
9 runtime::WsIoServerRuntime,
10};
11
12pub struct WsIoServerBuilder {
20 config: WsIoServerConfig,
21}
22
23impl WsIoServerBuilder {
24 pub(crate) fn new() -> Self {
25 Self {
26 config: WsIoServerConfig {
27 broadcast_concurrency_limit: 512,
28 http_request_upgrade_timeout: Duration::from_secs(3),
29 init_request_handler_timeout: Duration::from_secs(3),
30 init_response_handler_timeout: Duration::from_secs(3),
31 init_response_timeout: Duration::from_secs(5),
32 middleware_execution_timeout: Duration::from_secs(2),
33 on_close_handler_timeout: Duration::from_secs(2),
34 on_connect_handler_timeout: Duration::from_secs(3),
35 packet_codec: WsIoPacketCodec::SerdeJson,
36 request_path: "/ws.io".into(),
37 websocket_config: WebSocketConfig::default()
38 .max_frame_size(Some(8 * 1024 * 1024))
39 .max_message_size(Some(16 * 1024 * 1024))
40 .max_write_buffer_size(2 * 1024 * 1024)
41 .read_buffer_size(8 * 1024)
42 .write_buffer_size(8 * 1024),
43 },
44 }
45 }
46
47 pub fn broadcast_concurrency_limit(mut self, broadcast_concurrency_limit: usize) -> Self {
54 self.config.broadcast_concurrency_limit = broadcast_concurrency_limit;
55 self
56 }
57
58 pub fn build(self) -> WsIoServer {
60 WsIoServer(WsIoServerRuntime::new(self.config))
61 }
62
63 pub fn http_request_upgrade_timeout(mut self, duration: Duration) -> Self {
69 self.config.http_request_upgrade_timeout = duration;
70 self
71 }
72
73 pub fn init_request_handler_timeout(mut self, duration: Duration) -> Self {
78 self.config.init_request_handler_timeout = duration;
79 self
80 }
81
82 pub fn init_response_handler_timeout(mut self, duration: Duration) -> Self {
87 self.config.init_response_handler_timeout = duration;
88 self
89 }
90
91 pub fn init_response_timeout(mut self, duration: Duration) -> Self {
96 self.config.init_response_timeout = duration;
97 self
98 }
99
100 pub fn middleware_execution_timeout(mut self, duration: Duration) -> Self {
105 self.config.middleware_execution_timeout = duration;
106 self
107 }
108
109 pub fn on_close_handler_timeout(mut self, duration: Duration) -> Self {
114 self.config.on_close_handler_timeout = duration;
115 self
116 }
117
118 pub fn on_connect_handler_timeout(mut self, duration: Duration) -> Self {
124 self.config.on_connect_handler_timeout = duration;
125 self
126 }
127
128 pub fn packet_codec(mut self, packet_codec: WsIoPacketCodec) -> Self {
133 self.config.packet_codec = packet_codec;
134 self
135 }
136
137 pub fn request_path(mut self, request_path: impl AsRef<str>) -> Self {
143 self.config.request_path = request_path.as_ref().into();
144 self
145 }
146
147 pub fn websocket_config(mut self, websocket_config: WebSocketConfig) -> Self {
153 self.config.websocket_config = websocket_config;
154 self
155 }
156
157 pub fn websocket_config_mut<F: FnOnce(&mut WebSocketConfig)>(mut self, f: F) -> Self {
162 f(&mut self.config.websocket_config);
163 self
164 }
165}
166
167#[cfg(test)]
168mod tests {
169 use std::time::Duration;
170
171 use super::*;
172
173 #[test]
174 fn test_builder_configuration_chaining() {
175 let server = WsIoServer::builder()
176 .broadcast_concurrency_limit(1024)
177 .http_request_upgrade_timeout(Duration::from_millis(750))
178 .init_request_handler_timeout(Duration::from_secs(1))
179 .init_response_handler_timeout(Duration::from_secs(2))
180 .init_response_timeout(Duration::from_secs(3))
181 .middleware_execution_timeout(Duration::from_secs(4))
182 .on_close_handler_timeout(Duration::from_secs(5))
183 .on_connect_handler_timeout(Duration::from_secs(6))
184 .packet_codec(WsIoPacketCodec::Msgpack)
185 .request_path("/custom")
186 .websocket_config_mut(|config| {
187 *config = config.max_frame_size(Some(999));
188 })
189 .build();
190
191 let config = &server.0.config;
193 assert_eq!(config.broadcast_concurrency_limit, 1024);
194 assert_eq!(config.http_request_upgrade_timeout, Duration::from_millis(750));
195 assert_eq!(config.init_request_handler_timeout, Duration::from_secs(1));
196 assert_eq!(config.init_response_handler_timeout, Duration::from_secs(2));
197 assert_eq!(config.init_response_timeout, Duration::from_secs(3));
198 assert_eq!(config.middleware_execution_timeout, Duration::from_secs(4));
199 assert_eq!(config.on_close_handler_timeout, Duration::from_secs(5));
200 assert_eq!(config.on_connect_handler_timeout, Duration::from_secs(6));
201 assert!(matches!(config.packet_codec, WsIoPacketCodec::Msgpack));
202 assert_eq!(config.request_path, "/custom");
203 assert_eq!(config.websocket_config.max_frame_size, Some(999));
204 }
205
206 #[test]
207 fn test_builder_websocket_config_override() {
208 let config = WebSocketConfig::default().max_frame_size(Some(42));
209 let server = WsIoServer::builder().websocket_config(config).build();
210 assert_eq!(server.0.config.websocket_config.max_frame_size, Some(42));
211 }
212}