mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-21 23:05:46 +00:00
fix(analysis): CPU seed queue, single waveform emit, and log URL redaction
Serialize heavy PCM seeding through a dedicated queue with optional priority for the current track. Emit waveform-updated once per completed seed, fix Lucky Mix waveform refresh tokens, redact Subsonic URLs in logs, and align hot-cache prefetch with the queued path.
This commit is contained in:
@@ -20,7 +20,13 @@ interface HotCacheState {
|
||||
/** Persisted map `${serverId}:${trackId}` → file meta */
|
||||
entries: Record<string, HotCacheEntry>;
|
||||
getLocalUrl: (trackId: string, serverId: string) => string | null;
|
||||
setEntry: (trackId: string, serverId: string, localPath: string, sizeBytes: number) => void;
|
||||
setEntry: (
|
||||
trackId: string,
|
||||
serverId: string,
|
||||
localPath: string,
|
||||
sizeBytes: number,
|
||||
debugSource?: string,
|
||||
) => void;
|
||||
/** Bump LRU when the user actually plays this track (if it is in the hot cache). */
|
||||
touchPlayed: (trackId: string, serverId: string) => void;
|
||||
removeEntry: (trackId: string, serverId: string) => void;
|
||||
@@ -51,6 +57,29 @@ function lruStamp(meta: HotCacheEntry | undefined): number {
|
||||
return meta.lastPlayedAt ?? meta.cachedAt ?? 0;
|
||||
}
|
||||
|
||||
function evictionReasonForTier(tier: number): string {
|
||||
const labels: Record<number, string> = {
|
||||
0: 'inactive-server',
|
||||
1: 'not-in-queue',
|
||||
2: 'ahead-of-protected-window',
|
||||
3: 'behind-current-in-queue',
|
||||
};
|
||||
return labels[tier] ?? `tier-${tier}`;
|
||||
}
|
||||
|
||||
/** Settings → Logging → Debug, same as `emitNormalizationDebug` / lucky-mix. Dynamic `authStore` import avoids a static cycle (auth → player → hot-cache). */
|
||||
function hotCacheFrontendDebug(payload: Record<string, unknown>): void {
|
||||
void import('./authStore')
|
||||
.then(({ useAuthStore }) => {
|
||||
if (useAuthStore.getState().loggingMode !== 'debug') return;
|
||||
return invoke('frontend_debug_log', {
|
||||
scope: 'hot-cache',
|
||||
message: JSON.stringify(payload),
|
||||
});
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
export const useHotCacheStore = create<HotCacheState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
@@ -62,7 +91,7 @@ export const useHotCacheStore = create<HotCacheState>()(
|
||||
return `psysonic-local://${e.localPath}`;
|
||||
},
|
||||
|
||||
setEntry: (trackId, serverId, localPath, sizeBytes) => {
|
||||
setEntry: (trackId, serverId, localPath, sizeBytes, debugSource) => {
|
||||
const now = Date.now();
|
||||
set(s => ({
|
||||
entries: {
|
||||
@@ -75,6 +104,13 @@ export const useHotCacheStore = create<HotCacheState>()(
|
||||
},
|
||||
},
|
||||
}));
|
||||
hotCacheFrontendDebug({
|
||||
event: 'index-add',
|
||||
trackId,
|
||||
serverId,
|
||||
sizeBytes,
|
||||
source: debugSource ?? 'unknown',
|
||||
});
|
||||
},
|
||||
|
||||
touchPlayed: (trackId, serverId) => {
|
||||
@@ -97,6 +133,12 @@ export const useHotCacheStore = create<HotCacheState>()(
|
||||
delete next[entryKey(serverId, trackId)];
|
||||
return { entries: next };
|
||||
});
|
||||
hotCacheFrontendDebug({
|
||||
event: 'index-remove',
|
||||
trackId,
|
||||
serverId,
|
||||
reason: 'explicit-removeEntry',
|
||||
});
|
||||
emitAnalysisStorageChanged({ trackId, reason: 'hotcache-delete' });
|
||||
},
|
||||
|
||||
@@ -157,8 +199,31 @@ export const useHotCacheStore = create<HotCacheState>()(
|
||||
return a.lru - b.lru;
|
||||
});
|
||||
|
||||
for (const { key } of cands) {
|
||||
if (cands.length === 0) {
|
||||
hotCacheFrontendDebug({
|
||||
event: 'evict-no-candidates',
|
||||
sumBytes: sum,
|
||||
maxBytes,
|
||||
queueIndex,
|
||||
entryKeys: keys.length,
|
||||
reason: 'all-protected-or-grace-or-parse-fail',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
hotCacheFrontendDebug({
|
||||
event: 'evict-start',
|
||||
sumBytes: sum,
|
||||
maxBytes,
|
||||
queueIndex,
|
||||
protectLo,
|
||||
protectHi,
|
||||
candidateCount: cands.length,
|
||||
});
|
||||
|
||||
for (const cand of cands) {
|
||||
if (sum <= maxBytes) break;
|
||||
const { key, tier } = cand;
|
||||
const meta = entries[key];
|
||||
if (!meta) continue;
|
||||
const parsed = parseKey(key);
|
||||
@@ -166,7 +231,24 @@ export const useHotCacheStore = create<HotCacheState>()(
|
||||
await invoke('delete_hot_cache_track', {
|
||||
localPath: meta.localPath,
|
||||
customDir: hotCacheCustomDir || null,
|
||||
}).catch(() => {});
|
||||
}).catch((e: unknown) => {
|
||||
hotCacheFrontendDebug({
|
||||
event: 'evict-disk-delete-failed',
|
||||
trackId: parsed.trackId,
|
||||
serverId: parsed.serverId,
|
||||
error: String(e),
|
||||
});
|
||||
});
|
||||
hotCacheFrontendDebug({
|
||||
event: 'evict-remove',
|
||||
trackId: parsed.trackId,
|
||||
serverId: parsed.serverId,
|
||||
reason: `budget:${evictionReasonForTier(tier)}`,
|
||||
tier,
|
||||
bytes: meta.sizeBytes,
|
||||
sumBytesAfter: sum - (meta.sizeBytes || 0),
|
||||
maxBytes,
|
||||
});
|
||||
sum -= meta.sizeBytes || 0;
|
||||
delete entries[key];
|
||||
emitAnalysisStorageChanged({ trackId: parsed.trackId, reason: 'hotcache-delete' });
|
||||
@@ -176,6 +258,10 @@ export const useHotCacheStore = create<HotCacheState>()(
|
||||
},
|
||||
|
||||
clearAllDisk: async (customDir: string | null) => {
|
||||
hotCacheFrontendDebug({
|
||||
event: 'purge-all',
|
||||
customDir: customDir && customDir.length > 0 ? '(custom)' : 'default',
|
||||
});
|
||||
await invoke('purge_hot_cache', { customDir: customDir || null }).catch(() => {});
|
||||
set({ entries: {} });
|
||||
emitAnalysisStorageChanged({ trackId: null, reason: 'hotcache-purge' });
|
||||
|
||||
+36
-29
@@ -5,6 +5,7 @@ import { listen } from '@tauri-apps/api/event';
|
||||
import { showToast } from '../utils/toast';
|
||||
import { buildCoverArtUrl, buildStreamUrl, getPlayQueue, savePlayQueue, reportNowPlaying, scrobbleSong, SubsonicSong, getSong, getRandomSongs, getSimilarSongs2, getTopSongs, InternetRadioStation, setRating } from '../api/subsonic';
|
||||
import { resolvePlaybackUrl, streamUrlTrackId, getPlaybackSourceKind, type PlaybackSourceKind } from '../utils/resolvePlaybackUrl';
|
||||
import { redactSubsonicUrlForLog } from '../utils/redactSubsonicUrl';
|
||||
import { setDeferHotCachePrefetch } from '../utils/hotCacheGate';
|
||||
import { lastfmScrobble, lastfmUpdateNowPlaying, lastfmLoveTrack, lastfmUnloveTrack, lastfmGetTrackLoved, lastfmGetAllLovedTracks } from '../api/lastfm';
|
||||
import { useAuthStore } from './authStore';
|
||||
@@ -608,8 +609,16 @@ function touchHotCacheOnPlayback(trackId: string, serverId: string) {
|
||||
useHotCacheStore.getState().touchPlayed(trackId, serverId);
|
||||
}
|
||||
|
||||
/** Coalesce concurrent `analysis_get_waveform_for_track` for one id (StrictMode double mount, init + queue restore). */
|
||||
const waveformRefreshInflight = new Map<string, Promise<void>>();
|
||||
/** Last-write-wins generation per track: avoids applying a stale empty waveform read when
|
||||
* `analysis:waveform-updated` bumps gen after SQLite commit while an older `analysis_get_waveform_for_track`
|
||||
* is still in flight. Gen is bumped only on explicit invalidation (waveform-updated, analysis storage),
|
||||
* not on every `refreshWaveformForTrack` call — otherwise bursts (Lucky Mix, queue) cancel each other. */
|
||||
const waveformRefreshGenByTrackId: Record<string, number> = {};
|
||||
|
||||
function bumpWaveformRefreshGen(trackId: string) {
|
||||
if (!trackId) return;
|
||||
waveformRefreshGenByTrackId[trackId] = (waveformRefreshGenByTrackId[trackId] ?? 0) + 1;
|
||||
}
|
||||
|
||||
/** Coalesce concurrent `analysis_get_loudness_for_track` for one id+mode pair. The
|
||||
* analysis:waveform-updated listener fires refreshWaveform + refreshLoudness in
|
||||
@@ -740,33 +749,25 @@ async function reseedLoudnessForTrackId(trackId: string) {
|
||||
|
||||
async function refreshWaveformForTrack(trackId: string) {
|
||||
if (!trackId) return;
|
||||
let job = waveformRefreshInflight.get(trackId);
|
||||
if (!job) {
|
||||
const p = (async () => {
|
||||
try {
|
||||
const row = await invoke<WaveformCachePayload | null>('analysis_get_waveform_for_track', { trackId });
|
||||
// Never apply bins for a non-current track (e.g. gapless byte-preload fetches the neighbour).
|
||||
if (usePlayerStore.getState().currentTrack?.id !== trackId) return;
|
||||
const bins = row ? coerceWaveformBins(row.bins) : null;
|
||||
if (!bins || bins.length === 0) {
|
||||
usePlayerStore.setState({
|
||||
waveformBins: null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
usePlayerStore.setState({
|
||||
waveformBins: bins,
|
||||
});
|
||||
} catch {
|
||||
// best-effort; seekbar falls back to placeholder waveform
|
||||
}
|
||||
})();
|
||||
job = p.finally(() => {
|
||||
waveformRefreshInflight.delete(trackId);
|
||||
const gen = waveformRefreshGenByTrackId[trackId] ?? 0;
|
||||
try {
|
||||
const row = await invoke<WaveformCachePayload | null>('analysis_get_waveform_for_track', { trackId });
|
||||
if ((waveformRefreshGenByTrackId[trackId] ?? 0) !== gen) return;
|
||||
// Never apply bins for a non-current track (e.g. gapless byte-preload fetches the neighbour).
|
||||
if (usePlayerStore.getState().currentTrack?.id !== trackId) return;
|
||||
const bins = row ? coerceWaveformBins(row.bins) : null;
|
||||
if (!bins || bins.length === 0) {
|
||||
usePlayerStore.setState({
|
||||
waveformBins: null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
usePlayerStore.setState({
|
||||
waveformBins: bins,
|
||||
});
|
||||
waveformRefreshInflight.set(trackId, job);
|
||||
} catch {
|
||||
// best-effort; seekbar falls back to placeholder waveform
|
||||
}
|
||||
await job;
|
||||
}
|
||||
|
||||
/** When `syncPlayingEngine` is false, only update `cachedLoudnessGainByTrackId` (e.g. queue neighbour) — do not call `audio_update_replay_gain` for the already-playing track. */
|
||||
@@ -805,7 +806,11 @@ async function runRefreshLoudnessForTrack(trackId: string, syncEngine: boolean):
|
||||
analysisBackfillInFlightByTrackId[trackId] = true;
|
||||
analysisBackfillAttemptsByTrackId[trackId] = attempts + 1;
|
||||
const url = buildStreamUrl(trackId);
|
||||
emitNormalizationDebug('backfill:enqueue', { trackId, url, attempt: attempts + 1 });
|
||||
emitNormalizationDebug('backfill:enqueue', {
|
||||
trackId,
|
||||
url: redactSubsonicUrlForLog(url),
|
||||
attempt: attempts + 1,
|
||||
});
|
||||
void invoke('analysis_enqueue_seed_from_url', { trackId, url })
|
||||
.then(() => emitNormalizationDebug('backfill:queued', { trackId, attempt: attempts + 1 }))
|
||||
.catch((e) => emitNormalizationDebug('backfill:error', { trackId, error: String(e) }))
|
||||
@@ -884,7 +889,7 @@ async function promoteCompletedStreamToHotCache(track: Track, serverId: string,
|
||||
},
|
||||
);
|
||||
if (!res || !res.path) return;
|
||||
useHotCacheStore.getState().setEntry(track.id, serverId, res.path, res.size || 0);
|
||||
useHotCacheStore.getState().setEntry(track.id, serverId, res.path, res.size || 0, 'stream-promote');
|
||||
} catch {
|
||||
// best-effort promotion; normal hot-cache prefetch remains fallback
|
||||
}
|
||||
@@ -1300,6 +1305,7 @@ export function initAudioListeners(): () => void {
|
||||
const currentRaw = usePlayerStore.getState().currentTrack?.id;
|
||||
const currentId = currentRaw ? normalizeAnalysisTrackId(currentRaw) : null;
|
||||
if (currentId && payloadTrackId === currentId) {
|
||||
bumpWaveformRefreshGen(currentRaw!);
|
||||
void refreshWaveformForTrack(currentRaw!);
|
||||
void refreshLoudnessForTrack(currentId);
|
||||
emitNormalizationDebug('backfill:applied', { trackId: currentId });
|
||||
@@ -1468,6 +1474,7 @@ export function initAudioListeners(): () => void {
|
||||
const currentId = usePlayerStore.getState().currentTrack?.id;
|
||||
if (!currentId) return;
|
||||
if (detail.trackId && detail.trackId !== currentId) return;
|
||||
bumpWaveformRefreshGen(currentId);
|
||||
void refreshWaveformForTrack(currentId);
|
||||
void refreshLoudnessForTrack(currentId);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user