Precharge les mappings IMDb au warmup (1.27s -> 16ms sur le premier appel /api?t=imdb)

This commit is contained in:
unfr
2026-04-24 07:47:23 +02:00
parent 4687b83cde
commit fbf99a9ccf
2 changed files with 15 additions and 1 deletions

View File

@@ -33,3 +33,12 @@ export async function lookupImdb(imdbId) {
}
return null;
}
// Eager-load both mappings — used at server startup to remove the first-request
// 1s latency. Returns the number of entries loaded per type.
export async function preloadMappings() {
const out = { movie: 0, tv: 0 };
try { out.movie = (await loadOne('movie')).size; } catch { /* missing */ }
try { out.tv = (await loadOne('tv')).size; } catch { /* missing */ }
return out;
}

View File

@@ -5,6 +5,7 @@ import secureSession from '@fastify/secure-session';
import fastifyStatic from '@fastify/static';
import Fastify from 'fastify';
import { HOST, PORT, RATE_LIMIT_PER_SEC, ROOT, SESSION_SECRET } from './config.js';
import { preloadMappings } from './lib/imdbMapping.js';
import { getRatings } from './lib/imdbRatings.js';
import { httpDuration, httpRequests, imdbRatingsCount, searchWorkers } from './lib/metrics.js';
import { getPool } from './lib/searchEngine.js';
@@ -80,7 +81,11 @@ fastify.ready().then(async () => {
const tv = getPool('tv');
searchWorkers.set({ type: 'movie' }, movie.workers.length);
searchWorkers.set({ type: 'tv' }, tv.workers.length);
fastify.log.info({ ratings: ratings.size }, 'Warmup complete');
const maps = await preloadMappings();
fastify.log.info(
{ ratings: ratings.size, imdb_movie: maps.movie, imdb_tv: maps.tv },
'Warmup complete',
);
} catch (err) {
fastify.log.warn({ err }, 'Warmup failed');
}