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

server: Optimize sending for SubscriptionSink::pipe_from_stream #901

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions core/src/server/rpc_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,13 +886,7 @@ impl SubscriptionSink {
return Ok(false);
}

// Only possible to trigger when the connection is dropped.
if self.is_closed() {
return Ok(false);
}

let msg = self.build_message(result)?;
Ok(self.inner.send_raw(msg).is_ok())
self.send_without_accept(result)
}

/// Reads data from the `stream` and sends back data on the subscription
Expand Down Expand Up @@ -976,7 +970,7 @@ impl SubscriptionSink {
match futures_util::future::select(stream_item, closed_fut).await {
// The app sent us a value to send back to the subscribers
Either::Left((Ok(Some(result)), next_closed_fut)) => {
match self.send(&result) {
match self.send_without_accept(&result) {
Ok(true) => (),
Ok(false) => {
break SubscriptionClosed::RemotePeerAborted;
Expand Down Expand Up @@ -1034,6 +1028,21 @@ impl SubscriptionSink {
self.inner.is_closed() || self.close_notify.is_none() || !self.is_active_subscription()
}

/// Send a message back to subscribers.
///
/// This is similar to the [`SubscriptionSink::send`], but it does not try to accept
/// the subscription prior to sending.
#[inline]
fn send_without_accept<T: Serialize>(&mut self, result: &T) -> Result<bool, serde_json::Error> {
// Only possible to trigger when the connection is dropped.
if self.is_closed() {
return Ok(false);
}

let msg = self.build_message(result)?;
Ok(self.inner.send_raw(msg).is_ok())
}

fn is_active_subscription(&self) -> bool {
match self.unsubscribe.as_ref() {
Some(unsubscribe) => unsubscribe.has_changed().is_ok(),
Expand Down