Skip to main content

ntex/ws/
sink.rs

1#![allow(clippy::unused_async)]
2use std::rc::Rc;
3
4use crate::codec::{Decoder, Encoder};
5use crate::util::{BytePages, BytesMut};
6use crate::{Cfg, io::IoRef, io::Waiter, rt, time::sleep, util::select, ws};
7
8#[derive(Clone, Debug)]
9/// A cloneable handle for sending messages over a WebSocket connection.
10///
11/// Clones share the codec state. The sink is also the connection's codec, so
12/// the dispatcher and all sinks see the same state, e.g. once a close message
13/// is sent by either of them, no more messages can be sent.
14pub struct WsSink(Rc<WsSinkInner>);
15
16#[derive(Debug)]
17struct WsSinkInner {
18    io: IoRef,
19    codec: ws::Codec,
20    cfg: Cfg<ws::WsClientConfig>,
21}
22
23impl WsSink {
24    pub(crate) fn new(io: IoRef, codec: ws::Codec, cfg: Cfg<ws::WsClientConfig>) -> Self {
25        Self(Rc::new(WsSinkInner { io, codec, cfg }))
26    }
27
28    /// Returns the underlying I/O handle.
29    pub fn io(&self) -> &IoRef {
30        &self.0.io
31    }
32
33    /// Returns the codec, it holds the state shared by all clones.
34    pub(crate) fn codec(&self) -> &ws::Codec {
35        &self.0.codec
36    }
37
38    /// Returns `true` after a close message has been sent through this sink.
39    pub(crate) fn is_closed(&self) -> bool {
40        self.0.codec.is_closed()
41    }
42
43    pub(crate) fn start_close_timeout(&self) {
44        if self.0.cfg.close_timeout.non_zero() {
45            let io = self.0.io.clone();
46            let close_timeout = self.0.cfg.close_timeout;
47            rt::spawn_detached(async move {
48                select(sleep(close_timeout), io.on_disconnect()).await;
49                if io.is_active() {
50                    io.close();
51                }
52            });
53        }
54    }
55
56    /// Encodes and queues a message for the peer.
57    ///
58    /// Data messages (text, binary and continuation) wait while write
59    /// back-pressure is enabled, until the write buffer can accept more
60    /// output. Control messages are queued immediately.
61    ///
62    /// Sending a close message starts the closing handshake. The connection
63    /// remains open for the peer's close response and is shut down when the
64    /// configured closing-handshake timeout expires.
65    pub async fn send(&self, item: ws::Message) -> Result<(), ws::error::ProtocolError> {
66        let close = matches!(item, ws::Message::Close(_));
67
68        if matches!(
69            item,
70            ws::Message::Text(_) | ws::Message::Binary(_) | ws::Message::Continuation(_)
71        ) {
72            // a closed connection drops the message in `encode`, as before
73            let _ = self.0.io.write_ready().await;
74        }
75
76        if let Err(e) = self.0.io.encode(item, &self.0.codec) {
77            Err(e)
78        } else {
79            if close {
80                self.start_close_timeout();
81            }
82            Ok(())
83        }
84    }
85
86    /// Returns a future that resolves when the connection is disconnected.
87    pub fn on_disconnect(&self) -> Waiter<'static> {
88        self.0.io.on_disconnect()
89    }
90}
91
92impl Encoder for WsSink {
93    type Item = ws::Message;
94    type Error = ws::error::ProtocolError;
95
96    fn encode(&self, item: ws::Message, dst: &mut BytePages) -> Result<(), Self::Error> {
97        self.0.codec.encode(item, dst)
98    }
99}
100
101impl Decoder for WsSink {
102    type Item = ws::Frame;
103    type Error = ws::error::ProtocolError;
104
105    fn decode(&self, src: &mut BytesMut) -> Result<Option<ws::Frame>, Self::Error> {
106        self.0.codec.decode(src)
107    }
108
109    fn decode_eof(&self, src: &mut BytesMut) -> Result<Option<ws::Frame>, Self::Error> {
110        self.0.codec.decode_eof(src)
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    use crate::{SharedCfg, io::Io, testing::IoTest, time::Millis, time::timeout};
118
119    /// STATEMENT: WebSocket sink clones share close state and reject data after a clone encodes close.
120    ///
121    /// HOW:
122    /// - Encode close through a cloned sink, then inspect the original and attempt to send text.
123    #[crate::rt_test]
124    async fn clones_share_codec_state() {
125        let (client, server) = IoTest::create();
126        client.remote_buffer_cap(4096);
127        let io = Io::new(server, SharedCfg::new("WS-TEST"));
128        let sink = WsSink::new(io.get_ref(), ws::Codec::new(), io.shared().get());
129        let sink2 = sink.clone();
130
131        // a close message encoded by the dispatcher, through a clone
132        let mut dst = BytePages::default();
133        sink2.encode(ws::Message::Close(None), &mut dst).unwrap();
134        assert!(sink.is_closed());
135        assert!(matches!(
136            sink.send(ws::Message::Text("t".into())).await,
137            Err(ws::error::ProtocolError::Closed)
138        ));
139    }
140
141    /// STATEMENT: Write backpressure delays data sends but allows control frames until transport capacity returns.
142    ///
143    /// HOW:
144    /// - Block peer writes, exceed the high watermark, send a ping, and observe a pending text send.
145    /// - Restore peer capacity and verify the blocked send completes within a timeout.
146    #[crate::rt_test]
147    async fn send_waits_for_write_backpressure() {
148        let (client, server) = IoTest::create();
149        client.remote_buffer_cap(0);
150        let cfg =
151            SharedCfg::new("WS-TEST").add(crate::io::IoConfig::new().set_write_backpressure(64));
152        let io = Io::new(server, cfg);
153        let sink = WsSink::new(io.get_ref(), ws::Codec::new(), io.shared().get());
154
155        sink.send(ws::Message::Binary(vec![0; 128].into()))
156            .await
157            .unwrap();
158        assert!(io.is_wr_backpressure());
159
160        // control messages are not delayed
161        sink.send(ws::Message::Ping("p".into())).await.unwrap();
162
163        let sent = std::rc::Rc::new(std::cell::Cell::new(false));
164        let (sink2, sent2) = (sink.clone(), sent.clone());
165        let handle = rt::spawn(async move {
166            sink2.send(ws::Message::Text("t".into())).await.unwrap();
167            sent2.set(true);
168        });
169        sleep(Millis(50)).await;
170        assert!(!sent.get());
171
172        // the peer reads, the write buffer drains
173        client.remote_buffer_cap(4096);
174        let _ = client.read().await;
175        timeout(Millis(1000), handle)
176            .await
177            .expect("send was not released")
178            .unwrap();
179        assert!(sent.get());
180    }
181
182    /// STATEMENT: Disconnecting a backpressured transport releases a waiting WebSocket send.
183    ///
184    /// HOW:
185    /// - Saturate the write buffer, terminate the transport from another task, and bound send completion by a timeout.
186    #[crate::rt_test]
187    async fn send_on_disconnect_does_not_wait() {
188        let (client, server) = IoTest::create();
189        client.remote_buffer_cap(0);
190        let cfg =
191            SharedCfg::new("WS-TEST").add(crate::io::IoConfig::new().set_write_backpressure(64));
192        let io = Io::new(server, cfg);
193        let sink = WsSink::new(io.get_ref(), ws::Codec::new(), io.shared().get());
194
195        sink.send(ws::Message::Binary(vec![0; 128].into()))
196            .await
197            .unwrap();
198        assert!(io.is_wr_backpressure());
199
200        let (sink2, io2) = (sink.clone(), io.get_ref());
201        rt::spawn(async move {
202            sleep(Millis(20)).await;
203            io2.terminate();
204        });
205        timeout(Millis(1000), sink2.send(ws::Message::Text("t".into())))
206            .await
207            .expect("send was not released")
208            .unwrap();
209    }
210
211    /// STATEMENT: An unanswered WebSocket close keeps the connection briefly active before its configured timeout closes it.
212    ///
213    /// HOW:
214    /// - Send close, check immediate transport activity, and wait with an outer timeout for shutdown.
215    /// - Verify elapsed time is at least the configured close timeout.
216    #[crate::rt_test]
217    async fn close_timeout() {
218        let (client, server) = IoTest::create();
219        client.remote_buffer_cap(4096);
220        let cfg =
221            SharedCfg::new("WS-TEST").add(ws::WsClientConfig::new().set_close_timeout(Millis(50)));
222        let io = Io::new(server, cfg);
223        let sink = WsSink::new(io.get_ref(), ws::Codec::new(), io.shared().get());
224
225        let start = std::time::Instant::now();
226        sink.send(ws::Message::Close(None)).await.unwrap();
227        assert!(!client.is_server_dropped());
228        assert!(sink.io().is_active());
229
230        // a late timer wakeup can run this task before the close timeout task
231        timeout(Millis(1000), async {
232            while sink.io().is_active() {
233                sleep(Millis(10)).await;
234            }
235        })
236        .await
237        .expect("close timeout did not close the connection");
238        assert!(start.elapsed() >= std::time::Duration::from_millis(50));
239    }
240}