Repository navigation
feat(mp): refresh session tokens every 4 minutes during play - #117
Merged
yusufkaraaslan merged 2 commits intoAug 9, 2026
Merged
yusufkaraaslan merged 2 commits into
yusufkaraaslan merged 2 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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