From 18ab7cc1a431d640ccfc682bf2478eeaa09350d7 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Tue, 6 Feb 2024 17:13:47 +0800 Subject: [PATCH] [ENG-1548] use in-memory instances when sending messages to cloud (#2057) * use in-memory instances when sending messages to cloud * comments --- core/src/cloud/sync/mod.rs | 9 +-------- core/src/cloud/sync/send.rs | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/core/src/cloud/sync/mod.rs b/core/src/cloud/sync/mod.rs index 9cf45d3b3..d0e5e71d4 100644 --- a/core/src/cloud/sync/mod.rs +++ b/core/src/cloud/sync/mod.rs @@ -24,14 +24,7 @@ pub async fn declare_actors(library: &Arc, node: &Arc) { let library = library.clone(); let node = node.clone(); - move || { - send::run_actor( - library.db.clone(), - library.id, - library.sync.clone(), - node.clone(), - ) - } + move || send::run_actor(library.id, library.sync.clone(), node.clone()) }, autorun, ) diff --git a/core/src/cloud/sync/send.rs b/core/src/cloud/sync/send.rs index e47c65fcb..4704c79dc 100644 --- a/core/src/cloud/sync/send.rs +++ b/core/src/cloud/sync/send.rs @@ -2,8 +2,6 @@ use super::CompressedCRDTOperations; use sd_cloud_api::RequestConfigProvider; use sd_core_sync::{GetOpsArgs, SyncMessage, NTP64}; -use sd_prisma::prisma::{instance, PrismaClient}; -use sd_utils::from_bytes_to_uuid; use uuid::Uuid; use std::{sync::Arc, time::Duration}; @@ -13,24 +11,22 @@ use tokio::time::sleep; use super::err_break; pub async fn run_actor( - db: Arc, library_id: Uuid, sync: Arc, cloud_api_config_provider: Arc, ) { loop { loop { - let instances = err_break!( - db.instance() - .find_many(vec![]) - .select(instance::select!({ pub_id })) - .exec() - .await - ) - .into_iter() - .map(|i| from_bytes_to_uuid(&i.pub_id)) - .collect::>(); + // all available instances will have a default timestamp from create_instance + let instances = sync + .timestamps + .read() + .await + .keys() + .cloned() + .collect::>(); + // obtains a lock on the timestamp collections for the instances we have let req_adds = err_break!( sd_cloud_api::library::message_collections::request_add( cloud_api_config_provider.get_request_config().await, @@ -44,6 +40,7 @@ pub async fn run_actor( use sd_cloud_api::library::message_collections::do_add; + // gets new operations for each instance to send to cloud for req_add in req_adds { let ops = err_break!( sync.get_ops(GetOpsArgs { @@ -83,6 +80,7 @@ pub async fn run_actor( break; } + // uses lock we acquired earlier to send the operations to the cloud err_break!( do_add( cloud_api_config_provider.get_request_config().await,