Bläddra i källkod

Expire old spaces summary pagination sessions. (#10574)

Patrick Cloke 2 år sedan
förälder
incheckning
5acd8b5a96
2 ändrade filer med 24 tillägg och 1 borttagningar
  1. 1 0
      changelog.d/10574.feature
  2. 23 1
      synapse/handlers/space_summary.py

+ 1 - 0
changelog.d/10574.feature

@@ -0,0 +1 @@
+Add pagination to the spaces summary based on updates to [MSC2946](https://github.com/matrix-org/matrix-doc/pull/2946).

+ 23 - 1
synapse/handlers/space_summary.py

@@ -77,6 +77,8 @@ class _PaginationKey:
 class _PaginationSession:
     """The information that is stored for pagination."""
 
+    # The time the pagination session was created, in milliseconds.
+    creation_time_ms: int
     # The queue of rooms which are still to process.
     room_queue: Deque["_RoomQueueEntry"]
     # A set of rooms which have been processed.
@@ -84,6 +86,9 @@ class _PaginationSession:
 
 
 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()
@@ -108,6 +113,21 @@ class SpaceSummaryHandler:
             "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.creation_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,
@@ -312,6 +332,8 @@ class SpaceSummaryHandler:
 
         # 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
             )
@@ -391,7 +413,7 @@ class SpaceSummaryHandler:
                 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