Skip to content

docs(axum): recommend actor pattern for concurrent WebSocket read/write - #3842

Open
0xendale wants to merge 1 commit into
tokio-rs:mainfrom
0xendale:feat/docs
Open

docs(axum): recommend actor pattern for concurrent WebSocket read/write#3842
0xendale wants to merge 1 commit into
tokio-rs:mainfrom
0xendale:feat/docs

Conversation

@0xendale

Copy link
Copy Markdown

Motivation

The WebSocket module docs suggest StreamExt::split as the way to read and write concurrently. Splitting wraps each half in a lock that must be acquired on every operation, which adds avoidable overhead. Upstream issue #3793 proposes highlighting the actor pattern as the higher-performance alternative, and #3786 already laid the groundwork by documenting that WebSocket::recv is cancel safe.

Solution

Rework the "Read and write concurrently" section in axum/src/extract/ws.rs:

  • Lead with the actor pattern: a single task owns the socket and uses tokio::select! to race WebSocket::recv (cancel safe) against an mpsc channel that other tasks use to send messages to the client. Links to Actors with Tokio for background.
  • Keep StreamExt::split as a documented alternative, with a note about its per-operation locking overhead.

Both examples are compile-checked doc tests. Verified with cargo test -p axum --all-features --doc ws and cargo doc using CI's rustdoc flags (-D rustdoc::all -A rustdoc::private-doc-tests); the test-only hidden line does not appear in the rendered docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant