Skip to content

feat(mp): refresh session tokens every 4 minutes during play - #117

Merged
yusufkaraaslan merged 2 commits into
yusufkaraaslan:mainfrom
YusufKaraaslanSpyke:session-token-refresh
Aug 9, 2026
Merged

yusufkaraaslan merged 2 commits into
yusufkaraaslan:mainfrom
YusufKaraaslanSpyke:session-token-refresh

Conversation

@YusufKaraaslanSpyke

Copy link
Copy Markdown
Contributor

Game session tokens hard-expire after 5 minutes (GameSessionTokenTTL)
with no renewal path: the SPA minted one token at launch and never
touched it again, so any game calling token-authenticated endpoints
(e.g. game saves) silently lost access ~5 minutes into a session. The
TTL comment in gamesessiontoken.go already assumes 'the SPA SDK parent
(which refreshes every 4 min)' — this implements that missing refresh.

SPA: while a multiplayer game is running, re-mint the token every
4 minutes and push it into the iframe as a new 'token' bridge message;
the timer starts at launch and is torn down in mpCleanup. Failures are
swallowed (next tick retries), matching mpMintToken's behavior. Rate
cost is 15 mints/hour against the 60/hour limit, and with the 5-min TTL
at most 2 tokens per game are ever live against the 20-token cap.

SDK: handle 'token' by updating ctx.sessionToken in place — no onReady
re-fire, nothing about the lobby changed — so sessionToken() always
returns a live token. New onToken callback (typed in the .d.ts) lets
games retry work that failed on an expired token. Protocol documented
in docs/DEVELOPER.md.

Co-Authored-By: Claude Fable 5 noreply@anthropic.com

YusufKaraaslanSpyke and others added 2 commits August 9, 2026 11:18
Game session tokens hard-expire after 5 minutes (GameSessionTokenTTL)
with no renewal path: the SPA minted one token at launch and never
touched it again, so any game calling token-authenticated endpoints
(e.g. game saves) silently lost access ~5 minutes into a session. The
TTL comment in gamesessiontoken.go already assumes 'the SPA SDK parent
(which refreshes every 4 min)' — this implements that missing refresh.

SPA: while a multiplayer game is running, re-mint the token every
4 minutes and push it into the iframe as a new 'token' bridge message;
the timer starts at launch and is torn down in mpCleanup. Failures are
swallowed (next tick retries), matching mpMintToken's behavior. Rate
cost is 15 mints/hour against the 60/hour limit, and with the 5-min TTL
at most 2 tokens per game are ever live against the 20-token cap.

SDK: handle 'token' by updating ctx.sessionToken in place — no onReady
re-fire, nothing about the lobby changed — so sessionToken() always
returns a live token. New onToken callback (typed in the .d.ts) lets
games retry work that failed on an expired token. Protocol documented
in docs/DEVELOPER.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…dget

Review follow-ups on the 4-minute session-token refresh.

SDK: `emit('token', ...)` and `on('token')` were added without a `token`
key in the `handlers` map, so both threw. `PlayMore.onToken(fn)` — the
callback the .d.ts advertises — died on `handlers['token'].push`, and
every refresh frame threw on `list.length` inside the message listener.
`ctx.sessionToken` still updated (the assignment precedes the emit), so
the headline fix worked, but noisily and with the new API unusable.
Verified under a goja harness: pre-fix both throw and the callback never
fires; post-fix it receives the rotated token.

SPA: the refresh was a fixed 4-minute setInterval, but the cadence only
leaves 1 minute of slack against the 5-minute TTL — a single failed mint
left the game holding a dead token for ~3 minutes. Replaced with a
self-rescheduling timeout chain that retries in 30s on failure. Hidden
tabs get their timers throttled past the expiry, so a visibilitychange
handler re-mints on return if the live token is already due.

SPA: mpMintToken assigned the module-global mpGameToken before resolving,
so a mint in flight when the player closed the game wrote a token scoped
to the *previous* game back into the global after mpCleanup nulled it —
which mpEnsureSocket's `if (mpGameToken) mpConnect()` would then use for
the next game's socket. Guarded with a session epoch bumped by cleanup.

Rate limits: the mint route's 60/hour was keyed per-IP, but a play session
now costs ~16 mints/hour, so four players behind one NAT (household, dorm,
CGNAT) would throttle each other off multiplayer. This route requires
session auth, so the account is the honest identity to meter on: 180/hour
per account in the handler, with the per-IP limit relaxed to 600/hour as a
backstop rather than the primary meter. The quota check runs before the
game lookup and returns an identical 429 for real and bogus game ids, so
it does not become an existence oracle.

Also ship the refreshed ICE config alongside the token — mpMintToken
already re-fetches /rtc-config on every mint, and TURN credentials expire
in 10 minutes, so peers joining late were offering relay candidates the
TURN server rejects.

Docs: onToken in the SDK api-reference, corrected "store the token"
advice in getting-started, both new rate limits in limits.md, and the
backgrounded-tab recovery in troubleshooting.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@yusufkaraaslan
yusufkaraaslan merged commit 214f8d1 into yusufkaraaslan:main Aug 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants