feat: add leaderboard cache, performance benchmarks, and verify SSE cleanup

- Add 30s TTL leaderboard cache with invalidation on fight completion
- Add scoreRound performance benchmark: 1000 rounds in <100ms
- Add checkAnswer performance benchmark: 1000 checks in <50ms
- Add adversarial regex backtracking test for checkAnswer
- Verify SSE cleanup: connections, IP counters, spectator counts, event listeners
  all properly decremented in finally block on disconnect

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dorian
2026-03-09 07:50:46 +00:00
co-authored by Claude Opus 4.6
parent 7d9b8b1dbf
commit 4074ef94eb
4 changed files with 104 additions and 30 deletions
+50 -30
View File
@@ -11,6 +11,14 @@ import { rateLimit } from '../middleware/rate-limit.js'
export const botsRouter = new Hono()
// Leaderboard cache — invalidated on fight completion
const leaderboardCache = new Map<string, { data: unknown; expiresAt: number }>()
const LEADERBOARD_TTL_MS = 30_000 // 30s cache
export function invalidateLeaderboardCache() {
leaderboardCache.clear()
}
function hashSecret(secret: string): string {
return createHash('sha256').update(secret).digest('hex')
}
@@ -150,46 +158,58 @@ botsRouter.get('/:name', async (c) => {
// Get bot stats -- full account page data
// Season leaderboard endpoint
// Season leaderboard endpoint (cached)
botsRouter.get('/leaderboard', async (c) => {
const seasonParam = c.req.query('season')
const cacheKey = seasonParam || '__alltime__'
const now = Date.now()
const cached = leaderboardCache.get(cacheKey)
if (cached && now < cached.expiresAt) {
return c.json(cached.data)
}
let result: unknown
if (seasonParam === 'current' || seasonParam) {
const { getCurrentSeason, getSeasonLeaderboard, getSeasonById } = await import('../engine/seasons.js')
const season = seasonParam === 'current' ? getCurrentSeason() : getSeasonById(seasonParam)
if (!season) return c.json({ error: 'Season not found' }, 404)
const entries = await getSeasonLeaderboard(season.id)
return c.json({ season, entries })
result = { season, entries }
} else {
// All-time: fall through to default bot list sorted by Elo
const allBots = await db.select({
id: schema.bots.id,
name: schema.bots.name,
avatarSeed: schema.bots.avatarSeed,
archetype: schema.bots.archetype,
eloRating: schema.bots.eloRating,
wins: schema.bots.wins,
losses: schema.bots.losses,
winStreak: schema.bots.winStreak,
tier: schema.bots.tier,
botType: schema.bots.botType,
}).from(schema.bots)
const ranked = allBots.filter(b => b.botType !== 'classic')
ranked.sort((a, b) => b.eloRating - a.eloRating)
result = { season: null, entries: ranked.map(b => ({
botId: b.id,
botName: b.name,
archetype: b.archetype,
tier: b.tier,
wins: b.wins,
losses: b.losses,
eloRating: b.eloRating,
avatarSeed: b.avatarSeed,
winStreak: b.winStreak,
})) }
}
// All-time: fall through to default bot list sorted by Elo
const allBots = await db.select({
id: schema.bots.id,
name: schema.bots.name,
avatarSeed: schema.bots.avatarSeed,
archetype: schema.bots.archetype,
eloRating: schema.bots.eloRating,
wins: schema.bots.wins,
losses: schema.bots.losses,
winStreak: schema.bots.winStreak,
tier: schema.bots.tier,
botType: schema.bots.botType,
}).from(schema.bots)
const ranked = allBots.filter(b => b.botType !== 'classic')
ranked.sort((a, b) => b.eloRating - a.eloRating)
return c.json({ season: null, entries: ranked.map(b => ({
botId: b.id,
botName: b.name,
archetype: b.archetype,
tier: b.tier,
wins: b.wins,
losses: b.losses,
eloRating: b.eloRating,
avatarSeed: b.avatarSeed,
winStreak: b.winStreak,
})) })
leaderboardCache.set(cacheKey, { data: result, expiresAt: now + LEADERBOARD_TTL_MS })
return c.json(result)
})
botsRouter.get('/:name/stats', async (c) => {