From ac219ae728b46f43f79de112423d0d94e26f1b03 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 2 Jun 2020 09:35:27 +1000 Subject: [PATCH] use a shared async runtime instead of recreating each time --- rslib/Cargo.toml | 2 +- rslib/src/backend/mod.rs | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/rslib/Cargo.toml b/rslib/Cargo.toml index 0bb56f85e..0d7b7a229 100644 --- a/rslib/Cargo.toml +++ b/rslib/Cargo.toml @@ -23,7 +23,7 @@ unicode-normalization = "0.1.12" tempfile = "3.1.0" serde = "1.0.104" serde_json = "1.0.45" -tokio = { version = "0.2.11", features = ["fs"] } +tokio = { version = "0.2.21", features = ["fs", "rt-threaded"] } serde_derive = "1.0.104" zip = "0.5.4" serde_tuple = "0.4.0" diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index b6439b1c4..32e4455bf 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -54,7 +54,7 @@ use std::{ result, sync::{Arc, Mutex}, }; -use tokio::runtime::Runtime; +use tokio::runtime::{self, Runtime}; mod dbproxy; @@ -91,6 +91,7 @@ pub struct Backend { sync_abort: Option, media_sync_abort: Option, progress_state: Arc>, + runtime: Option, } #[derive(Clone, Copy)] @@ -1155,6 +1156,7 @@ impl Backend { want_abort: false, last_progress: None, })), + runtime: None, } } @@ -1203,6 +1205,20 @@ impl Backend { } } + fn runtime_handle(&mut self) -> runtime::Handle { + if self.runtime.is_none() { + self.runtime = Some( + runtime::Builder::new() + .threaded_scheduler() + .core_threads(1) + .enable_all() + .build() + .unwrap(), + ) + } + self.runtime.as_ref().unwrap().handle().clone() + } + fn sync_media_inner( &mut self, input: pb::SyncAuth, @@ -1217,7 +1233,7 @@ impl Backend { let progress_fn = move |progress| handler.update(progress, true); let mgr = MediaManager::new(&folder, &db)?; - let mut rt = Runtime::new().unwrap(); + let rt = self.runtime_handle(); let sync_fut = mgr.sync_media(progress_fn, input.host_number, &input.hkey, log); let abortable_sync = Abortable::new(sync_fut, abort_reg); let ret = match rt.block_on(abortable_sync) { @@ -1235,7 +1251,7 @@ impl Backend { let (abort_handle, abort_reg) = AbortHandle::new_pair(); self.sync_abort = Some(abort_handle); - let mut rt = Runtime::new().unwrap(); + let rt = self.runtime_handle(); let sync_fut = sync_login(&input.username, &input.password); let abortable_sync = Abortable::new(sync_fut, abort_reg); let ret = match rt.block_on(abortable_sync) { @@ -1257,7 +1273,7 @@ impl Backend { let (abort_handle, abort_reg) = AbortHandle::new_pair(); self.sync_abort = Some(abort_handle); - let mut rt = Runtime::new().unwrap(); + let rt = self.runtime_handle(); let input_copy = input.clone(); let ret = self.with_col(|col| { @@ -1298,6 +1314,8 @@ impl Backend { } fn full_sync_inner(&mut self, input: pb::SyncAuth, upload: bool) -> Result<()> { + let rt = self.runtime_handle(); + let mut col = self.col.lock().unwrap(); if col.is_none() { return Err(AnkiError::CollectionNotOpen); @@ -1321,8 +1339,6 @@ impl Backend { handler.update(progress, throttle); }; - let mut rt = Runtime::new().unwrap(); - let result = if upload { let sync_fut = col_inner.full_upload(input.into(), progress_fn); let abortable_sync = Abortable::new(sync_fut, abort_reg);