feat: session cache, explored bitmap and lock-free tile gate (#65)

The tile gate no longer takes dbLock or runs a SQL session JOIN per tile: a published
in-memory view (server/sessions.nim) holds token hashes -> player/role and one explored
bitmap per player. Sessions load from the DB at startup and are updated by join, key
replacement and player removal in the same lock as the write; refreshed cells publish
before the ack. The gate is one snapshot read (parse + session + bit test + redirect)
over scalar data only, so a tile burst is answered while a mutation holds dbLock.

Evidence: tests/tsessions.nim pins the cache/bitmap lifecycle and gate rule; tiles.test
adds the lock-held burst and the revocation/restart seam; auth.test adds the
LEAK_FAULT=sessioncache negative control (a stale entry makes the auth group fail);
mutation c5 keeps the cache drop load-bearing. Explored wire and persistence untouched.

Bench (devbox run bench, in-process): tile gate old 0.006 ms -> new 0.001 ms per
request; incremental explored mark 0.001 ms; snapshot build 2.0 ms at 262144 cells
(startup only).
This commit is contained in:
2026-11-21 00:10:39 +01:00
parent 3c0258ea31
commit 2727617db9
12 changed files with 664 additions and 56 deletions
+26
View File
@@ -141,6 +141,32 @@ test('key.replace: old key and old sessions die, live connections close, the new
})
})
// #65: the negative control for the in-memory session cache. `LEAK_FAULT=sessioncache` skips the
// cache drop on key.replace, so the old cookie still upgrades (101 instead of the clean build's
// 401). It proves the replacement test above is load-bearing: without cache invalidation the auth
// group fails. The fault build contains the hook; the shipped binary cannot.
test('fault sessioncache: a revoked session kept alive fails the authentication group', async () => {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const s = await startServer({ stateDir, defines: ['leakfault'],
env: { LEAK_FAULT: 'sessioncache' } })
try {
const gm = await openAs(s.origin, gmKey)
const player = await createPlayer(gm.ws)
const session = await openAs(s.origin, player.key)
assert.equal((await rawUpgrade(s.origin, { cookie: session.cookie })).status, 101)
assert.equal((await gm.ws.request({ t: 'key.replace', id: player.id })).ok, true)
await session.ws.closed
const status = (await rawUpgrade(s.origin, { cookie: session.cookie })).status
assert.equal(status, 101, 'the fault keeps the revoked session in the cache')
assert.notEqual(status, 401, 'a stale cache is exactly what the auth group refuses')
gm.ws.close()
} finally {
await stopServer(s)
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
test('player.remove: key, sessions and connections gone; slot freed; the GM is not removable', async () => {
await withGmServer(async (s, gmKey) => {
const gm = await openAs(s.origin, gmKey)