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

Commit 4b10880

Browse files
authored
Make sync response cache time configurable. (#10513)
1 parent dc46f12 commit 4b10880

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

changelog.d/10513.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a configuration setting for the time a `/sync` response is cached for.

docs/sample_config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,15 @@ caches:
711711
#
712712
#expiry_time: 30m
713713

714+
# Controls how long the results of a /sync request are cached for after
715+
# a successful response is returned. A higher duration can help clients with
716+
# intermittent connections, at the cost of higher memory usage.
717+
#
718+
# By default, this is zero, which means that sync responses are not cached
719+
# at all.
720+
#
721+
#sync_response_cache_duration: 2m
722+
714723

715724
## Database ##
716725

synapse/config/cache.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ def generate_config_section(self, **kwargs):
151151
# entries are never evicted based on time.
152152
#
153153
#expiry_time: 30m
154+
155+
# Controls how long the results of a /sync request are cached for after
156+
# a successful response is returned. A higher duration can help clients with
157+
# intermittent connections, at the cost of higher memory usage.
158+
#
159+
# By default, this is zero, which means that sync responses are not cached
160+
# at all.
161+
#
162+
#sync_response_cache_duration: 2m
154163
"""
155164

156165
def read_config(self, config, **kwargs):
@@ -212,6 +221,10 @@ def read_config(self, config, **kwargs):
212221
else:
213222
self.expiry_time_msec = None
214223

224+
self.sync_response_cache_duration = self.parse_duration(
225+
cache_config.get("sync_response_cache_duration", 0)
226+
)
227+
215228
# Resize all caches (if necessary) with the new factors we've loaded
216229
self.resize_all_caches()
217230

synapse/handlers/sync.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,22 @@ def __init__(self, hs: "HomeServer"):
269269
self.presence_handler = hs.get_presence_handler()
270270
self.event_sources = hs.get_event_sources()
271271
self.clock = hs.get_clock()
272-
self.response_cache: ResponseCache[SyncRequestKey] = ResponseCache(
273-
hs.get_clock(), "sync"
274-
)
275272
self.state = hs.get_state_handler()
276273
self.auth = hs.get_auth()
277274
self.storage = hs.get_storage()
278275
self.state_store = self.storage.state
279276

277+
# TODO: flush cache entries on subsequent sync request.
278+
# Once we get the next /sync request (ie, one with the same access token
279+
# that sets 'since' to 'next_batch'), we know that device won't need a
280+
# cached result any more, and we could flush the entry from the cache to save
281+
# memory.
282+
self.response_cache: ResponseCache[SyncRequestKey] = ResponseCache(
283+
hs.get_clock(),
284+
"sync",
285+
timeout_ms=hs.config.caches.sync_response_cache_duration,
286+
)
287+
280288
# ExpiringCache((User, Device)) -> LruCache(user_id => event_id)
281289
self.lazy_loaded_members_cache: ExpiringCache[
282290
Tuple[str, Optional[str]], LruCache[str, str]

0 commit comments

Comments
 (0)