mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
refactor(server): make the state ports async and route every reader through the provided store
The process-state ports were meant to let a store shared between processes replace the in-memory one, but they could not. The readers outside the container (the broadcast functions, the backup restore's permissions-cache flush, the pending-code sweep, hand-built consumers) went straight to the module-level in-memory instances, so an overridden provider was used by the gateway and the services while the broadcasts and the restore flush kept using the old store. And every port was synchronous, while a store in the database or on the network is async, so plugging one in would have changed every signature anyway. RateLimitStore, PendingCodeStore, OidcFlowStore and PermissionsCacheStore now return promises, and RateLimitService, OauthService, OidcService, PermissionsService and their callers await them. The in-memory stores still decide synchronously, so who gets a 429, a code or a cached level is unchanged. RoomRegistry stays synchronous: membership holds live sockets and broadcast() is a synchronous call every domain makes, so several processes need a fan-out in front of broadcast(), not a different place for the sets. The process-wide ports get a ProcessStoreSlot. PermissionsModule, OauthModule and RealtimeGatewayModule install the store the container resolved and release it on shutdown, and the free functions, the sweep and hand-built consumers read the slot, so overriding the provider changes every reader. Tests provide fake stores and check that the broadcasts, the restore flush and the sweep use them. Two callers needed more than an await. The transit MCP tools counted in a RateLimitService of their own that no provider could reach; they now take the one RateLimitModule provides (their mcp_transit_* buckets never overlap the REST ones). The link preview claimed its in-flight slot only after the budget check, which now yields, so simultaneous askers would each have been charged; it now claims the slot first, and an asker that joined someone out of budget asks again on its own budget. The unused putPendingCode/takePendingCode helpers are gone.
This commit is contained in:
@@ -646,18 +646,20 @@ export class CollabService {
|
||||
// entry yet, since the first has not answered. Joining the running fetch keeps
|
||||
// that a single outbound request instead of twenty.
|
||||
const running = this.inFlight.get(url);
|
||||
if (running !== undefined) return { ...(await running), url };
|
||||
|
||||
// Charged per outbound fetch rather than per request, which is what the
|
||||
// budget is actually protecting. Without a user there is no one to charge —
|
||||
// no caller passes that today, and the fetch stays behind the SSRF guard.
|
||||
if (userId !== undefined && !this.rateLimit.check('collab_link_preview', String(userId), PREVIEW_FETCHES_PER_MINUTE, 60_000, Date.now())) {
|
||||
return { ...fallback, rateLimited: true };
|
||||
if (running !== undefined) {
|
||||
const shared = await running;
|
||||
// The asker this call joined was out of budget, which says nothing about
|
||||
// this caller's: ask again, first in line or behind the next asker.
|
||||
if (shared.rateLimited) return this.linkPreview(url, userId);
|
||||
return { ...shared, url };
|
||||
}
|
||||
|
||||
// Memoised in-flight fetch, not a missing await: the promise is stored so the
|
||||
// concurrent askers above can join it, and this frame awaits it below.
|
||||
const task = this.fetchPreview(url, fallback);
|
||||
// concurrent askers above can join it, and this frame awaits it below. It is
|
||||
// stored before the budget check, which awaits its store, so an asker that
|
||||
// arrives meanwhile joins this one instead of being charged for a fetch it
|
||||
// does not make.
|
||||
const task = this.chargeAndFetch(url, fallback, userId);
|
||||
this.inFlight.set(url, task);
|
||||
try {
|
||||
return await task;
|
||||
@@ -666,6 +668,18 @@ export class CollabService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Charged per outbound fetch rather than per request, which is what the
|
||||
* budget is actually protecting. Without a user there is no one to charge:
|
||||
* no caller passes that today, and the fetch stays behind the SSRF guard.
|
||||
*/
|
||||
private async chargeAndFetch(url: string, fallback: LinkPreviewResult, userId?: number): Promise<LinkPreviewResult> {
|
||||
if (userId !== undefined && !(await this.rateLimit.check('collab_link_preview', String(userId), PREVIEW_FETCHES_PER_MINUTE, 60_000, Date.now()))) {
|
||||
return { ...fallback, rateLimited: true };
|
||||
}
|
||||
return this.fetchPreview(url, fallback);
|
||||
}
|
||||
|
||||
/** The outbound half of linkPreview, past the cache and the budget. */
|
||||
private async fetchPreview(url: string, fallback: LinkPreviewResult): Promise<LinkPreviewResult> {
|
||||
const ssrf = await checkSsrf(url, true);
|
||||
|
||||
Reference in New Issue
Block a user