fix(lastfm): optimize getTotalScrobbles fast path and rate-limit background syncs

- Read scrobble count sum directly from local database in getTotalScrobbles() so %stats renders instantly without timing out
- Run user playcount sync asynchronously in the background with gentle 200ms batch spacing to prevent Last.fm API rate-limiting
- Suppress false-alarm ErrorChannel warnings on transient background sync drops
- Include error cause details in apiCall fetch failures for easier network diagnostics
This commit is contained in:
NoLogicAlan committed 2026-10-28 02:38:56 +03:00
1 parent bf376c8861
commit c007bb93f8
3 files changed
+34 -8

No files matched your search

+3 -1
View File
@@ -124,7 +124,9 @@ const ScrobblingMixin = {
}
return 0;
}
logger.warn("[LastFm] Error:", e?.message);
if (!e?.message?.includes("fetch failed")) {
logger.warn("[LastFm] Error:", e?.message);
}
return 0;
}
}
+21 -6
View File
@@ -258,14 +258,26 @@ const ServerStatsMixin = {
return this._totalScrobblesCache;
}
if (this._totalScrobblesInflight) return this._totalScrobblesInflight;
this._totalScrobblesInflight = this._refreshTotalScrobbles(concurrency);
/* Fast path: immediately read cached sum from local database so caller (%stats) never times out */
try {
return await this._totalScrobblesInflight;
} finally {
this._totalScrobblesInflight = null;
const pool = await this._getPool();
const f = this._botIdFilter();
const [sumRows] = await pool.execute(
`SELECT COALESCE(SUM(scrobble_count), 0) AS total FROM lastfm_users WHERE 1=1${f.where}`,
[...f.params]
);
const dbTotal = Number(sumRows[0]?.total ?? 0);
if (this._totalScrobblesCache === null) {
this._totalScrobblesCache = dbTotal;
}
} catch (_) { }
if (!this._totalScrobblesInflight) {
this._totalScrobblesInflight = this._refreshTotalScrobbles(concurrency)
.finally(() => { this._totalScrobblesInflight = null; });
}
return this._totalScrobblesCache ?? 0;
},
/** @private @async Sync all users' scrobble counts from Last.fm and cache the total. @param {number} concurrency - Concurrent sync batch size. @returns {Promise<number>} Fresh total scrobble count. */
@@ -290,6 +302,9 @@ const ServerStatsMixin = {
for (let i = 0; i < userIds.length; i += concurrency) {
const batch = userIds.slice(i, i + concurrency);
await Promise.allSettled(batch.map(uid => this.syncUserScrobbleCount(uid)));
if (i + concurrency < userIds.length) {
await new Promise(r => setTimeout(r, 200));
}
}
const [sumRows] = await pool.execute(
+10 -1
View File
@@ -131,7 +131,16 @@ async function apiCall(params, apiSecret, post = false) {
}
: { signal: AbortSignal.timeout(LASTFM_FETCH_TIMEOUT_MS) };
const res = await fetch(url, opts);
let res;
try {
res = await fetch(url, opts);
} catch (err) {
const cause = err?.cause?.message ?? err?.cause?.code ?? err?.cause ?? "";
const detail = cause ? ` (${cause})` : "";
const networkErr = new Error(`fetch failed${detail}`);
networkErr.cause = err?.cause ?? err;
throw networkErr;
}
if (!res.ok) {
const text = await res.text().catch(() => "");
let code = 0;