expire old presences

This commit is contained in:
Devin Zuczek
2026-07-13 18:13:18 -04:00
parent 4860875cc7
commit c555f896a9
4 changed files with 97 additions and 4 deletions
+21
View File
@@ -137,6 +137,27 @@ export async function countPlayersInInstance(
return row?.n ?? 0
}
/**
* The room instances that expired presence rows still point at — the instances a
* player was in when they stopped heartbeating (a crash or a hard quit, where no
* matchmake ever moved them out). Their head-count has really dropped, so callers
* purging presence use this to recompute those instances' fullness. Distinct ids,
* lobby (null-instance) presence excluded.
*/
export async function getExpiredPresenceInstanceIds(
db: D1Database,
now = nowSeconds()
): Promise<number[]> {
const { results } = await db
.prepare(
`SELECT DISTINCT room_instance_id AS id FROM presence
WHERE expires_at <= ?1 AND room_instance_id IS NOT NULL`
)
.bind(now)
.all<{ id: number }>()
return results.map((r) => r.id)
}
/**
* Purge expired presence rows — housekeeping only, since reads already ignore them
* (and `INSERT OR REPLACE` keeps a single row per account, so the table is bounded