From 990cbb8126ed88d8642cc5096fddc69577fa6095 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Wed, 2 Aug 2023 08:39:46 +0100 Subject: [PATCH] Update tokio-tungstenite 0.20 (#2123) Co-authored-by: Jonas Platte --- axum/CHANGELOG.md | 4 ++- axum/Cargo.toml | 2 +- axum/src/extract/ws.rs | 36 +++++++++++++++++++++++--- examples/testing-websockets/Cargo.toml | 2 +- examples/websockets/Cargo.toml | 2 +- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 60067f5e6b..860c645fd9 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None. +- **added:** `WebSocketUpgrade::write_buffer_size` and `WebSocketUpgrade::max_write_buffer_size` +- **changed:** Deprecate `WebSocketUpgrade::max_send_queue` +- **change:** Update tokio-tungstenite to 0.20 # 0.6.19 (17. July, 2023) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index 0bb5d97a34..2634be47bc 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -61,7 +61,7 @@ serde_path_to_error = { version = "0.1.8", optional = true } serde_urlencoded = { version = "0.7", optional = true } sha1 = { version = "0.10", optional = true } tokio = { package = "tokio", version = "1.25.0", features = ["time"], optional = true } -tokio-tungstenite = { version = "0.19", optional = true } +tokio-tungstenite = { version = "0.20", optional = true } tracing = { version = "0.1", default-features = false, optional = true } [dependencies.tower-http] diff --git a/axum/src/extract/ws.rs b/axum/src/extract/ws.rs index c2c1d27977..26f28609ad 100644 --- a/axum/src/extract/ws.rs +++ b/axum/src/extract/ws.rs @@ -157,9 +157,39 @@ impl std::fmt::Debug for WebSocketUpgrade { } impl WebSocketUpgrade { - /// Set the size of the internal message send queue. - pub fn max_send_queue(mut self, max: usize) -> Self { - self.config.max_send_queue = Some(max); + /// Does nothing, instead use `max_write_buffer_size`. + #[deprecated] + pub fn max_send_queue(self, _: usize) -> Self { + self + } + + /// The target minimum size of the write buffer to reach before writing the data + /// to the underlying stream. + /// + /// The default value is 128 KiB. + /// + /// If set to `0` each message will be eagerly written to the underlying stream. + /// It is often more optimal to allow them to buffer a little, hence the default value. + /// + /// Note: [`flush`](SinkExt::flush) will always fully write the buffer regardless. + pub fn write_buffer_size(mut self, size: usize) -> Self { + self.config.write_buffer_size = size; + self + } + + /// The max size of the write buffer in bytes. Setting this can provide backpressure + /// in the case the write buffer is filling up due to write errors. + /// + /// The default value is unlimited. + /// + /// Note: The write buffer only builds up past [`write_buffer_size`](Self::write_buffer_size) + /// when writes to the underlying stream are failing. So the **write buffer can not + /// fill up if you are not observing write errors even if not flushing**. + /// + /// Note: Should always be at least [`write_buffer_size + 1 message`](Self::write_buffer_size) + /// and probably a little more depending on error handling strategy. + pub fn max_write_buffer_size(mut self, max: usize) -> Self { + self.config.max_write_buffer_size = max; self } diff --git a/examples/testing-websockets/Cargo.toml b/examples/testing-websockets/Cargo.toml index 7fc9a26a16..682f23aa06 100644 --- a/examples/testing-websockets/Cargo.toml +++ b/examples/testing-websockets/Cargo.toml @@ -9,4 +9,4 @@ axum = { path = "../../axum", features = ["ws"] } futures = "0.3" hyper = { version = "0.14", features = ["full"] } tokio = { version = "1.0", features = ["full"] } -tokio-tungstenite = "0.19" +tokio-tungstenite = "0.20" diff --git a/examples/websockets/Cargo.toml b/examples/websockets/Cargo.toml index d79c29b736..548e8d0009 100644 --- a/examples/websockets/Cargo.toml +++ b/examples/websockets/Cargo.toml @@ -10,7 +10,7 @@ futures = "0.3" futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] } headers = "0.3" tokio = { version = "1.0", features = ["full"] } -tokio-tungstenite = "0.19" +tokio-tungstenite = "0.20" tower = { version = "0.4", features = ["util"] } tower-http = { version = "0.4.0", features = ["fs", "trace"] } tracing = "0.1"