-
Notifications
You must be signed in to change notification settings - Fork 1
feat: use axum WS #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0558286
feat: use axum WS
prestwich 5a075d6
lint: clippy
prestwich 250eccb
fix: dep spec better spec good now
prestwich 3e90c18
feat: axum_ws tests
prestwich 1f494f0
lint: clippy
prestwich e3f3e14
nit: remove dead line
prestwich 8f4ce67
test: lower delay
prestwich 21920d0
chore: remove unused inports in examples
prestwich 87706ea
docs: expand them :)
prestwich 0f01a22
fix: with_handle
prestwich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
//! WebSocket connection manager for [`axum`] | ||
//! | ||
//! How this works: | ||
//! `axum` does not provide a connection pattern that allows us to iplement | ||
//! [`Listener`] or [`Connect`] directly. Instead, it uses a | ||
//! [`WebSocketUpgrade`] to upgrade a connection to a WebSocket. This means | ||
//! that we cannot use the [`Listener`] trait directly. Instead, we make a | ||
//! [`AxumWsCfg`] that will be the [`State`] for our handler. | ||
//! | ||
//! The [`ajj_websocket`] handler serves the role of the [`Listener`] in this | ||
//! case. | ||
//! | ||
//! [`Connect`]: crate::pubsub::Connect | ||
|
||
use crate::{ | ||
pubsub::{shared::ConnectionManager, Listener}, | ||
Router, | ||
}; | ||
use axum::{ | ||
extract::{ | ||
ws::{Message, WebSocket}, | ||
State, WebSocketUpgrade, | ||
}, | ||
response::Response, | ||
}; | ||
use bytes::Bytes; | ||
use futures_util::{ | ||
stream::{SplitSink, SplitStream}, | ||
SinkExt, Stream, StreamExt, | ||
}; | ||
use serde_json::value::RawValue; | ||
use std::{ | ||
convert::Infallible, | ||
pin::Pin, | ||
sync::Arc, | ||
task::{ready, Context, Poll}, | ||
}; | ||
use tokio::runtime::Handle; | ||
use tracing::debug; | ||
|
||
pub(crate) type SendHalf = SplitSink<WebSocket, Message>; | ||
pub(crate) type RecvHalf = SplitStream<WebSocket>; | ||
|
||
struct AxumListener; | ||
|
||
impl Listener for AxumListener { | ||
type RespSink = SendHalf; | ||
|
||
type ReqStream = WsJsonStream; | ||
|
||
type Error = Infallible; | ||
|
||
async fn accept(&self) -> Result<(Self::RespSink, Self::ReqStream), Self::Error> { | ||
unreachable!() | ||
} | ||
} | ||
|
||
/// Configuration details for WebSocket connections using [`axum::extract::ws`]. | ||
/// | ||
/// The main points of configuration are: | ||
/// - The runtime [`Handle`] on which to execute tasks, which can be set with | ||
/// [`Self::with_handle`]. | ||
/// - The notification buffer size per client, which can be set with | ||
/// [`Self::with_notification_buffer_per_client`]. See the [`crate::pubsub`] | ||
/// module documentation for more details. | ||
#[derive(Clone)] | ||
pub struct AxumWsCfg { | ||
inner: Arc<ConnectionManager>, | ||
} | ||
|
||
impl core::fmt::Debug for AxumWsCfg { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
f.debug_struct("AxumWsCfg") | ||
.field( | ||
"notification_buffer_per_client", | ||
&self.inner.notification_buffer_per_task, | ||
) | ||
.field("next_id", &self.inner.next_id) | ||
.finish() | ||
} | ||
} | ||
|
||
impl From<Router<()>> for AxumWsCfg { | ||
fn from(router: Router<()>) -> Self { | ||
Self::new(router) | ||
} | ||
} | ||
|
||
impl AxumWsCfg { | ||
/// Create a new [`AxumWsCfg`] with the given [`Router`]. | ||
pub fn new(router: Router<()>) -> Self { | ||
Self { | ||
inner: ConnectionManager::new(router).into(), | ||
} | ||
} | ||
|
||
fn into_inner(self) -> ConnectionManager { | ||
match Arc::try_unwrap(self.inner) { | ||
Ok(inner) => inner, | ||
Err(arc) => ConnectionManager { | ||
root_tasks: arc.root_tasks.clone(), | ||
next_id: arc.next_id.clone(), | ||
router: arc.router.clone(), | ||
notification_buffer_per_task: arc.notification_buffer_per_task, | ||
}, | ||
} | ||
} | ||
|
||
/// Set the handle on which to execute tasks. | ||
pub fn with_handle(self, handle: Handle) -> Self { | ||
Self { | ||
inner: self.into_inner().with_handle(handle).into(), | ||
} | ||
} | ||
|
||
/// Set the notification buffer size per client. | ||
pub fn with_notification_buffer_per_client( | ||
self, | ||
notification_buffer_per_client: usize, | ||
) -> Self { | ||
Self { | ||
inner: self | ||
.into_inner() | ||
.with_notification_buffer_per_client(notification_buffer_per_client) | ||
.into(), | ||
} | ||
} | ||
} | ||
|
||
/// Axum handler for WebSocket connections. Used to serve | ||
/// | ||
/// ```no_run | ||
/// # #[cfg(all(feature = "axum", feature = "pubsub"))] | ||
/// # use ajj::{Router, pubsub::{ajj_websocket, AxumWsCfg}}; | ||
/// # use std::sync::Arc; | ||
prestwich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// # { | ||
/// # async fn _main(router: Router<()>, axum: axum::Router<AxumWsCfg>) -> axum::Router<()>{ | ||
/// // The config object contains the tokio runtime handle, and the | ||
/// // notification buffer size. | ||
/// let cfg = AxumWsCfg::new(router); | ||
/// | ||
/// axum | ||
/// .route("/ws", axum::routing::any(ajj_websocket)) | ||
/// .with_state(cfg) | ||
/// # }} | ||
/// ``` | ||
pub async fn ajj_websocket(ws: WebSocketUpgrade, State(state): State<AxumWsCfg>) -> Response { | ||
ws.on_upgrade(move |ws| { | ||
let (sink, stream) = ws.split(); | ||
|
||
state | ||
.inner | ||
.handle_new_connection::<AxumListener>(stream.into(), sink); | ||
|
||
async {} | ||
}) | ||
} | ||
|
||
/// Simple stream adapter for extracting text from a [`WebSocket`]. | ||
#[derive(Debug)] | ||
struct WsJsonStream { | ||
inner: RecvHalf, | ||
complete: bool, | ||
} | ||
|
||
impl From<RecvHalf> for WsJsonStream { | ||
fn from(inner: RecvHalf) -> Self { | ||
Self { | ||
inner, | ||
complete: false, | ||
} | ||
} | ||
} | ||
|
||
impl WsJsonStream { | ||
/// Handle an incoming [`Message`] | ||
fn handle(&self, message: Message) -> Result<Option<Bytes>, &'static str> { | ||
match message { | ||
Message::Text(text) => Ok(Some(text.into())), | ||
Message::Close(Some(frame)) => { | ||
let s = "Received close frame with data"; | ||
let reason = format!("{} ({})", frame.reason, frame.code); | ||
debug!(%reason, "{}", &s); | ||
Err(s) | ||
} | ||
Message::Close(None) => { | ||
let s = "WS client has gone away"; | ||
debug!("{}", &s); | ||
Err(s) | ||
} | ||
_ => Ok(None), | ||
} | ||
} | ||
} | ||
|
||
impl Stream for WsJsonStream { | ||
type Item = Bytes; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
loop { | ||
if self.complete { | ||
return Poll::Ready(None); | ||
} | ||
|
||
let Some(Ok(msg)) = ready!(self.inner.poll_next_unpin(cx)) else { | ||
self.complete = true; | ||
return Poll::Ready(None); | ||
}; | ||
|
||
match self.handle(msg) { | ||
Ok(Some(item)) => return Poll::Ready(Some(item)), | ||
Ok(None) => continue, | ||
Err(_) => self.complete = true, | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl crate::pubsub::JsonSink for SendHalf { | ||
type Error = axum::Error; | ||
|
||
async fn send_json(&mut self, json: Box<RawValue>) -> Result<(), Self::Error> { | ||
self.send(Message::text(json.get())).await | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.