Skip to content

Commit 2edfdb4

Browse files
showelltimabbott
authored andcommitted
refactor: Extract bulk functions to add/remove peers.
We also streamline some of the error handling code by doing everything up front. This will prevent scenarios where a single bad stream_id/user_id causes a bunch of the same warnings in an inner loop.
1 parent e820c43 commit 2edfdb4

File tree

5 files changed

+81
-77
lines changed

5 files changed

+81
-77
lines changed

frontend_tests/node_tests/dispatch_subs.js

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const {strict: assert} = require("assert");
44

5-
const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
5+
const {set_global, zrequire} = require("../zjsunit/namespace");
66
const {make_stub, with_stub} = require("../zjsunit/stub");
77
const {run_test} = require("../zjsunit/test");
88

@@ -123,70 +123,29 @@ test("add error handling", (override) => {
123123
});
124124
});
125125

126-
test("peer event error handling (bad stream_ids)", (override) => {
126+
test("peer event error handling (bad stream_ids/user_ids)", (override) => {
127127
override("compose_fade.update_faded_users", () => {});
128128

129129
const add_event = {
130130
type: "subscription",
131131
op: "peer_add",
132-
stream_ids: [99999],
132+
stream_ids: [8888, 9999],
133+
user_ids: [3333, 4444],
133134
};
134135

135-
blueslip.expect("warn", "Cannot find stream for peer_add: 99999");
136+
blueslip.expect("warn", "We have untracked stream_ids: 8888,9999");
137+
blueslip.expect("warn", "We have untracked user_ids: 3333,4444");
136138
dispatch(add_event);
137139
blueslip.reset();
138140

139141
const remove_event = {
140142
type: "subscription",
141143
op: "peer_remove",
142-
stream_ids: [99999],
144+
stream_ids: [8888, 9999],
145+
user_ids: [3333, 4444],
143146
};
144147

145-
blueslip.expect("warn", "Cannot find stream for peer_remove: 99999");
148+
blueslip.expect("warn", "We have untracked stream_ids: 8888,9999");
149+
blueslip.expect("warn", "We have untracked user_ids: 3333,4444");
146150
dispatch(remove_event);
147151
});
148-
149-
test("peer event error handling (add/remove subscriber)", (override) => {
150-
override("compose_fade.update_faded_users", () => {});
151-
override("subs.update_subscribers_ui", () => {});
152-
153-
stream_data.add_sub({
154-
name: "devel",
155-
stream_id: 1,
156-
});
157-
158-
with_field(
159-
peer_data,
160-
"add_subscriber",
161-
() => false,
162-
() => {
163-
const add_event = {
164-
type: "subscription",
165-
op: "peer_add",
166-
stream_ids: [1],
167-
user_ids: [99999], // id is irrelevant
168-
};
169-
170-
blueslip.expect("warn", "Cannot process peer_add event");
171-
dispatch(add_event);
172-
blueslip.reset();
173-
},
174-
);
175-
176-
with_field(
177-
peer_data,
178-
"remove_subscriber",
179-
() => false,
180-
() => {
181-
const remove_event = {
182-
type: "subscription",
183-
op: "peer_remove",
184-
stream_ids: [1],
185-
user_ids: [99999], // id is irrelevant
186-
};
187-
188-
blueslip.expect("warn", "Cannot process peer_remove event.");
189-
dispatch(remove_event);
190-
},
191-
);
192-
});

static/js/peer_data.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export function get_subscribers(stream_id) {
9494
export function set_subscribers(stream_id, user_ids) {
9595
const subscribers = new LazySet(user_ids || []);
9696
stream_subscribers.set(stream_id, subscribers);
97+
return subscribers;
9798
}
9899

99100
export function add_subscriber(stream_id, user_id) {
@@ -128,6 +129,26 @@ export function remove_subscriber(stream_id, user_id) {
128129
return true;
129130
}
130131

132+
export function bulk_add_subscribers({stream_ids, user_ids}) {
133+
// We rely on our callers to validate stream_ids and user_ids.
134+
for (const stream_id of stream_ids) {
135+
const subscribers = stream_subscribers.get(stream_id) || set_subscribers(stream_id);
136+
for (const user_id of user_ids) {
137+
subscribers.add(user_id);
138+
}
139+
}
140+
}
141+
142+
export function bulk_remove_subscribers({stream_ids, user_ids}) {
143+
// We rely on our callers to validate stream_ids and user_ids.
144+
for (const stream_id of stream_ids) {
145+
const subscribers = stream_subscribers.get(stream_id) || set_subscribers(stream_id);
146+
for (const user_id of user_ids) {
147+
subscribers.delete(user_id);
148+
}
149+
}
150+
}
151+
131152
export function is_user_subscribed(stream_id, user_id) {
132153
// Most callers should call stream_data.is_user_subscribed,
133154
// which does additional checks.

static/js/people.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ export function get_by_user_id(user_id, ignore_missing) {
5656
return people_by_user_id_dict.get(user_id);
5757
}
5858

59+
export function validate_user_ids(user_ids) {
60+
const good_ids = [];
61+
const bad_ids = [];
62+
63+
for (const user_id of user_ids) {
64+
if (people_by_user_id_dict.has(user_id)) {
65+
good_ids.push(user_id);
66+
} else {
67+
bad_ids.push(user_id);
68+
}
69+
}
70+
71+
if (bad_ids.length > 0) {
72+
blueslip.warn(`We have untracked user_ids: ${bad_ids}`);
73+
}
74+
75+
return good_ids;
76+
}
77+
5978
export function get_by_email(email) {
6079
const person = people_dict.get(email);
6180

static/js/server_events_dispatch.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -345,42 +345,28 @@ exports.dispatch_normal_event = function dispatch_normal_event(event) {
345345
}
346346
}
347347
} else if (event.op === "peer_add") {
348-
for (const stream_id of event.stream_ids) {
349-
const sub = stream_data.get_sub_by_id(stream_id);
350-
351-
if (!sub) {
352-
blueslip.warn("Cannot find stream for peer_add: " + stream_id);
353-
continue;
354-
}
348+
const stream_ids = stream_data.validate_stream_ids(event.stream_ids);
349+
const user_ids = people.validate_user_ids(event.user_ids);
355350

356-
for (const user_id of event.user_ids) {
357-
if (!peer_data.add_subscriber(stream_id, user_id)) {
358-
blueslip.warn("Cannot process peer_add event");
359-
continue;
360-
}
361-
}
351+
peer_data.bulk_add_subscribers({stream_ids, user_ids});
362352

353+
for (const stream_id of stream_ids) {
354+
const sub = stream_data.get_sub_by_id(stream_id);
363355
subs.update_subscribers_ui(sub);
364356
}
357+
365358
compose_fade.update_faded_users();
366359
} else if (event.op === "peer_remove") {
367-
for (const stream_id of event.stream_ids) {
368-
const sub = stream_data.get_sub_by_id(stream_id);
369-
370-
if (!sub) {
371-
blueslip.warn("Cannot find stream for peer_remove: " + stream_id);
372-
continue;
373-
}
360+
const stream_ids = stream_data.validate_stream_ids(event.stream_ids);
361+
const user_ids = people.validate_user_ids(event.user_ids);
374362

375-
for (const user_id of event.user_ids) {
376-
if (!peer_data.remove_subscriber(sub.stream_id, user_id)) {
377-
blueslip.warn("Cannot process peer_remove event.");
378-
continue;
379-
}
380-
}
363+
peer_data.bulk_remove_subscribers({stream_ids, user_ids});
381364

365+
for (const stream_id of stream_ids) {
366+
const sub = stream_data.get_sub_by_id(stream_id);
382367
subs.update_subscribers_ui(sub);
383368
}
369+
384370
compose_fade.update_faded_users();
385371
} else if (event.op === "remove") {
386372
for (const rec of event.subscriptions) {

static/js/stream_data.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,25 @@ exports.get_sub_by_id = function (stream_id) {
225225
return subs_by_stream_id.get(stream_id);
226226
};
227227

228+
exports.validate_stream_ids = function (stream_ids) {
229+
const good_ids = [];
230+
const bad_ids = [];
231+
232+
for (const stream_id of stream_ids) {
233+
if (subs_by_stream_id.has(stream_id)) {
234+
good_ids.push(stream_id);
235+
} else {
236+
bad_ids.push(stream_id);
237+
}
238+
}
239+
240+
if (bad_ids.length > 0) {
241+
blueslip.warn(`We have untracked stream_ids: ${bad_ids}`);
242+
}
243+
244+
return good_ids;
245+
};
246+
228247
exports.get_stream_id = function (name) {
229248
// Note: Only use this function for situations where
230249
// you are comfortable with a user dealing with an

0 commit comments

Comments
 (0)