Skip to content

Commit 14325bd

Browse files
author
am
committed
fix: bound optional community icon requests
Co-authored-by: Kalvin Chau <[email protected]> Signed-off-by: Kalvin Chau <[email protected]>
1 parent 203c95c commit 14325bd

4 files changed

Lines changed: 110 additions & 21 deletions

File tree

‎docs/shell-design.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ page scaffold with only a centered title; Apps waits for a functional destinatio
9191
beside page content; it only delegates selection to the existing membership owner.
9292
The rail's Add control opens the existing join dialog and returns focus to its
9393
trigger. The former header picker is not mounted; the rail is the sole selector.
94-
The rail reads each saved community's icon from its relay's NIP-11 info via the
95-
same-origin broker, including inactive communities without opening a session.
94+
The rail reads saved-community NIP-11 icons through the same-origin broker with at
95+
most two concurrent optional reads, including inactive communities without
96+
opening sessions; slow icon responses cannot occupy all foreground connections.
9697
Unavailable or unsupported images fall back to a saved icon or name initial.
9798
The rail does not acquire inactive sessions or claim an unread total: the unread
9899
capability provides bounded observed evidence, not exact community totals

‎src/features/communities/CommunityRail.tsx‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,27 @@ export function CommunityRail({
3434
useEffect(() => {
3535
if (client.status !== "ready") return;
3636
const controller = new AbortController();
37-
for (const id of membershipIds.split("\n").filter(Boolean)) {
38-
void fetchCommunityIcon(id, controller.signal)
39-
.then((icon) => {
40-
if (!controller.signal.aborted)
41-
setIcons((previous) => {
42-
if (icon === undefined) return previous;
43-
return { ...previous, [id]: icon };
44-
});
45-
})
46-
.catch(() => {
47-
/* Unreachable relay: retain saved icon or initials. */
48-
});
49-
}
37+
// Icon discovery is optional. Reserve browser connections for foreground work
38+
// even when saved relays hold their NIP-11 responses indefinitely.
39+
const ids = membershipIds.split("\n").filter(Boolean);
40+
let next = 0;
41+
const workers = Array.from(
42+
{ length: Math.min(2, ids.length) },
43+
async () => {
44+
while (next < ids.length && !controller.signal.aborted) {
45+
const id = ids[next++];
46+
if (id === undefined) break;
47+
try {
48+
const icon = await fetchCommunityIcon(id, controller.signal);
49+
if (icon !== undefined && !controller.signal.aborted)
50+
setIcons((previous) => ({ ...previous, [id]: icon }));
51+
} catch {
52+
// Unreachable relay: retain saved icon or initials.
53+
}
54+
}
55+
},
56+
);
57+
void Promise.all(workers);
5058
return () => controller.abort();
5159
}, [client.status, membershipIds]);
5260
const select = (id: string | null) => {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { test, expect } from "./fixture.mjs";
2+
3+
// A real same-origin HTTP server holds icon responses. Browser route interception
4+
// would hide per-origin connection starvation and cannot prove server arrival.
5+
test.use({ iconCongestion: true });
6+
7+
test("held saved-community icons leave a connection for foreground traffic", async ({
8+
page,
9+
app,
10+
}) => {
11+
try {
12+
await page.goto(app.origin);
13+
const rail = page.getByRole("navigation", { name: "Communities" });
14+
await expect(
15+
rail.getByRole("button", { name: "Switch to Saved 5" }),
16+
).toBeVisible();
17+
await expect
18+
.poll(() => app.iconCongestion.iconRequests.length)
19+
.toBeGreaterThanOrEqual(2);
20+
21+
// Start the request from the page, not Node: it must compete for the same
22+
// browser's connections while both optional responses are still held.
23+
const foreground = page.evaluate(async () => {
24+
const response = await fetch("/foreground-probe");
25+
return response.json();
26+
});
27+
await expect
28+
.poll(() => app.iconCongestion.foregroundRequests.length)
29+
.toBe(1);
30+
expect(await foreground).toEqual({ reached: true });
31+
expect(app.iconCongestion.iconRequests).toHaveLength(2);
32+
} finally {
33+
app.iconCongestion.release();
34+
}
35+
await expect.poll(() => app.iconCongestion.iconRequests.length).toBe(8);
36+
});

‎tests/browser/fixture.mjs‎

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const test = base.extend({
4040
savedSidebar: [false, { option: true }],
4141
expectedPageFailure: [false, { option: true }],
4242
largeSidebar: [false, { option: true }],
43+
iconCongestion: [false, { option: true }],
4344
dmLabels: [false, { option: true }],
4445
tallMessages: [false, { option: true }],
4546
membershipActivity: [false, { option: true }],
@@ -68,6 +69,7 @@ export const test = base.extend({
6869
savedSidebar,
6970
expectedPageFailure,
7071
largeSidebar,
72+
iconCongestion,
7173
dmLabels,
7274
tallMessages,
7375
membershipActivity,
@@ -961,6 +963,10 @@ export const test = base.extend({
961963
send(response, { error: String(error) }, 500);
962964
}
963965
};
966+
const heldIcons = [];
967+
const iconRequests = [];
968+
const foregroundRequests = [];
969+
let iconsReleased = false;
964970
let server;
965971
try {
966972
server = await preview({
@@ -969,6 +975,21 @@ export const test = base.extend({
969975
{
970976
name: "fixture-relay",
971977
async configurePreviewServer(server) {
978+
if (iconCongestion) {
979+
server.middlewares.use((req, res, next) => {
980+
if (req.url?.includes("/icon-info")) {
981+
iconRequests.push(req.url);
982+
if (iconsReleased) return send(res, {});
983+
heldIcons.push(res);
984+
return;
985+
}
986+
if (req.url === "/foreground-probe") {
987+
foregroundRequests.push(req.url);
988+
return send(res, { reached: true });
989+
}
990+
next();
991+
});
992+
}
972993
if (relay) {
973994
report.brokerRequests = [];
974995
server.middlewares.use((req, res, next) => {
@@ -1042,26 +1063,46 @@ export const test = base.extend({
10421063
}
10431064
});
10441065
await page.addInitScript(
1045-
({ viewer, profilePicture }) => {
1066+
({ viewer, profilePicture, iconCongestion }) => {
10461067
const key = `buzz-client.v1:${viewer}`;
10471068
if (!localStorage.getItem(key))
10481069
localStorage.setItem(
10491070
key,
10501071
JSON.stringify({
10511072
profile: { name: "Browser Fixture", picture: profilePicture },
1052-
memberships: [
1053-
{ id: "primary", name: "Primary" },
1054-
{ id: "secondary", name: "Secondary" },
1055-
],
1073+
memberships: iconCongestion
1074+
? [
1075+
{ id: "primary", name: "Primary" },
1076+
{ id: "secondary", name: "Secondary" },
1077+
...Array.from({ length: 6 }, (_, index) => ({
1078+
id: `https://saved-${index}.example`,
1079+
name: `Saved ${index}`,
1080+
})),
1081+
]
1082+
: [
1083+
{ id: "primary", name: "Primary" },
1084+
{ id: "secondary", name: "Secondary" },
1085+
],
10561086
selected: "primary",
10571087
}),
10581088
);
10591089
},
1060-
{ viewer, profilePicture },
1090+
{ viewer, profilePicture, iconCongestion },
10611091
);
10621092
await use({
10631093
origin,
10641094
report,
1095+
iconCongestion: iconCongestion
1096+
? {
1097+
iconRequests,
1098+
foregroundRequests,
1099+
release() {
1100+
iconsReleased = true;
1101+
for (const response of heldIcons)
1102+
if (!response.writableEnded) send(response, {});
1103+
},
1104+
}
1105+
: undefined,
10651106
pending,
10661107
histories,
10671108
presenceThread,
@@ -1270,6 +1311,9 @@ export const test = base.extend({
12701311
),
12711312
).toEqual([]);
12721313
} finally {
1314+
if (iconCongestion)
1315+
for (const response of heldIcons)
1316+
if (!response.writableEnded) send(response, {});
12731317
await writeFile(
12741318
testInfo.outputPath("evidence.json"),
12751319
JSON.stringify(report, null, 2),

0 commit comments

Comments
 (0)