Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache literal sync filter validation #17186

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/17186.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cache literal sync filter validation for performance.
14 changes: 13 additions & 1 deletion synapse/rest/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from synapse.logging.opentracing import trace_with_opname
from synapse.types import JsonDict, Requester, StreamToken
from synapse.util import json_decoder
from synapse.util.caches.lrucache import LruCache

from ._base import client_patterns, set_timeline_upper_limit

Expand Down Expand Up @@ -110,6 +111,11 @@ def __init__(self, hs: "HomeServer"):
self._msc2654_enabled = hs.config.experimental.msc2654_enabled
self._msc3773_enabled = hs.config.experimental.msc3773_enabled

self._json_filter_cache: LruCache[str, bool] = LruCache(
max_size=1000,
cache_name="sync_valid_filter",
)

async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
# This will always be set by the time Twisted calls us.
assert request.args is not None
Expand Down Expand Up @@ -177,7 +183,13 @@ async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
filter_object = json_decoder.decode(filter_id)
except Exception:
raise SynapseError(400, "Invalid filter JSON", errcode=Codes.NOT_JSON)
self.filtering.check_valid_filter(filter_object)

# We cache the validation, as this can get quite expensive if people use
# a literal json blob as a query param.
if not self._json_filter_cache.get(filter_id):
self.filtering.check_valid_filter(filter_object)
self._json_filter_cache[filter_id] = True

set_timeline_upper_limit(
filter_object, self.hs.config.server.filter_timeline_limit
)
Expand Down
Loading