Public: anyone can read this; the crew can change it.

One signed-in account cannot hold more than its share of event streams #14982

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/14961-one-signed-in-account-cannot-hold-more-t → main 3 commits fork

  • Merged. torque/14961-one-signed-in-account-cannot-hold-more-t was merged into main as 53d5bf3.

Review

Sign in to review this change.
6 files changed +98 −15
src/http/caller.ts +10
@@ -50,6 +50,16 @@ export const noteCaller = (
5050 callers.set(request, { db, userId, sessionId });
5151 };
5252
53 /** The signed-in account behind a request, when auth found one. */
54 export const callerAccount = (request: Request): string | undefined =>
55 callers.get(request)?.userId;
56
57 /** A request rebuilt from another (a replay) is the same caller. */
58 export const carryCaller = (from: Request, to: Request): void => {
59 const caller = callers.get(from);
60 if (caller !== undefined) callers.set(to, caller);
61 };
62
5363 function hold(held: Held, key: string, close: () => void): () => void {
5464 let set = held.get(key);
5565 if (set === undefined) held.set(key, (set = new Set()));
src/http/limits.ts −3
@@ -49,9 +49,6 @@ export const noteSignedIn = (request: Request): void => {
4949 signedIn.add(request);
5050 };
5151
52 /** Whether the request carries a signed-in person or a token. */
53 export const isSignedIn = (request: Request): boolean => signedIn.has(request);
54
5552 /** The most a shared reader keeps of this request's body. */
5653 export const bodyCapFor = (request: Request): number =>
5754 signedIn.has(request) ? BODY_MAX : STRANGER_MAX;
src/http/server.ts +2
@@ -71,6 +71,7 @@ import { actingVia } from "../core/acting_via.ts";
7171 import {
7272 closeStreamsOf,
7373 closeStreamsOfSession,
74 carryCaller,
7475 noteCaller,
7576 sweepStanding,
7677 } from "./caller.ts";
@@ -2255,6 +2256,7 @@ async function withIdempotency(
22552256 // still see what was noted of the original.
22562257 carryPeer(request, again);
22572258 carryNotes(request, again);
2259 carryCaller(request, again);
22582260 return again;
22592261 };
22602262
src/http/sse.ts +4 −4
@@ -14,8 +14,7 @@
1414 * timer.
1515 */
1616
17 import { holdForCaller } from "./caller.ts";
18 import { isSignedIn } from "./limits.ts";
17 import { callerAccount, holdForCaller } from "./caller.ts";
1918 import { holdStream, tooManyStrangers } from "./strangers.ts";
2019 import type { App } from "../app.ts";
2120 import type { User } from "../core/users.ts";
@@ -88,7 +87,8 @@ export type Emit = (event: string, data: unknown, id?: string) => void;
8887 * `close` is called, or when a write fails — whichever happens first.
8988 *
9089 * Every stream is counted on `app` while it is open (`strangers.ts`): a
91 * stranger's against its address and every stranger's together, anyone's
90 * stranger's against its address and every stranger's together, a
91 * signed-in account's against the account and its address, anyone's
9292 * against the forge's whole budget. Past either, the answer is a 429 and
9393 * nothing is registered.
9494 */
@@ -97,7 +97,7 @@ export function eventStream(
9797 request: Request,
9898 register: (emit: Emit, close: () => void, ready: () => boolean) => () => void,
9999 ): Response {
100 const held = holdStream(app, request, !isSignedIn(request));
100 const held = holdStream(app, request, callerAccount(request));
101101 if (!held.allowed) return tooManyStrangers(held);
102102 const encoder = new TextEncoder();
103103 let cleanup = () => {};
src/http/strangers.ts +30 −7
@@ -54,6 +54,19 @@ export interface StrangerLimits {
5454 readonly streamsPerAddress: number;
5555 /** Event streams strangers may hold open, every one together. */
5656 readonly strangerStreams: number;
57 /**
58 * Event streams one signed-in account may hold open, whatever its tokens
59 * and browsers: dozens of tabs, each with its page's streams, and an agent
60 * or two watching runs, with room to spare — not a share of `allStreams`.
61 */
62 readonly streamsPerAccount: number;
63 /**
64 * Event streams signed-in callers from one address may hold open together,
65 * so one address cannot fill the pool by minting accounts. Counted apart
66 * from strangers' `streamsPerAddress`: an office behind one address holds
67 * several people's tabs.
68 */
69 readonly signedStreamsPerAddress: number;
5770 /** Event streams anyone may hold open on this forge. */
5871 readonly allStreams: number;
5972 }
@@ -68,6 +81,8 @@ export const STRANGER_LIMITS: StrangerLimits = {
6881 codeAtOnce: 1,
6982 streamsPerAddress: 8,
7083 strangerStreams: 256,
84 streamsPerAccount: 64,
85 signedStreamsPerAddress: 256,
7186 allStreams: 4096,
7287 };
7388
@@ -298,29 +313,37 @@ function codeSearchAt(app: App, bucket: string): Held | Refused {
298313 }
299314
300315 /**
301 * An event stream: every one counts toward `allStreams`; a stranger's also
302 * toward its address and toward every stranger's together.
316 * An event stream: every one counts toward `allStreams`. A stranger's
317 * (`account` undefined) also toward its address and toward every stranger's
318 * together; a signed-in caller's toward its account and toward what signed-in
319 * callers from its address hold, so no one account — nor one address with
320 * many — holds the pool every other account streams from.
303321 */
304322 export function holdStream(
305323 app: App,
306324 request: Request,
307 stranger: boolean,
325 account: string | undefined,
308326 ): Held | Refused {
309327 const state = stateOf(app);
310328 const { limits } = state;
329 const bucket = clientBucket(request);
311330 const release = holdAll(
312331 state,
313 stranger
332 account === undefined
314333 ? [
315334 ["stream:*", limits.allStreams],
316335 ["stream:strangers", limits.strangerStreams],
317 [`stream:${clientBucket(request)}`, limits.streamsPerAddress],
336 [`stream:${bucket}`, limits.streamsPerAddress],
318337 ]
319 : [["stream:*", limits.allStreams]],
338 : [
339 ["stream:*", limits.allStreams],
340 [`stream:account:${account}`, limits.streamsPerAccount],
341 [`stream:signed:${bucket}`, limits.signedStreamsPerAddress],
342 ],
320343 );
321344 if (release === undefined)
322345 return refused(
323 stranger
346 account === undefined
324347 ? "too many open streams for visitors who are not signed in"
325348 : "too many open streams",
326349 30,
test/strangers.test.ts +52 −1
@@ -16,7 +16,7 @@ import { createApp, type App } from "../src/app.ts";
1616 import { createRepo } from "../src/actions/repos.ts";
1717 import { createApp as createAppRecord } from "../src/core/apps.ts";
1818 import { createToken } from "../src/core/tokens.ts";
19 import type { User } from "../src/core/users.ts";
19 import { createUser, type User } from "../src/core/users.ts";
2020 import { transferGate } from "../src/git/env.ts";
2121 import { serve, type Servers } from "../src/http/server.ts";
2222 import {
@@ -441,6 +441,57 @@ describe("strangers at every door", () => {
441441 expect(await until(() => heldNow(app, "stream:*") === 0)).toBe(true);
442442 });
443443
444 test("event streams: per signed-in account and per signed-in address, beside strangers'", async () => {
445 setStrangerLimits(app, { streamsPerAccount: 2, signedStreamsPerAddress: 3 });
446 const bearer = async (username: string): Promise<string> => {
447 const made = await createUser(app.db, {
448 origin: "seed",
449 trusted: true,
450 agent: true,
451 username,
452 email: `${username}@f.dev`,
453 });
454 if (!made.ok) throw new Error(made.error);
455 return createToken(app.db, made.value.id, "t", "ro").token;
456 };
457 const [bob, carol, dave] = [await bearer("bob"), await bearer("carol"), await bearer("dave")];
458 // A second token is the same account: the cap is the account's, not the token's.
459 const ada2 = createToken(app.db, ada.id, "t2", "ro").token;
460 const open: AbortController[] = [];
461 const stream = async (ip: string, bearerToken: string): Promise<number> => {
462 const abort = new AbortController();
463 open.push(abort);
464 const res = await fetch(`${base}/events`, {
465 headers: from(ip, bearerToken === "" ? {} : { authorization: `Bearer ${bearerToken}` }),
466 signal: abort.signal,
467 });
468 if (res.status !== 200) await res.arrayBuffer();
469 return res.status;
470 };
471 try {
472 const [a, b, c, d, e] = [address(), address(), address(), address(), address()];
473 expect(await stream(a, token)).toBe(200);
474 expect(await stream(b, ada2)).toBe(200);
475 // Ada holds her two from wherever she asks, with whichever token.
476 expect(await stream(c, token)).toBe(429);
477 expect(heldNow(app, `stream:account:${ada.id}`)).toBe(2);
478 // Another account still connects.
479 expect(await stream(c, bob)).toBe(200);
480 // One address holds three signed-in streams, however many accounts.
481 expect(await stream(d, bob)).toBe(200);
482 expect(await stream(d, carol)).toBe(200);
483 expect(await stream(d, carol)).toBe(200);
484 expect(await stream(d, dave)).toBe(429);
485 expect(await stream(e, dave)).toBe(200);
486 // A stranger at that address is counted apart from who signed in there.
487 expect(await stream(d, "")).not.toBe(429);
488 } finally {
489 for (const abort of open) abort.abort();
490 setStrangerLimits(app, STRANGER_LIMITS);
491 }
492 expect(await until(() => heldNow(app, "stream:*") === 0)).toBe(true);
493 });
494
444495 test("a stranger's spawn plans are counted per address", async () => {
445496 setStrangerLimits(app, { perAddress: { ...STRANGER_LIMITS.perAddress, plan: 2 } });
446497 try {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs