Skip to content

Commit a157d95

Browse files
committed
Speed up appservice transactions when sending them
This should be done at an earlier level such as where the transactions are calculated, however that doesn't solve the immediate backlog problem on t2bot.io Instead, this alters the HTTP layer to short-circuit the transaction away from the appservice where possible to avoid delaying it.
1 parent f5ed52c commit a157d95

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

synapse/appservice/api.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#
2121
#
2222
import logging
23+
import time
2324
import urllib.parse
2425
from typing import (
2526
TYPE_CHECKING,
@@ -378,6 +379,15 @@ async def push_bulk(
378379
"left": list(device_list_summary.left),
379380
}
380381

382+
has_events = len(serialized_events) > 0
383+
has_edus = service.supports_ephemeral and len(ephemeral) > 0
384+
has_device_list_changes = service.msc3202_transaction_extensions and (len(list(device_list_summary.changed)) > 0 or len(list(device_list_summary.left)) > 0)
385+
has_messages = service.supports_ephemeral and len(to_device_messages) > 0
386+
logger.info("Transaction properties: has_events=%s, has_edus=%s, has_device_list_changes=%s, has_messages=%s", has_events, has_edus, has_device_list_changes, has_messages)
387+
if not has_events and not has_edus and not has_device_list_changes and not has_messages:
388+
logger.info("Returning early on transaction: nothing to send.")
389+
return True
390+
381391
try:
382392
args = None
383393
if self.config.use_appservice_legacy_authorization:
@@ -538,7 +548,19 @@ async def query_keys(
538548
def _serialize(
539549
self, service: "ApplicationService", events: Iterable[EventBase]
540550
) -> List[JsonDict]:
551+
new_events = []
541552
time_now = self.clock.time_msec()
553+
554+
for event in events:
555+
if int(round(time.time() * 1000)) - event.origin_server_ts > (15 * 60 * 1000):
556+
logger.warning("Dropping event (due to age) %s" % event.event_id)
557+
continue
558+
if service.id != "github" and service.is_interested_in_user(event.sender) and event.sender.endswith(":t2bot.io"):
559+
logger.warning("Dropping event (due to echo) %s" % event.event_id)
560+
continue
561+
logger.info("Allowing @ fallback: %s" % event.event_id)
562+
new_events.append(event)
563+
542564
return [
543565
serialize_event(
544566
e,
@@ -557,5 +579,5 @@ def _serialize(
557579
),
558580
),
559581
)
560-
for e in events
582+
for e in new_events
561583
]

0 commit comments

Comments
 (0)