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

Configurable maximum number of events requested by /sync and /messages #2221

Merged
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
6 changes: 6 additions & 0 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def read_config(self, config):
# "disable" federation
self.send_federation = config.get("send_federation", True)

self.filter_timeline_limit = config.get("filter_timeline_limit", -1)

if self.public_baseurl is not None:
if self.public_baseurl[-1] != '/':
self.public_baseurl += '/'
Expand Down Expand Up @@ -161,6 +163,10 @@ def default_config(self, server_name, **kwargs):
# The GC threshold parameters to pass to `gc.set_threshold`, if defined
# gc_thresholds: [700, 10, 10]

# Set the limit on the returned events in the timeline in the get
# and sync operations. The default value is -1, means no upper limit.
# filter_timeline_limit: 5000

# List of ports that Synapse should listen on, their purpose and their
# configuration.
listeners:
Expand Down
11 changes: 11 additions & 0 deletions synapse/rest/client/v2_alpha/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ def client_v2_patterns(path_regex, releases=(0,),
new_prefix = CLIENT_V2_ALPHA_PREFIX.replace("/v2_alpha", "/r%d" % release)
patterns.append(re.compile("^" + new_prefix + path_regex))
return patterns


def set_timeline_upper_limit(filter_json, filter_timeline_limit):
if filter_timeline_limit < 0:
return # no upper limits
if 'room' in filter_json \
and 'timeline' in filter_json['room'] \
and 'limit' in filter_json['room']['timeline']:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: We prefer to never use \ style new line continuations and instead use brackets.

e.g. something like:

if (
    'room' in filter_json
    and 'timeline' in filter_json['room']
    and 'limit' in filter_json['room']['timeline']
):
    ...

(This could also be written as:

timeline = filter_json.get('room', {}).get('timeline', {})
if 'limit' in timeline:
    ...

but I'm not sure if that's actually better in this case)

filter_json['room']['timeline']["limit"] = min(
filter_json['room']['timeline']['limit'],
filter_timeline_limit)
4 changes: 4 additions & 0 deletions synapse/rest/client/v2_alpha/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from synapse.types import UserID

from ._base import client_v2_patterns
from ._base import set_timeline_upper_limit

import logging

Expand Down Expand Up @@ -85,6 +86,9 @@ def on_POST(self, request, user_id):
raise AuthError(403, "Can only create filters for local users")

content = parse_json_object_from_request(request)
set_timeline_upper_limit(content,
self.hs.config.filter_timeline_limit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: For multi line stuff we prefer the following style:

set_timeline_upper_limit(
    content,
    self.hs.config.filter_timeline_limit,
)


filter_id = yield self.filtering.add_user_filter(
user_localpart=target_user.localpart,
user_filter=content,
Expand Down
4 changes: 4 additions & 0 deletions synapse/rest/client/v2_alpha/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from synapse.api.errors import SynapseError
from synapse.api.constants import PresenceState
from ._base import client_v2_patterns
from ._base import set_timeline_upper_limit

import itertools
import logging
Expand Down Expand Up @@ -78,6 +79,7 @@ class SyncRestServlet(RestServlet):

def __init__(self, hs):
super(SyncRestServlet, self).__init__()
self.hs = hs
self.auth = hs.get_auth()
self.sync_handler = hs.get_sync_handler()
self.clock = hs.get_clock()
Expand Down Expand Up @@ -121,6 +123,8 @@ def on_GET(self, request):
if filter_id.startswith('{'):
try:
filter_object = json.loads(filter_id)
set_timeline_upper_limit(filter_object,
self.hs.config.filter_timeline_limit)
except:
raise SynapseError(400, "Invalid filter JSON")
self.filtering.check_valid_filter(filter_object)
Expand Down