Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Expire old spaces summary pagination sessions. #10574

Merged
merged 4 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10574.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add pagination to the spaces summary based on updates to [MSC2946](https://github.com/matrix-org/matrix-doc/pull/2946).
24 changes: 23 additions & 1 deletion synapse/handlers/space_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,18 @@ class _PaginationKey:
class _PaginationSession:
"""The information that is stored for pagination."""

# The time the pagination session was created, in milliseconds.
creatione_time_ms: int
clokep marked this conversation as resolved.
Show resolved Hide resolved
# The queue of rooms which are still to process.
room_queue: Deque["_RoomQueueEntry"]
# A set of rooms which have been processed.
processed_rooms: Set[str]


class SpaceSummaryHandler:
# The time a pagination session remains valid for.
_PAGINATION_SESSION_VALIDITY_PERIOD_MS = 5 * 60 * 1000

def __init__(self, hs: "HomeServer"):
self._clock = hs.get_clock()
self._auth = hs.get_auth()
Expand All @@ -108,6 +113,21 @@ def __init__(self, hs: "HomeServer"):
"get_room_hierarchy",
)

def _expire_pagination_sessions(self):
"""Expire pagination session which are old."""
expire_before = (
self._clock.time_msec() - self._PAGINATION_SESSION_VALIDITY_PERIOD_MS
)
to_expire = []

for key, value in self._pagination_sessions.items():
if value.creatione_time_ms < expire_before:
to_expire.append(key)

for key in to_expire:
logger.debug("Expiring pagination session id %s", key)
del self._pagination_sessions[key]

async def get_space_summary(
self,
requester: str,
Expand Down Expand Up @@ -312,6 +332,8 @@ async def _get_room_hierarchy(

# If this is continuing a previous session, pull the persisted data.
if from_token:
self._expire_pagination_sessions()

pagination_key = _PaginationKey(
requested_room_id, suggested_only, max_depth, from_token
)
Expand Down Expand Up @@ -391,7 +413,7 @@ async def _get_room_hierarchy(
requested_room_id, suggested_only, max_depth, next_token
)
self._pagination_sessions[pagination_key] = _PaginationSession(
room_queue, processed_rooms
self._clock.time_msec(), room_queue, processed_rooms
)

return result
Expand Down