perf(server): offload sync disk/db IO to tokio blocking thread pool
Makes apply_sync_write, write_to_local_delta, and store modify truly async, preventing the mcp-memory-server from locking the tokio executor during disk IO
This commit is contained in:
1 parent
1fd1d119e6
commit
248799ca5d
5 files changed
+105
-40
No files matched your search
+7
-2
@@ -10,7 +10,7 @@ pub struct Store<T> {
|
||||
pub cache: RwLock<T>,
|
||||
}
|
||||
|
||||
impl<T: DeserializeOwned + Default + Serialize + Clone> Store<T> {
|
||||
impl<T: DeserializeOwned + Default + Serialize + Clone + Send + 'static> Store<T> {
|
||||
pub fn new(key: &str, db: Arc<Database>) -> Self {
|
||||
let initial_data = Self::load_from_db(key, &db);
|
||||
Self {
|
||||
@@ -50,6 +50,11 @@ impl<T: DeserializeOwned + Default + Serialize + Clone> Store<T> {
|
||||
pub fn modify<F: FnOnce(&mut T)>(&self, f: F) {
|
||||
let mut lock = self.cache.write().unwrap();
|
||||
f(&mut lock);
|
||||
Self::save_to_db(&self.key, &self.db, &lock);
|
||||
let key = self.key.clone();
|
||||
let db = self.db.clone();
|
||||
let data = lock.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
Self::save_to_db(&key, &db, &data);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user