diff options
author | Vika <vika@fireburn.ru> | 2024-08-26 13:33:37 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2024-08-26 14:36:57 +0300 |
commit | 5218889b43c4ee3eafee1c9871dbeba2887ded83 (patch) | |
tree | 1cc4791763a57853bd9107e6bbb3ae6ab0267453 /src/database/file | |
parent | d4fb528c14689263635b006e332cbca8d59d196a (diff) | |
download | kittybox-5218889b43c4ee3eafee1c9871dbeba2887ded83.tar.zst |
FileStorage: appease clippy and use block_in_place for rewinding feeds
Using `tokio::task::block_in_place` downgrades the thread temporarily to avoid starvation.
Diffstat (limited to 'src/database/file')
-rw-r--r-- | src/database/file/mod.rs | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/database/file/mod.rs b/src/database/file/mod.rs index ba8201f..117ba17 100644 --- a/src/database/file/mod.rs +++ b/src/database/file/mod.rs @@ -66,8 +66,8 @@ fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> { } (None, _) => comps.push(Component::ParentDir), (Some(a), Some(b)) if comps.is_empty() && a == b => (), - (Some(a), Some(b)) if b == Component::CurDir => comps.push(a), - (Some(_), Some(b)) if b == Component::ParentDir => return None, + (Some(a), Some(Component::CurDir)) => comps.push(a), + (Some(_), Some(Component::ParentDir)) => return None, (Some(a), Some(_)) => { comps.push(Component::ParentDir); for _ in itb { @@ -322,7 +322,7 @@ impl Storage for FileStorage { .as_str() .expect("Tried to save a post without UID"); let path = url_to_path(&self.root_dir, key); - let tempfile = (&path).with_extension("tmp"); + let tempfile = path.with_extension("tmp"); debug!("Creating {:?}", path); let parent = path @@ -395,7 +395,7 @@ impl Storage for FileStorage { let channel_name = post["properties"]["name"][0] .as_str() .map(|s| s.to_string()) - .unwrap_or_else(String::default); + .unwrap_or_default(); let key = key.to_string(); tracing::debug!("Opening temporary file to modify chnanels..."); let mut tempfile = OpenOptions::new() @@ -576,13 +576,18 @@ impl Storage for FileStorage { // This imperative snippet consumes after instead of emitting it, allowing the // stream of posts to return only those items that truly come *after* that one. // If I would implement an Iter combinator like this, I would call it `skip_until` + // + // Uses `tokio::task::block_in_place` to prevent starvation in case of rewinding + // incredibly long feeds. if let Some(after) = after { - for s in posts_iter.by_ref() { - if &s == after { - break; + tokio::task::block_in_place(|| { + for s in posts_iter.by_ref() { + if s == after { + break; + } } - } - }; + }) + } let posts = stream::iter(posts_iter) .map(|url: String| async move { self.get_post(&url).await }) .buffered(std::cmp::min(3, limit)) |