Spawn blocking I/O tasks on a dedicated thread

next
Sayan Nandan 3 years ago
parent 7d1b44a57f
commit 78e9441564

@ -243,6 +243,10 @@ impl CoreDB {
println!("{:#?}", self.acquire_read());
}
}
pub fn poison(&self) {
(*self.shared).table.write().poisoned = true;
}
/// Check if snapshotting is enabled
pub fn is_snapshot_enabled(&self) -> bool {

@ -98,6 +98,12 @@ impl FileLock {
// Now write to the file
self.file.write_all(bytes)
}
pub fn try_clone(&self) -> Result<Self> {
Ok(FileLock {
file: self.file.try_clone()?,
unlocked: self.unlocked,
})
}
}
impl Drop for FileLock {

@ -201,7 +201,22 @@ pub async fn bgsave_scheduler(
tokio::select! {
// Sleep until `duration` from the current time instant
_ = time::sleep_until(time::Instant::now() + duration) => {
if !handle.shared.run_bgsave(&mut file) {break;}
let clone_file = match file.try_clone() {
Ok(cloned_descriptor) => cloned_descriptor,
Err(e) => {
// failed to get a clone of the descriptor ugh
handle.poison();
log::error!("BGSAVE service failed to clone descriptor: '{}'", e);
continue;
}
};
let cloned_handle = handle.clone();
let continue_running = tokio::task::spawn_blocking(move || {
let mut owned_file = clone_file;
let owned_handle = cloned_handle;
owned_handle.shared.run_bgsave(&mut owned_file)
}).await.expect("Something caused the background service to panic");
if !continue_running {break;}
}
// Otherwise wait for a notification
_ = handle.shared.bgsave_task.notified() => {

Loading…
Cancel
Save