Этот коммит впервые фиксирует порт lab2 (приложение NestJS+Fastify, Dockerfile, одноservice-стенд compose, restart hook) и добавляет уровень 3/4 по lab3: - X-Instance-Id = INSTANCE_ID либо hostname контейнера (стабилен для процесса), добавляется и в ответах об ошибках, которые формируются до интерсептора. - /internal/lb-check: проба LB, намеренно не зависящая от PostgreSQL и Redis (отказ общей БД нельзя лечить выводом реплик из ротации); /health/ready остаётся операторским сигналом. /internal/metrics отдаёт instance_id и build_id. - Общая идемпотентность POST сообщения: таблица idempotency_keys, область (user, method, path, key), канонический отпечаток тела, заявка+результат в одной транзакции, повтор возвращает сохранённый ответ, другое тело — 409, отсутствие/неверный ключ — 400, просрочка по IDEMPOTENCY_TTL_SECONDS. - Транзиентные коды PostgreSQL (57014/40001/40P01) отображаются в 503, чтобы прерывание не фиксировалось идемпотентной записью как окончательный результат. - Миграции 003 (идемпотентность) и 004 (pg_stat_statements для трека 1), операторский прогон GC: npm run idempotency:gc.
206 lines
5.9 KiB
TypeScript
206 lines
5.9 KiB
TypeScript
import { Injectable, Logger } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
import type { AppConfig } from '../../config/app-config.schema.js';
|
|
import { RedisClient } from '../../redis/redis.client.js';
|
|
import type { Page } from '../../common/http/page.js';
|
|
import { MetricsService } from '../../metrics/metrics.service.js';
|
|
|
|
const LOCK_RETRY_COUNT = 4;
|
|
const LOCK_RETRY_DELAY_MS = 25;
|
|
|
|
export type CachePage = Page<unknown>;
|
|
|
|
@Injectable()
|
|
export class MessagesCacheService {
|
|
private readonly logger = new Logger(MessagesCacheService.name);
|
|
|
|
constructor(
|
|
private readonly redis: RedisClient,
|
|
private readonly config: ConfigService<AppConfig>,
|
|
private readonly metrics: MetricsService,
|
|
) {}
|
|
|
|
async getOrLoad(
|
|
chatId: string,
|
|
version: bigint,
|
|
cursor: string | undefined,
|
|
limit: number,
|
|
loader: () => Promise<CachePage>,
|
|
): Promise<CachePage> {
|
|
const key = this.cacheKey(chatId, version, cursor, limit);
|
|
if (this.config.getOrThrow('CACHE_MODE') === 'naive') {
|
|
return this.loadNaively(key, loader);
|
|
}
|
|
|
|
let cached: string | null;
|
|
try {
|
|
cached = await this.redis.get(key);
|
|
} catch (error) {
|
|
return this.fallbackToDatabase('read', error, loader);
|
|
}
|
|
const cachedPage = this.parsePage(cached);
|
|
if (cachedPage) {
|
|
this.metrics.increment('cache_hit');
|
|
return cachedPage;
|
|
}
|
|
this.metrics.increment('cache_miss');
|
|
|
|
const lockKey = `lock:${key}`;
|
|
const owner = `${process.pid}:${Math.random().toString(36).slice(2)}`;
|
|
for (let attempt = 0; attempt < LOCK_RETRY_COUNT; attempt += 1) {
|
|
let locked: boolean;
|
|
try {
|
|
locked = await this.redis.setNxPx(
|
|
lockKey,
|
|
owner,
|
|
this.config.getOrThrow('CACHE_LOCK_TTL_MS'),
|
|
);
|
|
} catch (error) {
|
|
return this.fallbackToDatabase('lock', error, loader);
|
|
}
|
|
if (locked) return this.fillLocked(key, lockKey, owner, loader);
|
|
|
|
if (attempt === 0) this.metrics.increment('cache_lock_wait');
|
|
try {
|
|
const waitedPage = this.parsePage(await this.redis.get(key));
|
|
if (waitedPage) {
|
|
this.metrics.increment('cache_hit');
|
|
return waitedPage;
|
|
}
|
|
} catch (error) {
|
|
return this.fallbackToDatabase('waiting read', error, loader);
|
|
}
|
|
await delay(LOCK_RETRY_DELAY_MS);
|
|
}
|
|
|
|
// The owner did not publish the page within the bounded wait: serve the
|
|
// authoritative result instead of blocking the request further.
|
|
this.metrics.increment('cache_lock_timeout');
|
|
return loader();
|
|
}
|
|
|
|
private async fillLocked(
|
|
key: string,
|
|
lockKey: string,
|
|
owner: string,
|
|
loader: () => Promise<CachePage>,
|
|
): Promise<CachePage> {
|
|
try {
|
|
let secondCheck: string | null;
|
|
try {
|
|
secondCheck = await this.redis.get(key);
|
|
} catch (error) {
|
|
return this.fallbackToDatabase('double-check', error, loader);
|
|
}
|
|
const secondPage = this.parsePage(secondCheck);
|
|
if (secondPage) {
|
|
this.metrics.increment('cache_hit');
|
|
return secondPage;
|
|
}
|
|
|
|
const page = await loader();
|
|
try {
|
|
await this.redis.set(key, JSON.stringify(page), {
|
|
EX: this.config.getOrThrow('CACHE_TTL_SECONDS'),
|
|
});
|
|
} catch (error) {
|
|
// Redis is derived state: the page is still valid for this response,
|
|
// but the failure must stay visible in metrics and logs.
|
|
this.metrics.increment('cache_write_error');
|
|
this.logger.warn(
|
|
`Cache fill failed: ${error instanceof Error ? error.message : 'unknown error'}`,
|
|
);
|
|
}
|
|
return page;
|
|
} finally {
|
|
try {
|
|
await this.redis.unlock(lockKey, owner);
|
|
} catch (error) {
|
|
// The lock has a TTL, so a stale lock expires on its own; a successful
|
|
// response must not be replaced by an unlock failure.
|
|
this.metrics.increment('cache_unlock_error');
|
|
this.logger.warn(
|
|
`Cache unlock failed: ${error instanceof Error ? error.message : 'unknown error'}`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
private cacheKey(
|
|
chatId: string,
|
|
version: bigint,
|
|
cursor: string | undefined,
|
|
limit: number,
|
|
): string {
|
|
return `messages:${chatId}:v${version.toString()}:c${cursor ?? 'start'}:l${limit}`;
|
|
}
|
|
|
|
private async loadNaively(
|
|
key: string,
|
|
loader: () => Promise<CachePage>,
|
|
): Promise<CachePage> {
|
|
let cached: string | null;
|
|
try {
|
|
cached = await this.redis.get(key);
|
|
} catch (error) {
|
|
return this.fallbackToDatabase('naive read', error, loader);
|
|
}
|
|
const cachedPage = this.parsePage(cached);
|
|
if (cachedPage) {
|
|
this.metrics.increment('cache_hit');
|
|
return cachedPage;
|
|
}
|
|
this.metrics.increment('cache_miss');
|
|
|
|
const cacheMissDelay = this.config.getOrThrow('CACHE_MISS_DELAY_MS');
|
|
if (cacheMissDelay > 0) await delay(cacheMissDelay);
|
|
const page = await loader();
|
|
try {
|
|
await this.redis.set(key, JSON.stringify(page), {
|
|
EX: this.config.getOrThrow('CACHE_TTL_SECONDS'),
|
|
});
|
|
} catch (error) {
|
|
this.metrics.increment('cache_write_error');
|
|
this.logger.warn(
|
|
`Cache fill failed: ${error instanceof Error ? error.message : 'unknown error'}`,
|
|
);
|
|
}
|
|
return page;
|
|
}
|
|
|
|
private async fallbackToDatabase(
|
|
stage: string,
|
|
error: unknown,
|
|
loader: () => Promise<CachePage>,
|
|
): Promise<CachePage> {
|
|
this.metrics.increment('cache_fallback');
|
|
this.logger.warn(
|
|
`Cache ${stage} failed, serving PostgreSQL: ${error instanceof Error ? error.message : 'unknown error'}`,
|
|
);
|
|
return loader();
|
|
}
|
|
|
|
private parsePage(value: string | null): CachePage | undefined {
|
|
if (!value) return undefined;
|
|
try {
|
|
const parsed: unknown = JSON.parse(value);
|
|
if (
|
|
typeof parsed !== 'object' ||
|
|
parsed === null ||
|
|
!('items' in parsed) ||
|
|
!('next_cursor' in parsed) ||
|
|
!Array.isArray(parsed.items) ||
|
|
(parsed.next_cursor !== null &&
|
|
typeof parsed.next_cursor !== 'string')
|
|
)
|
|
return undefined;
|
|
return { items: parsed.items, next_cursor: parsed.next_cursor };
|
|
} catch {
|
|
// A malformed entry is treated as a miss and overwritten by the owner fill.
|
|
this.metrics.increment('cache_corrupt_entry');
|
|
return undefined;
|
|
}
|
|
}
|
|
}
|