Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

peer_data: Store subscriber counts off of the sub object.

Evy Kassirer06845359bd88
diff --git a/web/src/peer_data.ts b/web/src/peer_data.ts
index 2bd3ba60b33..ff64815086f 100644
--- a/web/src/peer_data.ts
+++ b/web/src/peer_data.ts
@@ -7,10 +7,15 @@ import {LazySet} from "./lazy_set.ts";
import {page_params} from "./page_params.ts";
import type {User} from "./people.ts";
import * as people from "./people.ts";
-import type {StreamSubscription} from "./sub_store.ts";
import * as sub_store from "./sub_store.ts";
import * as util from "./util.ts";
+// Maps stream_id to the number of subscribers in that stream.
+// Note: These counts can sometimes be wrong due to races on the backend,
+// so we should fetch full subscriber data for a stream if we care about
+// its count being accurate.
+const subscriber_counts = new Map<number, number>();
+
// This maps a stream_id to a LazySet of user_ids who are subscribed.
// Make sure that when we have full subscriber data for a stream,
// the size of its subscribers set stays synced with the relevant
@@ -198,36 +203,56 @@ export function potential_subscribers(stream_id: number): User[] {
return people.filter_all_users(is_potential_subscriber);
}
-function increment_subscriber_count(sub: StreamSubscription, user_id: number): void {
- const subscribers = get_loaded_subscriber_subset(sub.stream_id);
+function increment_subscriber_count(
+ subscribers: LazySet,
+ stream_id: number,
+ user_id: number,
+): void {
if (subscribers.has(user_id)) {
return;
}
- sub.subscriber_count += 1;
+ const subscriber_count = subscriber_counts.get(stream_id);
+ if (subscriber_count === undefined) {
+ blueslip.error(
+ `We called increment_subscriber_count for an untracked stream: ${stream_id}`,
+ );
+ } else {
+ subscriber_counts.set(stream_id, subscriber_count + 1);
+ }
}
-function decrement_subscriber_count(sub: StreamSubscription, user_id: number): void {
- const subscribers = get_loaded_subscriber_subset(sub.stream_id);
+function decrement_subscriber_count(
+ subscribers: LazySet,
+ stream_id: number,
+ user_id: number,
+): void {
// If we don't have full subscriber data, we assume that even if didn't know
// they were a subscriber, we still want to decrement the count.
- if (fetched_stream_ids.has(sub.stream_id) && !subscribers.has(user_id)) {
+ if (fetched_stream_ids.has(stream_id) && !subscribers.has(user_id)) {
return;
}
- sub.subscriber_count -= 1;
+ const subscriber_count = subscriber_counts.get(stream_id);
+ if (subscriber_count === undefined) {
+ blueslip.error(
+ `We called decrement_subscriber_count for an untracked stream: ${stream_id}`,
+ );
+ } else {
+ subscriber_counts.set(stream_id, subscriber_count - 1);
+ }
}
// Note: `subscriber_count` can sometimes be wrong due to races on the backend,
// so we should fetch full subscriber data if we care about this number being
// accurate.
export let get_subscriber_count = (stream_id: number, include_bots = true): number => {
- const sub = sub_store.get(stream_id);
- if (sub === undefined) {
+ const count = subscriber_counts.get(stream_id);
+ if (count === undefined) {
blueslip.warn(`We called get_subscriber_count for an untracked stream: ${stream_id}`);
return 0;
}
if (include_bots) {
- return sub.subscriber_count;
+ return count;
}
let bot_count = 0;
@@ -236,13 +261,21 @@ export let get_subscriber_count = (stream_id: number, include_bots = true): numb
bot_count += 1;
}
}
- return sub.subscriber_count - bot_count;
+ return count - bot_count;
};
export function rewire_get_subscriber_count(value: typeof get_subscriber_count): void {
get_subscriber_count = value;
}
+export function set_subscriber_count(stream_id: number, count: number): void {
+ subscriber_counts.set(stream_id, count);
+}
+
+export function clear_subscriber_counts_for_tests(): void {
+ subscriber_counts.clear();
+}
+
export function get_subscribers(stream_id: number): number[] {
// This is our external interface for callers who just
// want an array of user_ids who are subscribed to a stream.
@@ -273,7 +306,7 @@ export function set_subscribers(stream_id: number, user_ids: number[], full_data
if (!sub) {
blueslip.warn(`We called set_subscribers for an untracked stream: ${stream_id}`);
} else if (full_data) {
- sub.subscriber_count = subscribers.size;
+ subscriber_counts.set(stream_id, subscribers.size);
}
if (full_data) {
fetched_stream_ids.add(stream_id);
@@ -288,23 +321,13 @@ export function add_subscriber(stream_id: number, user_id: number): void {
if (person === undefined) {
blueslip.warn(`We tried to add invalid subscriber: ${user_id}`);
}
- const sub = sub_store.get(stream_id);
- // If the sub is undefined, we'll be raising a warning in
- // `get_loaded_subscriber_subset`, so we don't need to here.
- if (sub) {
- increment_subscriber_count(sub, user_id);
- }
+ increment_subscriber_count(subscribers, stream_id, user_id);
subscribers.add(user_id);
}
export function remove_subscriber(stream_id: number, user_id: number): void {
- const sub = sub_store.get(stream_id);
- // If the sub is undefined, we'll be raising a warning in
- // `get_loaded_subscriber_subset`, so we don't need to here.
- if (sub) {
- decrement_subscriber_count(sub, user_id);
- }
const subscribers = get_loaded_subscriber_subset(stream_id);
+ decrement_subscriber_count(subscribers, stream_id, user_id);
subscribers.delete(user_id);
}
@@ -323,7 +346,7 @@ export function bulk_add_subscribers({
// If the sub is undefined, we'll be raising a warning in
// `get_loaded_subscriber_subset`, so we don't need to here.
if (sub) {
- increment_subscriber_count(sub, user_id);
+ increment_subscriber_count(subscribers, sub.stream_id, user_id);
}
subscribers.add(user_id);
}
@@ -352,7 +375,7 @@ export function bulk_remove_subscribers({
// If the sub is undefined, we'll be raising a warning in
// `get_loaded_subscriber_subset`, so we don't need to here.
if (sub) {
- decrement_subscriber_count(sub, user_id);
+ decrement_subscriber_count(subscribers, sub.stream_id, user_id);
}
subscribers.delete(user_id);
}
diff --git a/web/src/stream_data.ts b/web/src/stream_data.ts
index 3fb022f5c6f..977703ac9fd 100644
--- a/web/src/stream_data.ts
+++ b/web/src/stream_data.ts
@@ -126,14 +126,17 @@ const stream_ids_by_old_names = new FoldDict<number>();
const default_stream_ids = new Set<number>();
const realm_web_public_stream_ids = new Set<number>();
-export function clear_subscriptions(): void {
+export function clear_subscriptions(for_tests = true): void {
// This function is only used once at page load, and then
// it should only be used in tests.
stream_info = new BinaryDict((sub) => sub.subscribed);
sub_store.clear();
+ if (for_tests) {
+ peer_data.clear_subscriber_counts_for_tests();
+ }
}
-clear_subscriptions();
+clear_subscriptions(false);
export function rename_sub(sub: StreamSubscription, new_name: string): void {
const old_name = sub.name;
@@ -162,7 +165,7 @@ export function unsubscribe_myself(sub: StreamSubscription): void {
stream_info.set_false(sub.stream_id, sub);
}
-export function add_sub_for_tests(sub: StreamSubscription): void {
+export function add_sub_for_tests(sub: StreamSubscription, subscriber_count = 0): void {
// This function is used only by tests.
// We use create_sub_from_server_data at page load.
// We use create_streams for new streams in live-update events.
@@ -172,6 +175,7 @@ export function add_sub_for_tests(sub: StreamSubscription): void {
realm_web_public_stream_ids.add(sub.stream_id);
}
sub_store.add_hydrated_sub(sub.stream_id, sub);
+ peer_data.set_subscriber_count(sub.stream_id, subscriber_count);
}
export function get_sub(stream_name: string): StreamSubscription | undefined {
@@ -1078,8 +1082,11 @@ export function create_sub_from_server_data(
const full_data = attrs.partial_subscribers === undefined;
const subscriber_user_ids = full_data ? attrs.subscribers : attrs.partial_subscribers;
- delete attrs.subscribers;
- delete attrs.partial_subscribers;
+ // Omit properties not used for the sub object
+ const {subscribers, subscriber_count, partial_subscribers, ...sub_attrs} = attrs;
+
+ assert(attrs.subscriber_count !== undefined);
+ peer_data.set_subscriber_count(attrs.stream_id, attrs.subscriber_count);
sub = {
newly_subscribed: false,
@@ -1093,7 +1100,7 @@ export function create_sub_from_server_data(
color: "color" in attrs ? attrs.color : color_data.pick_color(),
subscribed,
previously_subscribed,
- ...attrs,
+ ...sub_attrs,
};
clean_up_description(sub);
diff --git a/web/src/stream_types.ts b/web/src/stream_types.ts
index b2d291f6348..dda6f0ed71b 100644
--- a/web/src/stream_types.ts
+++ b/web/src/stream_types.ts
@@ -60,10 +60,11 @@ export const stream_schema = z.object({
rendered_description: z.string(),
stream_id: z.number(),
stream_post_policy: z.enum(StreamPostPolicy),
- // Generally, this should not be accessed directly, since it can
- // have small inaccuracies in the event of rare races. See
- // the comments on peer_data.get_subscriber_count.
- subscriber_count: z.number(),
+ // This field is stripped from subscriber objects when loading data
+ // from the server. Always use `peer_data.get_subscriber_count` to
+ // access channel subscriber counts, and see its comments for notes
+ // about the possibility of inaccuracy in the presence of certain races.
+ subscriber_count: z.optional(z.number()),
topics_policy: stream_topics_policy_schema,
});
diff --git a/web/src/sub_store.ts b/web/src/sub_store.ts
index dd54410282e..c5b820921cf 100644
--- a/web/src/sub_store.ts
+++ b/web/src/sub_store.ts
@@ -21,6 +21,7 @@ export type ApiStreamSubscription = z.infer<typeof api_stream_subscription_schem
export const stream_subscription_schema = z.object({
...z.omit(api_stream_subscription_schema, {
subscribers: true,
+ subscriber_count: true,
}).shape,
// These properties are added in `stream_data` when hydrating the streams and are not present in the data we get from the server.
newly_subscribed: z.boolean(),
diff --git a/web/tests/dispatch_subs.test.cjs b/web/tests/dispatch_subs.test.cjs
index 6b4af413aa6..a16f90c6347 100644
--- a/web/tests/dispatch_subs.test.cjs
+++ b/web/tests/dispatch_subs.test.cjs
@@ -75,9 +75,10 @@ test("add", ({override}) => {
test("peer add/remove", ({override}) => {
let event = event_fixtures.subscription__peer_add;
+ const devel_stream_id = event.stream_ids[0];
stream_data.add_sub_for_tests({
name: "devel",
- stream_id: event.stream_ids[0],
+ stream_id: devel_stream_id,
});
const stream_stub = make_stub();
@@ -86,13 +87,13 @@ test("peer add/remove", ({override}) => {
dispatch(event);
assert.equal(stream_stub.num_calls, 1);
- assert.ok(peer_data.is_user_subscribed(event.stream_ids[0], event.user_ids[0]));
+ assert.ok(peer_data.is_user_subscribed(devel_stream_id, event.user_ids[0]));
event = event_fixtures.subscription__peer_remove;
dispatch(event);
assert.equal(stream_stub.num_calls, 2);
- assert.ok(!peer_data.is_user_subscribed(event.stream_ids[0], event.user_ids[0]));
+ assert.ok(!peer_data.is_user_subscribed(devel_stream_id, event.user_ids[0]));
});
test("remove", ({override}) => {
diff --git a/web/tests/lib/events.cjs b/web/tests/lib/events.cjs
index 5785dfb69f2..765b3c79935 100644
--- a/web/tests/lib/events.cjs
+++ b/web/tests/lib/events.cjs
@@ -63,6 +63,7 @@ exports.test_streams = {
can_send_message_group: 2,
can_remove_subscribers_group: 2,
is_recently_active: true,
+ subscriber_count: 10,
},
test: {
is_archived: false,
@@ -88,6 +89,7 @@ exports.test_streams = {
can_send_message_group: 2,
can_remove_subscribers_group: 2,
is_recently_active: true,
+ subscriber_count: 2,
},
};
diff --git a/web/tests/peer_data.test.cjs b/web/tests/peer_data.test.cjs
index 19dd2223017..70c0438f297 100644
--- a/web/tests/peer_data.test.cjs
+++ b/web/tests/peer_data.test.cjs
@@ -204,7 +204,12 @@ test("subscribers", async () => {
"warn",
"We called get_loaded_subscriber_subset for an untracked stream: " + bad_stream_id,
);
+ blueslip.expect(
+ "error",
+ "We called decrement_subscriber_count for an untracked stream: " + bad_stream_id,
+ );
peer_data.remove_subscriber(bad_stream_id, brutus.user_id);
+ blueslip.reset();
// verify that removing an already-removed subscriber is a noop
peer_data.remove_subscriber(stream_id, brutus.user_id);
@@ -255,6 +260,10 @@ test("subscribers", async () => {
"warn",
"We called get_loaded_subscriber_subset for an untracked stream: 9999999",
);
+ blueslip.expect(
+ "error",
+ "We called increment_subscriber_count for an untracked stream: 9999999",
+ );
peer_data.add_subscriber(9999999, brutus.user_id);
blueslip.reset();
@@ -390,6 +399,7 @@ test("get_subscriber_count", async () => {
blueslip.expect("warn", "We called get_subscriber_count for an untracked stream: 102");
assert.equal(peer_data.get_subscriber_count(india.stream_id), 0);
+ blueslip.reset();
stream_data.add_sub_for_tests(india);
assert.equal(peer_data.get_subscriber_count(india.stream_id), 0);
@@ -409,7 +419,7 @@ test("get_subscriber_count", async () => {
// We don't have full data, so we assume this is a decrement even though gail isn't
// in the subscriber list.
- india.subscriber_count = 20;
+ peer_data.set_subscriber_count(india.stream_id, 20);
assert.deepStrictEqual(peer_data.get_subscriber_count(india.stream_id), 20);
peer_data.remove_subscriber(india.stream_id, gail.user_id);
assert.deepStrictEqual(peer_data.get_subscriber_count(india.stream_id), 19);
diff --git a/web/tests/stream_data.test.cjs b/web/tests/stream_data.test.cjs
index 995b3ef75e3..2a0f4c98ae8 100644
--- a/web/tests/stream_data.test.cjs
+++ b/web/tests/stream_data.test.cjs
@@ -1240,11 +1240,13 @@ test("create_sub", () => {
name: "India",
subscribed: true,
is_web_public: true,
+ subscriber_count: 3,
};
const canada = {
name: "Canada",
subscribed: true,
+ subscriber_count: 2,
};
const antarctica = {
@@ -1253,6 +1255,7 @@ test("create_sub", () => {
subscribed: true,
color: "#76ce90",
partial_subscribers: [1, 2, 3],
+ subscriber_count: 10,
};
const india_sub = stream_data.create_sub_from_server_data(india);
@@ -1310,6 +1313,7 @@ test("initialize", ({override}) => {
{
name: "subscriptions",
stream_id: 2001,
+ subscriber_count: 0,
},
];
@@ -1317,6 +1321,7 @@ test("initialize", ({override}) => {
{
name: "unsubscribed",
stream_id: 2002,
+ subscriber_count: 0,
},
];
@@ -1324,6 +1329,7 @@ test("initialize", ({override}) => {
{
name: "never_subscribed",
stream_id: 2003,
+ subscriber_count: 0,
},
];
diff --git a/web/tests/stream_settings_ui.test.cjs b/web/tests/stream_settings_ui.test.cjs
index 91925ba363b..246fdb2a234 100644
--- a/web/tests/stream_settings_ui.test.cjs
+++ b/web/tests/stream_settings_ui.test.cjs
@@ -90,6 +90,7 @@ run_test("redraw_left_panel", ({override, mock_template}) => {
can_subscribe_group: admins_group.id,
date_created: 1691057093,
creator_id: null,
+ subscriber_count: 0,
};
const poland = {
elem: "poland",
@@ -106,6 +107,7 @@ run_test("redraw_left_panel", ({override, mock_template}) => {
can_subscribe_group: admins_group.id,
date_created: 1691057093,
creator_id: null,
+ subscriber_count: 0,
};
const pomona = {
elem: "pomona",
@@ -122,6 +124,7 @@ run_test("redraw_left_panel", ({override, mock_template}) => {
can_subscribe_group: admins_group.id,
date_created: 1691057093,
creator_id: null,
+ subscriber_count: 0,
};
const cpp = {
elem: "cpp",
@@ -138,6 +141,7 @@ run_test("redraw_left_panel", ({override, mock_template}) => {
can_subscribe_group: admins_group.id,
date_created: 1691057093,
creator_id: null,
+ subscriber_count: 0,
};
const zzyzx = {
elem: "zzyzx",
@@ -154,6 +158,7 @@ run_test("redraw_left_panel", ({override, mock_template}) => {
can_subscribe_group: admins_group.id,
date_created: 1691057093,
creator_id: null,
+ subscriber_count: 0,
};
const abcd = {
elem: "abcd",
@@ -170,6 +175,7 @@ run_test("redraw_left_panel", ({override, mock_template}) => {
can_subscribe_group: admins_group.id,
date_created: 1691057093,
creator_id: null,
+ subscriber_count: 0,
};
const utopia = {
elem: "utopia",
@@ -186,6 +192,7 @@ run_test("redraw_left_panel", ({override, mock_template}) => {
can_subscribe_group: admins_group.id,
date_created: 1691057093,
creator_id: null,
+ subscriber_count: 0,
};
const jerry = {
elem: "jerry",
@@ -202,6 +209,7 @@ run_test("redraw_left_panel", ({override, mock_template}) => {
can_subscribe_group: admins_group.id,
date_created: 1691057093,
creator_id: null,
+ subscriber_count: 0,
};
const sub_row_data = [denmark, poland, pomona, cpp, zzyzx, abcd, utopia, jerry];
diff --git a/web/tests/topic_list.test.cjs b/web/tests/topic_list.test.cjs
index ba05cf2d7bd..b6a953d4993 100644
--- a/web/tests/topic_list.test.cjs
+++ b/web/tests/topic_list.test.cjs
@@ -31,6 +31,7 @@ test("is_full_topic_history_available", ({override}) => {
name: "general",
stream_id,
first_message_id: null,
+ subscriber_count: 0,
};
const messages = [
{id: 1, stream_id},
diff --git a/web/tests/typeahead_helper.test.cjs b/web/tests/typeahead_helper.test.cjs
index 886f56eb505..5a6e28d828e 100644
--- a/web/tests/typeahead_helper.test.cjs
+++ b/web/tests/typeahead_helper.test.cjs
@@ -131,12 +131,14 @@ const dev_sub = {
name: "Dev",
color: "blue",
stream_id: 1,
+ subscriber_count: 0,
};
const linux_sub = {
name: "Linux",
color: "red",
stream_id: 2,
+ subscriber_count: 0,
};
stream_data.create_streams([dev_sub, linux_sub]);
stream_data.add_sub_for_tests(dev_sub);