Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
api: the state cache gets an age and a size, or it is just a leak
Boris 'B' Kurktchiev003a72dfb45f
diff --git a/api/server.js b/api/server.jsindex 04e2e5a8a..777a2f6cb 100644--- a/api/server.js+++ b/api/server.js@@ -317,16 +317,33 @@ const minutesLate = (time, now) => hhmmToMin(now.hhmm) - hhmmToMin(time);// The tick reads every subscribed user's state file every 10 s. Most of those files do not// change between ticks; a stat is far cheaper than a read and a parse of a state that can be// megabytes, and it keeps the tick short — a slow tick was one more way to miss the minute.-// Nothing evicts by age or size, so this holds one parsed state per user who polls or gets a-// reminder, for the life of the process (single-user deployment here; an upstream caveat).-const stateCache = new Map(); // uid -> { mtimeMs, size, S }+//+// What it holds is a whole parsed state per user, and a state can be megabytes, so the two+// bounds below are what keep it a cache rather than a leak on an instance with more than one+// person on it: an entry nobody has touched for ten minutes is dropped, and the map never holds+// more than STATE_CACHE_MAX users. Map iteration order is insertion order, so re-inserting on+// every hit makes the first key the least recently hit one — which is the one to evict.+const STATE_CACHE_MAX = 64;+// Ten minutes: far longer than the gap between a client's polls (30 s) or the reminder tick's+// (10 s), so nobody who is actually using the instance is ever evicted by age; the tests shorten+// it, as they do REMINDER_TICK_MS.+const STATE_CACHE_TTL_MS = Math.max(50, +(process.env.STATE_CACHE_TTL_MS || 600000) || 600000);+const stateCache = new Map(); // uid -> { mtimeMs, size, hitAt, S }function readStateCached(uid) {let st;try { st = fs.statSync(stateFile(uid)); } catch { stateCache.delete(uid); return null; }+ const now = Date.now();+ for (const [k, v] of stateCache) if (now - v.hitAt > STATE_CACHE_TTL_MS) stateCache.delete(k);const hit = stateCache.get(uid);- if (hit && hit.mtimeMs === st.mtimeMs && hit.size === st.size) return hit.S;+ stateCache.delete(uid); // re-inserted below, so the map stays in hit order+ if (hit && hit.mtimeMs === st.mtimeMs && hit.size === st.size) {+ hit.hitAt = now;+ stateCache.set(uid, hit);+ return hit.S;+ }const S = readState(uid);- stateCache.set(uid, { mtimeMs: st.mtimeMs, size: st.size, S });+ stateCache.set(uid, { mtimeMs: st.mtimeMs, size: st.size, hitAt: now, S });+ while (stateCache.size > STATE_CACHE_MAX) stateCache.delete(stateCache.keys().next().value);return S;}setInterval(() => {diff --git a/api/test/server-state-cache-bounds.test.js b/api/test/server-state-cache-bounds.test.jsnew file mode 100644index 000000000..0819cafd2--- /dev/null+++ b/api/test/server-state-cache-bounds.test.js@@ -0,0 +1,90 @@+/* The stat cache behind GET /api/data/rev (and the reminder tick, and the two push routes) holds+ a whole parsed state per user — megabytes each on a long-used profile — and used to hold every+ one of them for the life of the process. Two bounds now: an entry nobody has hit for+ STATE_CACHE_TTL_MS is dropped, and the map never exceeds 64 users, oldest hit first.++ Neither is visible in a response on its own, so both are read here through the probe+ server-state-read-cost.test.js established: an out-of-band rewrite with a pinned mtime and an+ identical size is invisible to the cache key, so a cached read answers with the old revision+ and a read that had to go to disk answers with the new one. Nothing in this tree writes a state+ file that way — which is what makes it a probe rather than a bug. */+import { test } from 'node:test';+import assert from 'node:assert/strict';+import crypto from 'node:crypto';+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import { spawn } from 'node:child_process';+import { fileURLToPath } from 'node:url';++const API = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');+const SECRET = crypto.randomBytes(32).toString('hex');+const PROBE = 'u_cache_probe';+// One more than the cap, so polling all of them pushes exactly one entry — the probe's — out.+const FILLERS = Array.from({ length: 64 }, (_, i) => `u_cache_${i}`);++const mint = uid => {+ const payload = `${uid}:${Date.now() + 86400000}:0`;+ return payload + '.' + crypto.createHmac('sha256', SECRET).update(payload).digest('base64url');+};+const headers = uid => ({ Cookie: `gymsid=${mint(uid)}` });++async function startServer(t, env = {}) {+ const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gym-cachebound-'));+ fs.writeFileSync(path.join(dataDir, 'secret'), SECRET, { mode: 0o600 });+ const users = [PROBE, ...FILLERS].map(id => ({ id, name: id, created: new Date().toISOString() }));+ fs.writeFileSync(path.join(dataDir, 'db.json'), JSON.stringify({ users, creds: [], subs: [], invites: [] }));+ // Every filler needs a file of its own: a uid with no state file is never cached at all.+ for (const uid of FILLERS) fs.writeFileSync(path.join(dataDir, `state-${uid}.json`), JSON.stringify({ _rev: 1, workouts: [], routines: [] }));+ const child = spawn(process.execPath, ['server.js'], {+ cwd: API, stdio: ['ignore', 'pipe', 'pipe'],+ env: { ...process.env, PORT: '0', DATA_DIR: dataDir, ORIGIN: 'http://localhost:8080', RP_ID: 'localhost', ...env }+ });+ t.after(() => { child.kill('SIGKILL'); fs.rmSync(dataDir, { recursive: true, force: true }); });+ let log = '';+ const port = await new Promise((resolve, reject) => {+ const give = setTimeout(() => reject(new Error(`server never announced a port:\n${log}`)), 20000);+ child.stdout.on('data', d => { log += d; const m = /gym-api on :(\d+)/.exec(log); if (m) { clearTimeout(give); resolve(+m[1]); } });+ child.stderr.on('data', d => { log += d; });+ child.on('exit', c => { clearTimeout(give); reject(new Error(`server exited (${c}):\n${log}`)); });+ });+ return { api: `http://127.0.0.1:${port}`, dataDir };+}++// utimesSync round-trips this value exactly, and the document is the same length at every+// revision below, so (mtimeMs, size) cannot tell two of these apart.+const PINNED = new Date(1700000000000);+const writePinned = (dir, uid, rev) => {+ const file = path.join(dir, `state-${uid}.json`);+ fs.writeFileSync(file, JSON.stringify({ _rev: rev, lang: 'en', workouts: [], routines: [] }));+ fs.utimesSync(file, PINNED, PINNED);+};++test('the cache holds at most 64 users, and the least recently polled is the one dropped', async t => {+ const h = await startServer(t);+ const rev = async uid => (await fetch(`${h.api}/api/data/rev`, { headers: headers(uid) }).then(r => r.json())).rev;++ writePinned(h.dataDir, PROBE, 7);+ assert.equal(await rev(PROBE), 7, 'the first poll reads the file');+ writePinned(h.dataDir, PROBE, 8);+ assert.equal(await rev(PROBE), 7, 'and the second is served from the cache, as it should be');++ // 64 other people poll once each. That is one more entry than the cap allows alongside the+ // probe's, and the probe's is the one nobody has touched since.+ for (const uid of FILLERS) await rev(uid);++ assert.equal(await rev(PROBE), 8, 'the probe was evicted, so its file was read again');+});++test('an entry nobody has polled for longer than the TTL is dropped', async t => {+ const h = await startServer(t, { STATE_CACHE_TTL_MS: '150' });+ const rev = async uid => (await fetch(`${h.api}/api/data/rev`, { headers: headers(uid) }).then(r => r.json())).rev;++ writePinned(h.dataDir, PROBE, 7);+ assert.equal(await rev(PROBE), 7);+ writePinned(h.dataDir, PROBE, 8);+ assert.equal(await rev(PROBE), 7, 'still inside the TTL — the cached copy answers');++ await new Promise(r => setTimeout(r, 400));+ assert.equal(await rev(PROBE), 8, 'past it, the entry is gone and the file is read again');+});