fix: use database to publish all events
This fixes a race condition where a publisher might send an event, and immediately after issue a subscription for the same event ID. Prior to this change, that event would have been published on the broadcast channel (and ignored by our publisher, because they had not yet issued the subscription), but not yet committed to the database. Their subscription would trigger a database query which would return zero results. Therefore, they would never see the event they published. The noscl tool is one client that would suffer from this. Now, all events are broadcast only after they exist in the database, so a late subscription will always return the event.
This commit is contained in:
@@ -67,6 +67,7 @@ CREATE INDEX IF NOT EXISTS pubkey_ref_index ON pubkey_ref(referenced_pubkey);
|
||||
/// Spawn a database writer that persists events to the SQLite store.
|
||||
pub async fn db_writer(
|
||||
mut event_rx: tokio::sync::mpsc::Receiver<Event>,
|
||||
bcast_tx: tokio::sync::broadcast::Sender<Event>,
|
||||
) -> tokio::task::JoinHandle<Result<()>> {
|
||||
task::spawn_blocking(move || {
|
||||
let mut conn = Connection::open_with_flags(
|
||||
@@ -94,6 +95,8 @@ pub async fn db_writer(
|
||||
info!("nothing inserted (dupe?)");
|
||||
} else {
|
||||
info!("persisted event: {}", event.get_event_id_prefix());
|
||||
// send this out to all clients
|
||||
bcast_tx.send(event.clone()).ok();
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
|
||||
Reference in New Issue
Block a user