From 54dfeb552752eb0e4d887af3a0affaa80863943d Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Thu, 25 Apr 2024 20:27:11 +0800 Subject: [PATCH] chore: stop retry when current plugin is going to destory (#495) --- Cargo.lock | 8 +++--- Cargo.toml | 8 +++--- libs/client-api/src/collab_sync/plugin.rs | 34 +++++++++++++++++++++-- tests/workspace/template_test.rs | 2 +- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5dce7b78..5c11e220 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1648,7 +1648,7 @@ dependencies = [ [[package]] name = "collab" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=f8930ed1b19b65dd7b890df2b0db54048141e8c4#f8930ed1b19b65dd7b890df2b0db54048141e8c4" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=851239880d8aca9e07abb47d96a4f8ff532dd930#851239880d8aca9e07abb47d96a4f8ff532dd930" dependencies = [ "anyhow", "async-trait", @@ -1672,7 +1672,7 @@ dependencies = [ [[package]] name = "collab-document" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=f8930ed1b19b65dd7b890df2b0db54048141e8c4#f8930ed1b19b65dd7b890df2b0db54048141e8c4" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=851239880d8aca9e07abb47d96a4f8ff532dd930#851239880d8aca9e07abb47d96a4f8ff532dd930" dependencies = [ "anyhow", "collab", @@ -1691,7 +1691,7 @@ dependencies = [ [[package]] name = "collab-entity" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=f8930ed1b19b65dd7b890df2b0db54048141e8c4#f8930ed1b19b65dd7b890df2b0db54048141e8c4" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=851239880d8aca9e07abb47d96a4f8ff532dd930#851239880d8aca9e07abb47d96a4f8ff532dd930" dependencies = [ "anyhow", "bytes", @@ -1706,7 +1706,7 @@ dependencies = [ [[package]] name = "collab-folder" version = "0.1.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=f8930ed1b19b65dd7b890df2b0db54048141e8c4#f8930ed1b19b65dd7b890df2b0db54048141e8c4" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=851239880d8aca9e07abb47d96a4f8ff532dd930#851239880d8aca9e07abb47d96a4f8ff532dd930" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 4399c882..7088c37e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -209,10 +209,10 @@ debug = true # will be removed when using yrs 0.18.2 that expose pendings yrs = { git = "https://github.com/appflowy/y-crdt", rev = "3f25bb510ca5274e7657d3713fbed41fb46b4487" } -collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "f8930ed1b19b65dd7b890df2b0db54048141e8c4" } -collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "f8930ed1b19b65dd7b890df2b0db54048141e8c4" } -collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "f8930ed1b19b65dd7b890df2b0db54048141e8c4" } -collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "f8930ed1b19b65dd7b890df2b0db54048141e8c4" } +collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "851239880d8aca9e07abb47d96a4f8ff532dd930" } +collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "851239880d8aca9e07abb47d96a4f8ff532dd930" } +collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "851239880d8aca9e07abb47d96a4f8ff532dd930" } +collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "851239880d8aca9e07abb47d96a4f8ff532dd930" } [features] ai_enable = [] diff --git a/libs/client-api/src/collab_sync/plugin.rs b/libs/client-api/src/collab_sync/plugin.rs index 10898adb..3cecd90f 100644 --- a/libs/client-api/src/collab_sync/plugin.rs +++ b/libs/client-api/src/collab_sync/plugin.rs @@ -17,11 +17,12 @@ use futures_util::SinkExt; use std::future::Future; use std::pin::Pin; +use std::sync::atomic::AtomicBool; use std::sync::{Arc, Weak}; use std::time::Duration; use tokio::time::sleep; use tokio_retry::strategy::FixedInterval; -use tokio_retry::{Action, Retry}; +use tokio_retry::{Action, Condition, RetryIf}; use tokio_stream::StreamExt; use tracing::{error, trace}; use yrs::updates::encoder::Encode; @@ -33,12 +34,18 @@ pub struct SyncPlugin { #[allow(dead_code)] channel: Option>, collab: Weak, + is_destroyed: Arc, } impl Drop for SyncPlugin { fn drop(&mut self) { #[cfg(feature = "sync_verbose_log")] trace!("Drop sync plugin: {}", self.object.object_id); + + // when the plugin is dropped, set the is_destroyed flag to true + self + .is_destroyed + .store(true, std::sync::atomic::Ordering::SeqCst); } } @@ -124,6 +131,7 @@ where object, channel, collab, + is_destroyed: Arc::new(Default::default()), } } } @@ -143,8 +151,12 @@ where collab: self.collab.clone(), }; + let condition = InitSyncRetryCondition { + is_plugin_destroyed: self.is_destroyed.clone(), + }; + tokio::spawn(async move { - if let Err(err) = Retry::spawn(retry_strategy, action).await { + if let Err(err) = RetryIf::spawn(retry_strategy, action, condition).await { error!("Failed to start init sync: {}", err); } }); @@ -218,6 +230,12 @@ where } }); } + + fn destroy(&self) { + self + .is_destroyed + .store(true, std::sync::atomic::Ordering::SeqCst); + } } #[derive(Clone, Debug)] @@ -297,3 +315,15 @@ where }) } } + +pub(crate) struct InitSyncRetryCondition { + is_plugin_destroyed: Arc, +} +impl Condition for InitSyncRetryCondition { + fn should_retry(&mut self, _error: &anyhow::Error) -> bool { + // Only retry if the plugin is not destroyed + !self + .is_plugin_destroyed + .load(std::sync::atomic::Ordering::SeqCst) + } +} diff --git a/tests/workspace/template_test.rs b/tests/workspace/template_test.rs index f4478534..e60efbcc 100644 --- a/tests/workspace/template_test.rs +++ b/tests/workspace/template_test.rs @@ -9,7 +9,7 @@ async fn get_user_default_workspace_test() { let test_client = TestClient::new_user().await; let folder = test_client.get_user_folder().await; - let views = folder.get_workspace_views(); + let views = folder.get_views_belong_to(&test_client.workspace_id().await); assert_eq!(views.len(), 1); assert_eq!(views[0].name, "Getting started"); }