mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
fix(plugins): hold the write lock for the plugin cap checks on Postgres
meta.set and scheduler.set checked their caps in a transaction of the repository's own, which keeps check and write together on SQLite's single writer but not on Postgres, where two pool connections both read the same count. Both RPCs now run the capped write inside UnitOfWork.transactional, which takes the engine's write lock; the repository's transaction becomes a savepoint of it.
This commit is contained in:
@@ -11,6 +11,7 @@ import { BadParams, ForbiddenResource } from '../../../../nest-rpc/rpc-errors';
|
||||
import { PluginController, PluginMethod } from '../../../../nest-rpc/rpc-kit/decorators';
|
||||
import type { PluginRpcContext } from '../../../../nest-rpc/rpc-kit/types';
|
||||
import { asPayload, num, str } from '../../../../nest-rpc/rpc-params';
|
||||
import { UnitOfWork } from '../../../database/unit-of-work';
|
||||
import { UnreadableLlmResponse } from '../../../llm-parse/clients/openai-compatible.client';
|
||||
import { createLlmClient } from '../../../llm-parse/llm-client.factory';
|
||||
import type { ResolvedLlmConfig } from '../../../llm-parse/llm-config';
|
||||
@@ -58,6 +59,7 @@ export class HostSurfaceRpc {
|
||||
@InjectRepository(Trips) private readonly trips: TripsRepository,
|
||||
// HR5–HR8 (Plan 3j Task 5) — a plugin's own scheduler.set/scheduler.cancel RPCs.
|
||||
@InjectRepository(PluginScheduledTasks) private readonly scheduledTasks: PluginScheduledTasksRepository,
|
||||
private readonly uow: UnitOfWork,
|
||||
) {}
|
||||
|
||||
@PluginMethod('users.getById', { permission: 'db:read:users' })
|
||||
@@ -212,11 +214,16 @@ export class HostSurfaceRpc {
|
||||
throw new BadParams(`scheduler payload too large (max ${SCHED_PAYLOAD_MAX} bytes)`);
|
||||
// HR5/HR6/HR7 — Plan 3j Task 7 fix (must-land 3): one atomic call, not
|
||||
// three separately-awaited ones (task-7-review.md's concurrent-cap-bypass).
|
||||
// Upsert by (plugin, name): re-scheduling the same name replaces it.
|
||||
const written = await this.scheduledTasks.upsertTaskCapped(
|
||||
{ plugin_id: ctx.pluginId, name, due_at: Math.max(dueAt, Date.now()), payload: json, every_ms: everyMs ?? null },
|
||||
SCHED_MAX,
|
||||
);
|
||||
// Upsert by (plugin, name): re-scheduling the same name replaces it. In a
|
||||
// unit of work, whose write lock keeps the cap check atomic on Postgres too.
|
||||
const task = {
|
||||
plugin_id: ctx.pluginId,
|
||||
name,
|
||||
due_at: Math.max(dueAt, Date.now()),
|
||||
payload: json,
|
||||
every_ms: everyMs ?? null,
|
||||
};
|
||||
const written = await this.uow.transactional(() => this.scheduledTasks.upsertTaskCapped(task, SCHED_MAX));
|
||||
if (!written) throw new BadParams(`too many scheduled tasks (max ${SCHED_MAX})`);
|
||||
return { scheduled: true };
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import { BadParams, ForbiddenResource } from '../../../../nest-rpc/rpc-errors';
|
||||
import { PluginController, PluginMethod } from '../../../../nest-rpc/rpc-kit/decorators';
|
||||
import type { PluginRpcContext } from '../../../../nest-rpc/rpc-kit/types';
|
||||
import { num, str } from '../../../../nest-rpc/rpc-params';
|
||||
import { UnitOfWork } from '../../../database/unit-of-work';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
|
||||
/** Core entities a plugin may attach its own db:meta to. */
|
||||
@@ -57,6 +58,7 @@ export class MetaRpc {
|
||||
@InjectRepository(Days) private readonly days: DaysRepository,
|
||||
@InjectRepository(Reservations) private readonly reservations: ReservationsRepository,
|
||||
@InjectRepository(DayAccommodations) private readonly dayAccommodations: DayAccommodationsRepository,
|
||||
private readonly uow: UnitOfWork,
|
||||
) {}
|
||||
|
||||
@PluginMethod('meta.get', { permission: 'db:meta' })
|
||||
@@ -79,8 +81,11 @@ export class MetaRpc {
|
||||
const json = JSON.stringify(params.value ?? null);
|
||||
if (json.length > META_VALUE_MAX) throw new BadParams(`metadata value too large (>${META_VALUE_MAX} bytes)`);
|
||||
// MR2/MR3/MR4 — Plan 3j Task 7 fix (must-land 2): one atomic call, not
|
||||
// three separately-awaited ones (task-7-review.md's concurrent-cap-bypass).
|
||||
const written = await this.meta.upsertValueCapped(ctx.pluginId, entityType, entityId, key, json, META_KEYS_MAX);
|
||||
// three separately-awaited ones (task-7-review.md's concurrent-cap-bypass),
|
||||
// in a unit of work, whose write lock keeps it atomic on Postgres's pool too.
|
||||
const written = await this.uow.transactional(() =>
|
||||
this.meta.upsertValueCapped(ctx.pluginId, entityType, entityId, key, json, META_KEYS_MAX),
|
||||
);
|
||||
if (!written) throw new BadParams(`too many metadata keys on this ${entityType} (max ${META_KEYS_MAX})`);
|
||||
return { key, value: params.value ?? null };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user