Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion crates/y-sweet-core/src/sync_kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct SyncKv {
key: String,
dirty: AtomicBool,
dirty_callback: Box<dyn Fn() + Send + Sync>,
shutdown: AtomicBool,
}

impl SyncKv {
Expand Down Expand Up @@ -44,11 +45,12 @@ impl SyncKv {
key,
dirty: AtomicBool::new(false),
dirty_callback: Box::new(callback),
shutdown: AtomicBool::new(false),
})
}

fn mark_dirty(&self) {
if !self.dirty.load(Ordering::Relaxed) {
if !self.dirty.load(Ordering::Relaxed) && !self.shutdown.load(Ordering::SeqCst) {
self.dirty.store(true, Ordering::Relaxed);
(self.dirty_callback)();
}
Expand Down Expand Up @@ -88,6 +90,16 @@ impl SyncKv {
pub fn is_empty(&self) -> bool {
self.data.lock().unwrap().is_empty()
}

pub fn is_shutdown(&self) -> bool {
self.shutdown.load(Ordering::SeqCst)
}

pub fn shutdown(&self) {
self.shutdown.store(true, Ordering::SeqCst);
// Call the callback one last time to wake up the persistence worker
(self.dirty_callback)();
}
}

impl<'d> DocOps<'d> for SyncKv {}
Expand Down
7 changes: 7 additions & 0 deletions crates/y-sweet/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ impl Server {

if checkpoints_without_refs >= 2 {
tracing::info!("GCing doc");
if let Some(doc) = docs.get(&doc_id) {
doc.sync_kv().shutdown();
}

docs.remove(&doc_id);
break;
}
Expand All @@ -229,6 +233,9 @@ impl Server {
let is_done = tokio::select! {
v = recv.recv() => v.is_none(),
_ = cancellation_token.cancelled() => true,
_ = tokio::time::sleep(checkpoint_freq) => {
sync_kv.is_shutdown()
}
};

tracing::info!("Received signal. done: {}", is_done);
Expand Down
Loading