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

reducer to wait until subsciption broadcast finishes #776

Merged
merged 1 commit into from
Feb 1, 2024
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
15 changes: 12 additions & 3 deletions crates/core/src/subscription/module_subscription_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use futures::{stream::FuturesUnordered, Future, StreamExt};
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::Identity;
use tokio::sync::mpsc;
use tokio::sync::{mpsc, oneshot};

/// All of these commands (and likely any future ones) should all be in the same enum with the
/// same queue so that e.g. db updates from commit events and db updates from subscription
Expand All @@ -39,6 +39,8 @@ enum Command {
},
BroadcastCommitEvent {
event: ModuleEvent,
// channel for signaling that all subscriptions have been evaluated.
feedback_tx: oneshot::Sender<()>,
},
}

Expand Down Expand Up @@ -76,9 +78,13 @@ impl ModuleSubscriptionManager {
pub async fn broadcast_event(&self, client: Option<&ClientConnectionSender>, mut event: ModuleEvent) {
match event.status {
EventStatus::Committed(_) => {
let (tx, rx) = oneshot::channel();
self.tx
.send(Command::BroadcastCommitEvent { event })
.send(Command::BroadcastCommitEvent { event, feedback_tx: tx })
.expect("subscription actor panicked");
if (rx.await).is_err() {
log::error!("failed to receive acknowledgement of successful evaluation from subscription actor")
}
}
EventStatus::Failed(_) => {
if let Some(client) = client {
Expand Down Expand Up @@ -119,7 +125,10 @@ impl ModuleSubscriptionActor {
match command {
Command::AddSubscriber { sender, subscription } => self.add_subscription(sender, subscription).await?,
Command::RemoveSubscriber { client_id } => self.remove_subscriber(client_id),
Command::BroadcastCommitEvent { event } => self.broadcast_commit_event(event).await,
Command::BroadcastCommitEvent { event, feedback_tx } => {
self.broadcast_commit_event(event).await;
let _ = feedback_tx.send(());
}
}
Ok(())
}
Expand Down
Loading