forked from Furumi/furumi-tui
Keep podcast store work in sync handlers off Tokio workers
The device-sync stream handlers exported and merged podcast snapshot sections and operations synchronously on a Tokio worker. While a large feed refresh held the store (up to about half a second), the worker's other tasks (feed checks, streams, art) waited. Run that work in block_in_place on the multi-thread runtime, and say so in ARCHITECTURE. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e0da2ddf2b
commit
268ea915da
3 files changed
+68
-8
No files matched your search
+32
-5
@@ -2328,7 +2328,17 @@ impl DeviceSync {
|
||||
/// the podcast snapshot sections. Podcast merges are order-independent;
|
||||
/// operations go first so a changed progress register reaches the UI as
|
||||
/// such (it is shown in place) instead of as a generic change.
|
||||
fn apply_received(&self, mut snapshot: SyncSnapshot, ops: Vec<SyncOpWire>) -> Result<()> {
|
||||
fn apply_received(&self, snapshot: SyncSnapshot, ops: Vec<SyncOpWire>) -> Result<()> {
|
||||
// Called from the async stream handlers; podcast store merges can
|
||||
// wait on a feed refresh holding the store.
|
||||
off_worker(|| self.apply_received_blocking(snapshot, ops))
|
||||
}
|
||||
|
||||
fn apply_received_blocking(
|
||||
&self,
|
||||
mut snapshot: SyncSnapshot,
|
||||
ops: Vec<SyncOpWire>,
|
||||
) -> Result<()> {
|
||||
let podcast = SyncSnapshot {
|
||||
podcast_subscriptions: std::mem::take(&mut snapshot.podcast_subscriptions),
|
||||
podcast_shares: std::mem::take(&mut snapshot.podcast_shares),
|
||||
@@ -2593,10 +2603,12 @@ impl DeviceSync {
|
||||
let mut snapshot = self.snapshot()?;
|
||||
if music_dht::device_sync::supports_podcasts(peer_version)
|
||||
&& let Some(store) = self.podcast_store()
|
||||
&& let Err(err) = store.fill_snapshot(
|
||||
&mut snapshot,
|
||||
music_dht::podcast::MAX_PODCAST_SNAPSHOT_BYTES,
|
||||
)
|
||||
&& let Err(err) = off_worker(|| {
|
||||
store.fill_snapshot(
|
||||
&mut snapshot,
|
||||
music_dht::podcast::MAX_PODCAST_SNAPSHOT_BYTES,
|
||||
)
|
||||
})
|
||||
{
|
||||
// Music sync goes on; podcast state comes with a later sync.
|
||||
tracing::warn!("podcast snapshot export failed: {err:#}");
|
||||
@@ -4213,6 +4225,21 @@ mod wire_compat_tests;
|
||||
#[cfg(test)]
|
||||
mod podcast_sync_tests;
|
||||
|
||||
/// Runs podcast store work from an async device-sync handler without
|
||||
/// stalling a Tokio worker: the store may be busy with a feed refresh (one
|
||||
/// transaction, up to about half a second for a very large feed). On the
|
||||
/// multi-thread runtime the worker hands its other tasks over while `work`
|
||||
/// runs (`block_in_place`); elsewhere (blocking pool, tests without a
|
||||
/// runtime, a current-thread runtime) it simply runs.
|
||||
fn off_worker<T>(work: impl FnOnce() -> T) -> T {
|
||||
match tokio::runtime::Handle::try_current() {
|
||||
Ok(handle) if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread => {
|
||||
tokio::task::block_in_place(work)
|
||||
}
|
||||
_ => work(),
|
||||
}
|
||||
}
|
||||
|
||||
fn playback_clock() -> u64 {
|
||||
static START: std::sync::OnceLock<std::time::Instant> = std::sync::OnceLock::new();
|
||||
START
|
||||
|
||||
Reference in new issue
Block a user