forked from mtusi2026/mtusi2026lab6
Перенесено дерево lab5 целиком: NestJS-приложение (API + worker,
RabbitMQ, outbox, S3), Dockerfile, compose.yaml, infra/{haproxy,minio,
rabbitmq}, стендовые скрипты, scripts/lab4, scripts/lab5,
evidence/lab4, evidence/lab5 и contracts/extensions.openapi.json.
Задание lab6 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,
examples,observability}, tests, compose.example.yaml, infra/otel,
.github/pull_request_template.md.
Адаптации при переносе:
- docker-проект mtusi-lab5 -> mtusi-lab6 и тег образа
mtusi-messenger:lab6 (compose.yaml, scripts/stop-one.sh, .env.example,
infra/README.md), чтобы тома и контейнеры не пересекались с lab5;
- Makefile: цели lab5 сохранены целиком (skeleton lab6 не отличается
от skeleton lab5);
- .env.example: переменные приложения и стенда lab5 + блок lab6
(OTEL_*) из skeleton.
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;
|
|
}
|
|
}
|
|
}
|