Skip to content
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

Add publish_status and remove_status methods to WebSocketServer #192

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

eloff
Copy link
Contributor

@eloff eloff commented Feb 6, 2025

Relatively simple PR to add support for publishing status messages to all connected clients, and removing them by list of ids.

WS Spec

Current Python implementation

One thing I did here is treat status messages the same as log messages, in that when the queue is full it will drop older messages from the data plane queue.

@eloff eloff requested review from bryfox and gasmith February 6, 2025 19:04
Copy link

linear bot commented Feb 6, 2025

Copy link
Contributor

@gasmith gasmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG in general, though I wonder about the use of the data plane channel.

self.data_plane_tx.try_send(message).unwrap_or_else(|err| {
tracing::warn!("Failed to send status to client {}: {err}", self.addr)
});
self.send_data_lossy(message, MAX_SEND_RETRIES);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether these should be sent over the control plane channel instead, so that we don't drop them. I presume status messages are relatively low-frequency and high-importance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think so given our discussion we had on slack about dropping messages and using status messages over control plane to notify that.

How about a mix? control_plane for warning and above, data plane for info?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems pretty reasonable, sure.

}

/// Removes status messages by id from all clients.
pub fn remove_status(&self, status_ids: Vec<String>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make it a bit more generic?

Suggested change
pub fn remove_status(&self, status_ids: Vec<String>) {
pub fn remove_status<II, S>(&self, status_ids: II)
where
II: IntoIterator<Item = S>,
S: Into<String>,
{

Or maybe that's overkill.

Copy link
Contributor Author

@eloff eloff Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that initially, but we end up needing a Vec<String> anyway, and maybe I'm wrong but I think IntoIterator<String>.collect::<Vec<String>>() will create a new Vec, so then it's just worse in the common case where you already had a Vec.

Better to get the user to do the collect into a Vec if they have an iterator, or just pass it in if they have a Vec.

@@ -168,6 +168,22 @@ impl WebSocketServerHandle {
.await;
}

/// Publishes a status message to all clients.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add links to the ws-protocol spec for these.

Comment on lines +174 to +176
level: crate::websocket::StatusLevel,
message: impl Into<String>,
id: Option<impl Into<String>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's an optional argument, we might consider having the client construct the Status, and perhaps with a builder (imagine, e.g., Status::error("oh no").id("x")).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems slightly better, yes

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

Successfully merging this pull request may close these issues.

2 participants