Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 59bb5ce

Browse files
authored
Merge pull request #1798 from matrix-org/luke/limit-group-requests
Limit group requests to 3 at once
2 parents 99b3922 + 35cc443 commit 59bb5ce

File tree

1 file changed

+54
-11
lines changed

1 file changed

+54
-11
lines changed

src/stores/GroupStore.js

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,48 @@ function parseRoomsResponse(response) {
2727
return response.chunk.map((apiRoom) => groupRoomFromApiObject(apiRoom));
2828
}
2929

30+
// The number of ongoing group requests
31+
let ongoingRequestCount = 0;
32+
33+
// This has arbitrarily been set to a small number to lower the priority
34+
// of doing group-related requests because we care about other important
35+
// requests like hitting /sync.
36+
const LIMIT = 3; // Maximum number of ongoing group requests
37+
38+
// FIFO queue of functions to call in the backlog
39+
const backlogQueue = [
40+
// () => {...}
41+
];
42+
43+
// Pull from the FIFO queue
44+
function checkBacklog() {
45+
const item = backlogQueue.shift();
46+
if (typeof item === 'function') item();
47+
}
48+
49+
// Limit the maximum number of ongoing promises returned by fn to LIMIT and
50+
// use a FIFO queue to handle the backlog.
51+
function limitConcurrency(fn) {
52+
return new Promise((resolve, reject) => {
53+
const item = () => {
54+
ongoingRequestCount++;
55+
resolve();
56+
};
57+
if (ongoingRequestCount >= LIMIT) {
58+
// Enqueue this request for later execution
59+
backlogQueue.push(item);
60+
} else {
61+
item();
62+
}
63+
})
64+
.then(fn)
65+
.then((result) => {
66+
ongoingRequestCount--;
67+
checkBacklog();
68+
return result;
69+
});
70+
}
71+
3072
/**
3173
* Stores the group summary for a room and provides an API to change it and
3274
* other useful group APIs that may have an effect on the group summary.
@@ -56,23 +98,24 @@ export default class GroupStore extends EventEmitter {
5698
this._fetchResourcePromise = {};
5799
this._resourceFetcher = {
58100
[GroupStore.STATE_KEY.Summary]: () => {
59-
return MatrixClientPeg.get()
60-
.getGroupSummary(this.groupId);
101+
return limitConcurrency(
102+
() => MatrixClientPeg.get().getGroupSummary(this.groupId),
103+
);
61104
},
62105
[GroupStore.STATE_KEY.GroupRooms]: () => {
63-
return MatrixClientPeg.get()
64-
.getGroupRooms(this.groupId)
65-
.then(parseRoomsResponse);
106+
return limitConcurrency(
107+
() => MatrixClientPeg.get().getGroupRooms(this.groupId).then(parseRoomsResponse),
108+
);
66109
},
67110
[GroupStore.STATE_KEY.GroupMembers]: () => {
68-
return MatrixClientPeg.get()
69-
.getGroupUsers(this.groupId)
70-
.then(parseMembersResponse);
111+
return limitConcurrency(
112+
() => MatrixClientPeg.get().getGroupUsers(this.groupId).then(parseMembersResponse),
113+
);
71114
},
72115
[GroupStore.STATE_KEY.GroupInvitedMembers]: () => {
73-
return MatrixClientPeg.get()
74-
.getGroupInvitedUsers(this.groupId)
75-
.then(parseMembersResponse);
116+
return limitConcurrency(
117+
() => MatrixClientPeg.get().getGroupInvitedUsers(this.groupId).then(parseMembersResponse),
118+
);
76119
},
77120
};
78121

0 commit comments

Comments
 (0)