aboutsummaryrefslogtreecommitdiff
path: root/syncapi/internal
diff options
context:
space:
mode:
authorTill <2353100+S7evinK@users.noreply.github.com>2023-02-07 14:31:23 +0100
committerGitHub <noreply@github.com>2023-02-07 14:31:23 +0100
commiteb29a315507f0075c2c6a495ac59c64a7f45f9fc (patch)
tree3ed37d947dbacdf95c77dc27e4193c671e4968dc /syncapi/internal
parentcf254ba0445e2509f77f41dbec69f632b126b847 (diff)
Optimize `/sync` and history visibility (#2961)
Should fix the following issues or make a lot less worse when using Postgres: The main issue behind #2911: The client gives up after a certain time, causing a cascade of context errors, because the response couldn't be built up fast enough. This mostly happens on accounts with many rooms, due to the inefficient way we're getting recent events and current state For #2777: The queries for getting the membership events for history visibility were being executed for each room (I think 185?), resulting in a whooping 2k queries for membership events. (Getting the statesnapshot -> block nids -> actual wanted membership event) Both should now be better by: - Using a LATERAL join to get all recent events for all joined rooms in one go (TODO: maybe do the same for room summary and current state etc) - If we're lazy loading on initial syncs, we're now not getting the whole current state, just to drop the majority of it because we're lazy loading members - we add a filter to exclude membership events on the first call to `CurrentState`. - Using an optimized query to get the membership events needed to calculate history visibility --------- Co-authored-by: kegsay <kegan@matrix.org>
Diffstat (limited to 'syncapi/internal')
-rw-r--r--syncapi/internal/history_visibility.go27
1 files changed, 13 insertions, 14 deletions
diff --git a/syncapi/internal/history_visibility.go b/syncapi/internal/history_visibility.go
index 71d7ddd1..87c59e03 100644
--- a/syncapi/internal/history_visibility.go
+++ b/syncapi/internal/history_visibility.go
@@ -121,10 +121,7 @@ func ApplyHistoryVisibilityFilter(
// Get the mapping from eventID -> eventVisibility
eventsFiltered := make([]*gomatrixserverlib.HeaderedEvent, 0, len(events))
- visibilities, err := visibilityForEvents(ctx, rsAPI, events, userID, events[0].RoomID())
- if err != nil {
- return eventsFiltered, err
- }
+ visibilities := visibilityForEvents(ctx, rsAPI, events, userID, events[0].RoomID())
for _, ev := range events {
evVis := visibilities[ev.EventID()]
evVis.membershipCurrent = membershipCurrent
@@ -175,7 +172,7 @@ func visibilityForEvents(
rsAPI api.SyncRoomserverAPI,
events []*gomatrixserverlib.HeaderedEvent,
userID, roomID string,
-) (map[string]eventVisibility, error) {
+) map[string]eventVisibility {
eventIDs := make([]string, len(events))
for i := range events {
eventIDs[i] = events[i].EventID()
@@ -185,6 +182,7 @@ func visibilityForEvents(
// get the membership events for all eventIDs
membershipResp := &api.QueryMembershipAtEventResponse{}
+
err := rsAPI.QueryMembershipAtEvent(ctx, &api.QueryMembershipAtEventRequest{
RoomID: roomID,
EventIDs: eventIDs,
@@ -201,19 +199,20 @@ func visibilityForEvents(
membershipAtEvent: gomatrixserverlib.Leave, // default to leave, to not expose events by accident
visibility: event.Visibility,
}
- membershipEvs, ok := membershipResp.Memberships[eventID]
- if !ok {
+ ev, ok := membershipResp.Membership[eventID]
+ if !ok || ev == nil {
result[eventID] = vis
continue
}
- for _, ev := range membershipEvs {
- membership, err := ev.Membership()
- if err != nil {
- return result, err
- }
- vis.membershipAtEvent = membership
+
+ membership, err := ev.Membership()
+ if err != nil {
+ result[eventID] = vis
+ continue
}
+ vis.membershipAtEvent = membership
+
result[eventID] = vis
}
- return result, nil
+ return result
}