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:
+43
-11
@@ -17,17 +17,27 @@
|
||||
##
|
||||
## Access rule [FIXED, Q4/3.3]: the GM is always allowed; a player is allowed iff at least one cell
|
||||
## of that player's explored grid overlaps the tile rectangle (in v1 `tile_size == cell`, so one
|
||||
## cell). Nothing here reads the world or a lock: main.nim hands in the scene and the player's
|
||||
## explored set. A player with no sight state has explored nothing, so every tile is refused.
|
||||
## cell). Nothing here reads the world, the DB or the global lock: these are pure functions over a
|
||||
## `TileScene` and either an explored set (unit tests) or its bitmap (the published gate view,
|
||||
## issue #65). A player with no sight state has explored nothing, so every tile is refused.
|
||||
##
|
||||
## Status split [FIXED (owner-delegated), #24]: a nonexistent tile is 404; an existing tile that the
|
||||
## session may not have (no session, unknown session, or unexplored) is 403. The 403 is the same for
|
||||
## "no session" and "unexplored" so the route never says whether a tile exists for an anonymous
|
||||
## requester beyond the non-secret scene geometry (3.1: world size and cell size are not secret).
|
||||
import std/[math, sets, strutils]
|
||||
import vision, world
|
||||
import explored, vision, world
|
||||
|
||||
type
|
||||
TileScene* = object
|
||||
## The tile gate's scalar copy of the scene (issue #65). The gate reads it from the published
|
||||
## snapshot without the global lock; the value holds no `seq`/`ref`, so copying it across a
|
||||
## thread boundary costs no ORC reference-count traffic. `w/h/tile_size/cell/art_version` never
|
||||
## change at runtime (`scene.set` cannot patch them, 6.1), so a snapshot is stable for its life.
|
||||
id*: int
|
||||
w*, h*: float
|
||||
tileSize*, cell*, cols*, rows*, artVersion*: int
|
||||
|
||||
TileOutcome* = enum
|
||||
toTileOk
|
||||
toTileMalformed ## a component that is not canonical or a suffix that is not `.png` -> 404
|
||||
@@ -48,6 +58,12 @@ const
|
||||
## 99999 tiles is far beyond any v1 scene (128 tiles per side at 4096 px); a longer component
|
||||
## is malformed rather than parsed.
|
||||
|
||||
converter tileScene*(sc: SceneRec): TileScene =
|
||||
## The one conversion point: existing callers keep passing a `SceneRec`; the gate publishes and
|
||||
## consumes a `TileScene` (no refs). `cols`/`rows` come from `vision` (ceil(w / cell)).
|
||||
TileScene(id: sc.id, w: sc.w, h: sc.h, tileSize: sc.tileSize, cell: sc.cell,
|
||||
cols: sc.cols, rows: sc.rows, artVersion: sc.artVersion)
|
||||
|
||||
proc canonicalNumber*(raw: string, maxDigits: int, dest: var int): bool =
|
||||
## The URL's number grammar (this module's header): digits only, 1..maxDigits, no leading zero.
|
||||
## Nothing is parsed unless every rule holds, so an overflow cannot reach `parseInt`.
|
||||
@@ -66,18 +82,18 @@ proc parseVersion*(raw: string, dest: var int): bool =
|
||||
if raw.len < 2 or raw[0] != 'v': return false
|
||||
canonicalNumber(raw[1 .. ^1], MaxVersionDigits, dest)
|
||||
|
||||
proc tileCols*(sc: SceneRec): int =
|
||||
proc tileCols*(sc: TileScene): int =
|
||||
## Tiles across the scene (DESIGN 3.3: the tile grid's origin is the world origin).
|
||||
max(1, int(ceil(sc.w / float(sc.tileSize))))
|
||||
|
||||
proc tileRows*(sc: SceneRec): int =
|
||||
proc tileRows*(sc: TileScene): int =
|
||||
max(1, int(ceil(sc.h / float(sc.tileSize))))
|
||||
|
||||
proc tileInRange*(sc: SceneRec, x, y: int): bool =
|
||||
proc tileInRange*(sc: TileScene, x, y: int): bool =
|
||||
x >= 0 and y >= 0 and x < tileCols(sc) and y < tileRows(sc)
|
||||
|
||||
proc parseTileRequest*(sceneRaw, versionRaw, fileRaw: string,
|
||||
sc: SceneRec): tuple[outcome: TileOutcome, req: TileRequest] =
|
||||
sc: TileScene): tuple[outcome: TileOutcome, req: TileRequest] =
|
||||
## Strict parse of the three route components (this module's header). `req` is only meaningful
|
||||
## when `outcome == toTileOk`.
|
||||
var sceneId, artVersion: int
|
||||
@@ -106,7 +122,7 @@ proc parseTileRequest*(sceneRaw, versionRaw, fileRaw: string,
|
||||
|
||||
(toTileOk, TileRequest(scene: sceneId, artVersion: artVersion, x: x, y: y))
|
||||
|
||||
proc tileCoveredByExplored*(sc: SceneRec, explored: HashSet[int], tileX, tileY: int): bool =
|
||||
proc tileCoveredByExplored*(sc: TileScene, explored: HashSet[int], tileX, tileY: int): bool =
|
||||
## DESIGN 3.3: a tile is allowed iff at least one explored cell overlaps it. The tile rectangle is
|
||||
## `[tileX*tile_size, (tileX+1)*tile_size) x ...`; a cell overlaps when its rectangle shares any
|
||||
## point with it. General over `tile_size` and `cell` (v1 pins them equal, Q4, but a 64 px
|
||||
@@ -120,16 +136,32 @@ proc tileCoveredByExplored*(sc: SceneRec, explored: HashSet[int], tileX, tileY:
|
||||
for cy in cy0 .. cy1:
|
||||
for cx in cx0 .. cx1:
|
||||
if cx < 0 or cy < 0 or cx >= sc.cols or cy >= sc.rows: continue
|
||||
if sc.cellIndex(cx, cy) in explored: return true
|
||||
if cy * sc.cols + cx in explored: return true
|
||||
false
|
||||
|
||||
proc tileAllowed*(sc: SceneRec, explored: HashSet[int], tileX, tileY: int,
|
||||
proc tileCoveredByBits*(sc: TileScene, bits: openArray[byte], tileX, tileY: int): bool =
|
||||
## The same overlap rule over the gate's explored bitmap (issue #65): cell index `cy * cols + cx`,
|
||||
## bit `idx shr 3`, mask `1 shl (idx and 7)`, exactly `BitMap`/`cellIndex`. The bitmap is borrowed
|
||||
## (openArray), so a gate read never copies or ref-counts the snapshot.
|
||||
let x0 = tileX * sc.tileSize
|
||||
let y0 = tileY * sc.tileSize
|
||||
let cx0 = x0 div sc.cell
|
||||
let cy0 = y0 div sc.cell
|
||||
let cx1 = (x0 + sc.tileSize - 1) div sc.cell
|
||||
let cy1 = (y0 + sc.tileSize - 1) div sc.cell
|
||||
for cy in cy0 .. cy1:
|
||||
for cx in cx0 .. cx1:
|
||||
if cx < 0 or cy < 0 or cx >= sc.cols or cy >= sc.rows: continue
|
||||
if bitSet(bits, cy * sc.cols + cx): return true
|
||||
false
|
||||
|
||||
proc tileAllowed*(sc: TileScene, explored: HashSet[int], tileX, tileY: int,
|
||||
isGm: bool): bool =
|
||||
## The whole gate rule (3.3): the GM is always allowed; a player only through its explored grid.
|
||||
if isGm: return true
|
||||
tileCoveredByExplored(sc, explored, tileX, tileY)
|
||||
|
||||
proc tilePath*(sc: SceneRec, x, y: int): string =
|
||||
proc tilePath*(sc: TileScene, x, y: int): string =
|
||||
## The internal nginx path of a tile (3.3, 11.1): `/_tiles/<scene>/v<art_version>/<x>_<y>.png`.
|
||||
## Built only from integers, never from request text, so no request can steer it.
|
||||
"/_tiles/" & $sc.id & "/v" & $sc.artVersion & "/" & $x & "_" & $y & ".png"
|
||||
|
||||
Reference in New Issue
Block a user