feat: redesign auction engine with redis sot

This commit is contained in:
dahyeon committed 2026-06-23 03:16:04 -07:00
1 parent 737702d78c
commit e8bb20d988
75 files changed
+12737 -542

No files matched your search

@@ -1,5 +1,5 @@
import { Injectable, Logger } from '@nestjs/common';
import { GarnetService } from '../global/garnet/garnet.service';
import { RedisService } from '../global/redis/redis.service';
const DDRAGON_BASE = 'https://ddragon.leagueoflegends.com';
const DDRAGON_CDN = `${DDRAGON_BASE}/cdn`;
@@ -75,13 +75,13 @@ export class RiotDdragonService {
private readonly logger = new Logger(RiotDdragonService.name);
private inflight = new Map<string, Promise<DdragonManifest>>();
constructor(private readonly garnetService: GarnetService) {}
constructor(private readonly redisService: RedisService) {}
public async getManifest(locale: string = DEFAULT_LOCALE): Promise<DdragonManifest> {
const normalizedLocale = normalizeLocale(locale);
const cacheKey = manifestCacheKey(normalizedLocale);
const cached = await this.garnetService.getJson<DdragonManifest>(cacheKey);
const cached = await this.redisService.getJson<DdragonManifest>(cacheKey);
if (cached) return cached;
const pending = this.inflight.get(cacheKey);
@@ -89,7 +89,7 @@ export class RiotDdragonService {
const promise = this.buildManifest(normalizedLocale)
.then(async (manifest) => {
await this.garnetService.setJson(cacheKey, manifest, CACHE_TTL_SECONDS);
await this.redisService.setJson(cacheKey, manifest, CACHE_TTL_SECONDS);
return manifest;
})
.finally(() => {
@@ -1,15 +1,15 @@
import { GarnetService } from '../global/garnet/garnet.service';
import { RedisService } from '../global/redis/redis.service';
import { RiotRateLimiterService } from './riot-rate-limiter.service';
function makeService(sendCommand = jest.fn().mockResolvedValue(0)) {
const garnet = {
const redis = {
getString: jest.fn().mockResolvedValue(null),
raw: jest.fn().mockReturnValue({ sendCommand }),
setString: jest.fn().mockResolvedValue(undefined),
} as unknown as jest.Mocked<GarnetService>;
} as unknown as jest.Mocked<RedisService>;
return {
service: new RiotRateLimiterService(garnet),
garnet,
service: new RiotRateLimiterService(redis),
redis,
sendCommand,
};
}
@@ -51,12 +51,12 @@ describe('RiotRateLimiterService', () => {
it('records the 120 second app rate observation from Riot headers', async () => {
jest.spyOn(Date, 'now').mockReturnValue(1_000_000);
const { service, garnet } = makeService();
const { service, redis } = makeService();
await service.recordAppRateLimitObservation('47:120,3:1', '100:120,20:1');
expect(garnet.setString).toHaveBeenCalledTimes(1);
const [key, raw, ttl] = garnet.setString.mock.calls[0];
expect(redis.setString).toHaveBeenCalledTimes(1);
const [key, raw, ttl] = redis.setString.mock.calls[0];
expect(key).toBe('riot:app:rl:observed');
expect(ttl).toBe(300);
expect(JSON.parse(raw)).toEqual({
@@ -68,10 +68,10 @@ describe('RiotRateLimiterService', () => {
});
it('resets the inferred window when Riot count decreases or observation is stale', async () => {
const { service, garnet } = makeService();
const { service, redis } = makeService();
jest.spyOn(Date, 'now').mockReturnValue(1_060_000);
garnet.getString.mockResolvedValueOnce(
redis.getString.mockResolvedValueOnce(
observedState({
count120: 80,
observedAt: 1_050_000,
@@ -81,13 +81,13 @@ describe('RiotRateLimiterService', () => {
await service.recordAppRateLimitObservation('2:120', null);
expect(JSON.parse(garnet.setString.mock.calls[0][1])).toMatchObject({
expect(JSON.parse(redis.setString.mock.calls[0][1])).toMatchObject({
count120: 2,
windowStartAt: 1_060_000,
});
jest.spyOn(Date, 'now').mockReturnValue(1_240_000);
garnet.getString.mockResolvedValueOnce(
redis.getString.mockResolvedValueOnce(
observedState({
count120: 20,
observedAt: 1_120_000,
@@ -97,18 +97,18 @@ describe('RiotRateLimiterService', () => {
await service.recordAppRateLimitObservation('25:120', null);
expect(JSON.parse(garnet.setString.mock.calls[1][1])).toMatchObject({
expect(JSON.parse(redis.setString.mock.calls[1][1])).toMatchObject({
count120: 25,
windowStartAt: 1_240_000,
});
});
it('opens the refresh gate when there is no active observation', async () => {
const { service, garnet } = makeService();
const { service, redis } = makeService();
await expect(service.isRefreshWindowOpen(0.5)).resolves.toBe(true);
garnet.getString.mockResolvedValueOnce(
redis.getString.mockResolvedValueOnce(
observedState({
count120: 90,
observedAt: 1_000_000,
@@ -129,8 +129,8 @@ describe('RiotRateLimiterService', () => {
'checks tail headroom for refresh gate ratio=$ratio count=$count120',
async ({ ratio, count120, expected }) => {
jest.spyOn(Date, 'now').mockReturnValue(1_070_000);
const { service, garnet } = makeService();
garnet.getString.mockResolvedValue(
const { service, redis } = makeService();
redis.getString.mockResolvedValue(
observedState({
count120,
observedAt: 1_069_000,
@@ -144,8 +144,8 @@ describe('RiotRateLimiterService', () => {
it('keeps the refresh gate closed before the 120 second window tail', async () => {
jest.spyOn(Date, 'now').mockReturnValue(1_050_000);
const { service, garnet } = makeService();
garnet.getString.mockResolvedValue(
const { service, redis } = makeService();
redis.getString.mockResolvedValue(
observedState({
count120: 10,
observedAt: 1_049_000,
@@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import type { RiotPlatformRoute, RiotRegionalRoute } from './riot.types';
import { GarnetService } from '../global/garnet/garnet.service';
import { RedisService } from '../global/redis/redis.service';
const APP_LIMITS = [
{ windowMs: 1_000, max: 15 },
@@ -85,7 +85,7 @@ return 0
export class RiotRateLimiterService {
private readonly logger = new Logger(RiotRateLimiterService.name);
constructor(private readonly garnet: GarnetService) {}
constructor(private readonly redis: RedisService) {}
public async acquire(
route: RiotRegionalRoute | RiotPlatformRoute,
@@ -133,7 +133,7 @@ export class RiotRateLimiterService {
// Concurrent responses can race here. This observation is advisory, so
// last-writer-wins is acceptable and self-corrects on the next Riot header.
await this.garnet.setString(
await this.redis.setString(
APP_OBSERVED_KEY,
JSON.stringify(observation),
APP_OBSERVED_TTL_SECONDS,
@@ -167,7 +167,7 @@ export class RiotRateLimiterService {
now: number,
cost: number,
): Promise<number> {
const client = this.garnet.raw();
const client = this.redis.raw();
const member = `${process.pid}:${now}:${Math.random().toString(36).slice(2)}`;
const argumentsList = [
String(now),
@@ -190,7 +190,7 @@ export class RiotRateLimiterService {
}
private async getAppRateObservation(): Promise<AppRateObservation | null> {
const raw = await this.garnet.getString(APP_OBSERVED_KEY);
const raw = await this.redis.getString(APP_OBSERVED_KEY);
if (!raw) return null;
try {
@@ -4,7 +4,7 @@ import type {
RiotMatch as StoredRiotMatch,
} from '../global/database/schema';
import { ConnectionService } from '../global/database/connection.service';
import { GarnetService } from '../global/garnet/garnet.service';
import { RedisService } from '../global/redis/redis.service';
import type { RiotRegionalRoute } from './riot.types';
import { RiotMatchService } from './riot-match.service';
@@ -22,7 +22,7 @@ export class RiotRecentMatchesService {
constructor(
private readonly connectionService: ConnectionService,
private readonly matchService: RiotMatchService,
private readonly garnetService: GarnetService,
private readonly redisService: RedisService,
) {}
public async syncRecentMatches(params: {
@@ -37,7 +37,7 @@ export class RiotRecentMatchesService {
const lockKey = `riot:enrich:matches:${params.puuid}`;
const lockValue = `${process.pid}:${Date.now()}`;
const acquired = await this.garnetService.setStringIfAbsent(
const acquired = await this.redisService.setStringIfAbsent(
lockKey,
lockValue,
MATCH_ENRICHMENT_LOCK_TTL_SECONDS,
@@ -103,7 +103,7 @@ export class RiotRecentMatchesService {
return this.loadFromDb({ puuid: params.puuid, limit, queueId });
} finally {
await this.garnetService.delete(lockKey).catch((error: unknown) => {
await this.redisService.delete(lockKey).catch((error: unknown) => {
this.logger.warn(
`Failed to release recent matches lock for ${params.puuid}: ${
(error as Error)?.message ?? error