lab3: итог

This commit is contained in:
2026-10-16 16:18:15 +03:00
parent 6cced49486
commit e42f02c016
141 changed files with 3180 additions and 4753 deletions
@@ -22,37 +22,43 @@ const PURGE_BATCHES = 10;
export class IdempotencyRepository {
constructor(private readonly database: DatabaseClient) {}
/**
* Reads the stored result and re-checks access in the same round trip: a
* replay must still prove that the caller may read this chat.
*/
async read(
userId: string,
path: string,
key: string,
chatId: string,
ttlSeconds: number,
): Promise<IdempotencyRecord | undefined> {
const result = await this.database.query<IdempotencyRecord>(
'idempotency.read',
`SELECT k.request_fingerprint, k.status_code, k.response_body, k.created_at,
/**
* Reads the stored result and re-checks access in the same round trip: a
* replay must still prove that the caller may read this chat.
*/
async read(
userId: string,
path: string,
key: string,
chatId: string,
ttlSeconds: number,
): Promise<IdempotencyRecord | undefined> {
const result = await this.database.query<IdempotencyRecord>(
'idempotency.read',
`SELECT k.request_fingerprint, k.status_code, k.response_body, k.created_at,
k.created_at < now() - make_interval(secs => $4::double precision) AS expired,
EXISTS (
SELECT 1 FROM chat_members WHERE chat_id = $5 AND user_id = $1
) AS is_member
FROM idempotency_keys k
WHERE k.user_id = $1 AND k.method = 'POST' AND k.path = $2 AND k.key = $3`,
[userId, path, key, ttlSeconds, chatId],
);
return result.rows[0];
}
[userId, path, key, ttlSeconds, chatId],
);
return result.rows[0];
}
async forget(userId: string, path: string, key: string): Promise<void> {
async forget(
userId: string,
path: string,
key: string,
ttlSeconds: number,
): Promise<void> {
await this.database.query(
'idempotency.forget',
`DELETE FROM idempotency_keys
WHERE user_id = $1 AND method = 'POST' AND path = $2 AND key = $3`,
[userId, path, key],
WHERE user_id = $1 AND method = 'POST' AND path = $2 AND key = $3
AND created_at < now() - make_interval(secs => $4::double precision)`,
[userId, path, key, ttlSeconds],
);
}