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

How to get notified on unsubscribe / drop connection #990

Closed
xlc opened this issue Jan 26, 2023 · 5 comments
Closed

How to get notified on unsubscribe / drop connection #990

xlc opened this issue Jan 26, 2023 · 5 comments

Comments

@xlc
Copy link
Contributor

xlc commented Jan 26, 2023

close_notify: Option<SubscriptionPermit>,

I can't find any public API to expose this to allow me cleanup resources on drop connection. Also no public API to notify unsubscribe as well.

@xlc xlc changed the title How to get notified on unsubscribe How to get notified on unsubscribe / drop connection Jan 26, 2023
@niklasad1
Copy link
Member

niklasad1 commented Jan 26, 2023

There is only

pub fn is_closed(&self) -> bool {
,

I guess we could have an async fn closed I have added that in a pending PR to add backpressure.

Thus, it's on it's way but I you use pipe_from_stream it will return once the subscription is closed but assuming you need more low-level API.

@lexnv
Copy link
Contributor

lexnv commented Jan 26, 2023

An alternative to that, depending on your use-case, would be to interact with the subscription.

Let's presume the client has disconnected or called the unsubscribed method.
Presume the server doesn't use the is_closed or any other future to detect the disconnect, then the server will eventually try to submit a message to the subscription. This is where the server receives Ok(false) as a result of that action.

/// Send a message back to subscribers.
///
/// Returns
/// - `Ok(true)` if the message could be send.
/// - `Ok(false)` if the sink was closed (either because the subscription was closed or the connection was terminated),
/// or the subscription could not be accepted.
/// - `Err(err)` if the message could not be serialized.
pub fn send<T: Serialize>(&mut self, result: &T) -> Result<bool, serde_json::Error> {

To that regard, there is also this suggestion in discussion: #944, which would make things a bit more explicit to the user. Currently, you can shoot yourself in the foot with a code of those lines:

loop {
  match s.send() {
    Ok(_) => // if it is false, then we should stop.
  ..
}}

@xlc
Copy link
Contributor Author

xlc commented Jan 26, 2023

My use case is a JSON RPC proxy that forwards subscriptions to upstream server. So I will want to unsubscribe from upstream as soon as the downstream disconnect/unsubscribe.

So I don't want to use the sync closed method because when should I check it? and dont' want to only drop subscription on failed to send because that will just be a waste of resources on both proxy server and upstream server.

@niklasad1
Copy link
Member

Yeah, that makes sense we should provide an async function that returns once this connection is closed or subscription is canceled.

It's useful for folks to build their own async primitives instead of using pipe_from_stream

My PR #962 will enable that.

@niklasad1
Copy link
Member

Closed by #962

We have now introduced SubscriptionSink::closed that notifies once the subscription is closed.

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

No branches or pull requests

3 participants