feat: redesign auction engine with redis sot
This commit is contained in:
1 parent
737702d78c
commit
e8bb20d988
75 files changed
+12737
-542
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { GarnetService } from '../global/garnet/garnet.service';
|
||||
import { RedisService } from '../global/redis/redis.service';
|
||||
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
|
||||
import { RiotRateLimiterService } from '../riot/riot-rate-limiter.service';
|
||||
import { BackgroundRefreshScheduler } from './background-refresh.scheduler';
|
||||
@@ -33,13 +33,13 @@ function makeScheduler() {
|
||||
const rawClient = {
|
||||
sendCommand: jest.fn().mockResolvedValue(1),
|
||||
};
|
||||
const garnet = {
|
||||
const redis = {
|
||||
getString: jest.fn().mockResolvedValue(null),
|
||||
setString: jest.fn().mockResolvedValue(undefined),
|
||||
setStringIfAbsent: jest.fn().mockResolvedValue(true),
|
||||
delete: jest.fn().mockResolvedValue(1),
|
||||
raw: jest.fn().mockReturnValue(rawClient),
|
||||
} as unknown as jest.Mocked<GarnetService> & { raw: jest.Mock };
|
||||
} as unknown as jest.Mocked<RedisService> & { raw: jest.Mock };
|
||||
const rateLimiter = {
|
||||
isRefreshWindowOpen: jest.fn().mockResolvedValue(true),
|
||||
} as unknown as jest.Mocked<RiotRateLimiterService>;
|
||||
@@ -51,7 +51,7 @@ function makeScheduler() {
|
||||
findStaleAccounts: jest.fn().mockResolvedValue([]),
|
||||
} as unknown as jest.Mocked<StaleAccountRepository>;
|
||||
const scheduler = new BackgroundRefreshScheduler(
|
||||
garnet,
|
||||
redis,
|
||||
rateLimiter,
|
||||
enrichmentService,
|
||||
staleAccountRepository,
|
||||
@@ -59,7 +59,7 @@ function makeScheduler() {
|
||||
|
||||
return {
|
||||
scheduler,
|
||||
garnet,
|
||||
redis,
|
||||
rawClient,
|
||||
rateLimiter,
|
||||
enrichmentService,
|
||||
@@ -115,8 +115,8 @@ describe('BackgroundRefreshScheduler', () => {
|
||||
|
||||
it('does not query stale accounts when another leader holds the lock', async () => {
|
||||
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T18:00:00.000Z'));
|
||||
const { scheduler, garnet, staleAccountRepository } = makeScheduler();
|
||||
garnet.getString.mockResolvedValue('another-consumer');
|
||||
const { scheduler, redis, staleAccountRepository } = makeScheduler();
|
||||
redis.getString.mockResolvedValue('another-consumer');
|
||||
|
||||
await scheduler.tick();
|
||||
|
||||
@@ -193,12 +193,12 @@ describe('BackgroundRefreshScheduler', () => {
|
||||
|
||||
it('skips candidates already in failure backoff', async () => {
|
||||
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T18:00:00.000Z'));
|
||||
const { scheduler, garnet, enrichmentService, staleAccountRepository } =
|
||||
const { scheduler, redis, enrichmentService, staleAccountRepository } =
|
||||
makeScheduler();
|
||||
staleAccountRepository.findStaleAccounts.mockResolvedValue([
|
||||
candidate('skip-me'),
|
||||
]);
|
||||
garnet.getString.mockImplementation(async (key: string) => {
|
||||
redis.getString.mockImplementation(async (key: string) => {
|
||||
if (key.includes('riot:bg:fail:skip-me')) return '3';
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
RiotAccountEnrichmentService,
|
||||
} from '../riot/riot-account-enrichment.service';
|
||||
import { RiotRateLimiterService } from '../riot/riot-rate-limiter.service';
|
||||
import { GarnetService } from '../global/garnet/garnet.service';
|
||||
import { RedisService } from '../global/redis/redis.service';
|
||||
import { isInKstBackgroundWindow } from './kst-window.util';
|
||||
import { StaleAccountRepository } from './stale-account.repository';
|
||||
|
||||
@@ -41,7 +41,7 @@ export class BackgroundRefreshScheduler
|
||||
private readonly consumer = `${process.pid}:${Math.random().toString(36).slice(2)}`;
|
||||
|
||||
constructor(
|
||||
private readonly garnet: GarnetService,
|
||||
private readonly redis: RedisService,
|
||||
private readonly rateLimiter: RiotRateLimiterService,
|
||||
private readonly enrichmentService: RiotAccountEnrichmentService,
|
||||
private readonly staleAccountRepository: StaleAccountRepository,
|
||||
@@ -158,9 +158,9 @@ export class BackgroundRefreshScheduler
|
||||
}
|
||||
|
||||
private async tryBecomeLeader(): Promise<boolean> {
|
||||
const current = await this.garnet.getString(LEADER_KEY);
|
||||
const current = await this.redis.getString(LEADER_KEY);
|
||||
if (current === this.consumer) {
|
||||
await this.garnet.setString(
|
||||
await this.redis.setString(
|
||||
LEADER_KEY,
|
||||
this.consumer,
|
||||
LEADER_TTL_SECONDS,
|
||||
@@ -169,7 +169,7 @@ export class BackgroundRefreshScheduler
|
||||
}
|
||||
if (current) return false;
|
||||
|
||||
return this.garnet.setStringIfAbsent(
|
||||
return this.redis.setStringIfAbsent(
|
||||
LEADER_KEY,
|
||||
this.consumer,
|
||||
LEADER_TTL_SECONDS,
|
||||
@@ -178,9 +178,9 @@ export class BackgroundRefreshScheduler
|
||||
|
||||
private async renewLeaderIfHeld(): Promise<void> {
|
||||
try {
|
||||
const current = await this.garnet.getString(LEADER_KEY);
|
||||
const current = await this.redis.getString(LEADER_KEY);
|
||||
if (current !== this.consumer) return;
|
||||
await this.garnet.setString(
|
||||
await this.redis.setString(
|
||||
LEADER_KEY,
|
||||
this.consumer,
|
||||
LEADER_TTL_SECONDS,
|
||||
@@ -200,7 +200,7 @@ export class BackgroundRefreshScheduler
|
||||
}
|
||||
|
||||
private async getFailureCount(puuid: string): Promise<number> {
|
||||
const raw = await this.garnet.getString(this.failureBackoffKey(puuid));
|
||||
const raw = await this.redis.getString(this.failureBackoffKey(puuid));
|
||||
const count = Number(raw ?? 0);
|
||||
if (!Number.isFinite(count) || count < 0) return 0;
|
||||
return count;
|
||||
@@ -208,7 +208,7 @@ export class BackgroundRefreshScheduler
|
||||
|
||||
private async bumpFailureCounter(puuid: string): Promise<void> {
|
||||
const key = this.failureBackoffKey(puuid);
|
||||
const client = this.garnet.raw();
|
||||
const client = this.redis.raw();
|
||||
const response = await client.sendCommand(['INCR', key]);
|
||||
await client.sendCommand([
|
||||
'EXPIRE',
|
||||
@@ -225,7 +225,7 @@ export class BackgroundRefreshScheduler
|
||||
}
|
||||
|
||||
private async clearFailureCounter(puuid: string): Promise<void> {
|
||||
await this.garnet.delete(this.failureBackoffKey(puuid));
|
||||
await this.redis.delete(this.failureBackoffKey(puuid));
|
||||
}
|
||||
|
||||
private isFailedEnrichment(result: EnrichResult): boolean {
|
||||
|
||||
Reference in new issue
Block a user