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

matrix.org speedups #7544

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 4 additions & 17 deletions synapse/app/generic_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import synapse
import synapse.events
from synapse.api.constants import EventTypes
from synapse.api.errors import HttpResponseException, SynapseError
from synapse.api.errors import SynapseError
from synapse.api.urls import (
CLIENT_API_PREFIX,
FEDERATION_PREFIX,
Expand Down Expand Up @@ -140,31 +140,18 @@

class PresenceStatusStubServlet(RestServlet):
"""If presence is disabled this servlet can be used to stub out setting
presence status, while proxying the getters to the master instance.
presence status.
"""

PATTERNS = client_patterns("/presence/(?P<user_id>[^/]*)/status")

def __init__(self, hs):
super(PresenceStatusStubServlet, self).__init__()
self.http_client = hs.get_simple_http_client()
self.auth = hs.get_auth()
self.main_uri = hs.config.worker_main_http_uri

async def on_GET(self, request, user_id):
# Pass through the auth headers, if any, in case the access token
# is there.
auth_headers = request.requestHeaders.getRawHeaders("Authorization", [])
headers = {"Authorization": auth_headers}

try:
result = await self.http_client.get_json(
self.main_uri + request.uri.decode("ascii"), headers=headers
)
except HttpResponseException as e:
raise e.to_synapse_error()

return 200, result
await self.auth.get_user_by_req(request)
return 200, {"presence": "offline"}

async def on_PUT(self, request, user_id):
await self.auth.get_user_by_req(request)
Expand Down
8 changes: 5 additions & 3 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,10 @@ def __init__(self, hs):
if self._block_events_without_consent_error:
self._consent_uri_builder = ConsentURIBuilder(self.config)

self._is_worker_app = self.config.worker_app is not None

if (
not self.config.worker_app
not self._is_worker_app
and self.config.cleanup_extremities_with_dummy_events
):
self.clock.looping_call(
Expand Down Expand Up @@ -824,7 +826,7 @@ async def handle_new_client_event(
success = False
try:
# If we're a worker we need to hit out to the master.
if self.config.worker_app:
if self._is_worker_app:
await self.send_event_to_master(
event_id=event.event_id,
store=self.store,
Expand Down Expand Up @@ -890,7 +892,7 @@ async def persist_and_notify_client_event(

This should only be run on master.
"""
assert not self.config.worker_app
assert not self._is_worker_app

if ratelimit:
# We check if this is a room admin redacting an event so that we
Expand Down
15 changes: 7 additions & 8 deletions synapse/server_notices/resource_limits_server_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def __init__(self, hs):

self._notifier = hs.get_notifier()

self._enabled = (
hs.config.limit_usage_by_mau
and self._server_notices_manager.is_enabled()
and not hs.config.hs_disabled
)

async def maybe_send_server_notice_to_user(self, user_id):
"""Check if we need to send a notice to this user, this will be true in
two cases.
Expand All @@ -61,14 +67,7 @@ async def maybe_send_server_notice_to_user(self, user_id):
Returns:
Deferred
"""
if self._config.hs_disabled is True:
return

if self._config.limit_usage_by_mau is False:
return

if not self._server_notices_manager.is_enabled():
# Don't try and send server notices unless they've been enabled
if not self._enabled:
return

timestamp = await self._store.user_last_seen_monthly_active(user_id)
Expand Down
19 changes: 11 additions & 8 deletions synapse/storage/data_stores/main/monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ class MonthlyActiveUsersStore(MonthlyActiveUsersWorkerStore):
def __init__(self, database: Database, db_conn, hs):
super(MonthlyActiveUsersStore, self).__init__(database, db_conn, hs)

self._limit_usage_by_mau = hs.config.limit_usage_by_mau
self._mau_stats_only = hs.config.mau_stats_only
self._max_mau_value = hs.config.max_mau_value

# Do not add more reserved users than the total allowable number
# cur = LoggingTransaction(
self.db.new_transaction(
Expand All @@ -130,7 +134,7 @@ def __init__(self, database: Database, db_conn, hs):
[],
[],
self._initialise_reserved_users,
hs.config.mau_limits_reserved_threepids[: self.hs.config.max_mau_value],
hs.config.mau_limits_reserved_threepids[: self._max_mau_value],
)

def _initialise_reserved_users(self, txn, threepids):
Expand Down Expand Up @@ -191,8 +195,7 @@ def _reap_users(txn, reserved_users):

txn.execute(sql, query_args)

max_mau_value = self.hs.config.max_mau_value
if self.hs.config.limit_usage_by_mau:
if self._limit_usage_by_mau:
# If MAU user count still exceeds the MAU threshold, then delete on
# a least recently active basis.
# Note it is not possible to write this query using OFFSET due to
Expand All @@ -210,13 +213,13 @@ def _reap_users(txn, reserved_users):
LIMIT ?
)
"""
txn.execute(sql, (max_mau_value,))
txn.execute(sql, ((self._max_mau_value),))
# Need if/else since 'AND user_id NOT IN ({})' fails on Postgres
# when len(reserved_users) == 0. Works fine on sqlite.
else:
# Must be >= 0 for postgres
num_of_non_reserved_users_to_remove = max(
max_mau_value - len(reserved_users), 0
self._max_mau_value - len(reserved_users), 0
)

# It is important to filter reserved users twice to guard
Expand Down Expand Up @@ -335,7 +338,7 @@ def populate_monthly_active_users(self, user_id):
Args:
user_id(str): the user_id to query
"""
if self.hs.config.limit_usage_by_mau or self.hs.config.mau_stats_only:
if self._limit_usage_by_mau or self._mau_stats_only:
# Trial users and guests should not be included as part of MAU group
is_guest = yield self.is_guest(user_id)
if is_guest:
Expand All @@ -356,11 +359,11 @@ def populate_monthly_active_users(self, user_id):
# In the case where mau_stats_only is True and limit_usage_by_mau is
# False, there is no point in checking get_monthly_active_count - it
# adds no value and will break the logic if max_mau_value is exceeded.
if not self.hs.config.limit_usage_by_mau:
if not self._limit_usage_by_mau:
yield self.upsert_monthly_active_user(user_id)
else:
count = yield self.get_monthly_active_count()
if count < self.hs.config.max_mau_value:
if count < self._max_mau_value:
yield self.upsert_monthly_active_user(user_id)
elif now - last_seen_timestamp > LAST_SEEN_GRANULARITY:
yield self.upsert_monthly_active_user(user_id)