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

Subscription Sink improve ergonomics of send API #944

Closed
lexnv opened this issue Nov 18, 2022 · 2 comments
Closed

Subscription Sink improve ergonomics of send API #944

lexnv opened this issue Nov 18, 2022 · 2 comments

Comments

@lexnv
Copy link
Contributor

lexnv commented Nov 18, 2022

The send API of the subscription sink can return the following:

  • Ok(true): message was sent
  • Ok(false): message not sent, because the client is disconnected
  • Err(err): Error regarding serialization failure

/// 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> {
// Cannot accept the subscription.
if let Err(SubscriptionAcceptRejectError::RemotePeerAborted) = self.accept() {
return Ok(false);
}
self.send_without_accept(result)
}

This is a bit tricky to work with, given that users might try to match
only errors (ie if let Err(err) = sink.send() { stop_service(); }).

To improve the API, ensure that on success Ok(()) is returned and on
failure have a dedicated error to differentiate between disconnect errors
or serde errors:

pub enum SubscriptionSinkError {
  ClientDisconnected,
  Serialization(serde::Error),
}
@niklasad1
Copy link
Member

niklasad1 commented Nov 21, 2022

Ideally, folks shouldn't don't have to bother with this API at all and use pipe_from_sink but there might some edge-cases where one want to use send.

I think we designed it just like that around the pipe_from_sink i.e, not to regard ConnectionClosed as an error.
In addition serde::Serialization errors are most likely bugs and should not really occur.

Anyway, I'm all good with regarding all send errors as an error instead and to make this API cleaner.

We could also have some specific error type such as https://docs.rs/futures/latest/futures/channel/mpsc/struct.TrySendError.html#

@niklasad1
Copy link
Member

Closed by #962

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

2 participants