Skip to content

Commit

Permalink
Speed up appservice transactions when sending them
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
turt2live committed Dec 30, 2022
1 parent 774e20b commit 3c1cd9a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion synapse/appservice/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import time
import urllib.parse
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Mapping, Optional, Tuple

Expand Down Expand Up @@ -327,6 +328,14 @@ async def push_bulk(
"left": list(device_list_summary.left),
}

has_events = len(serialized_events) == 0
has_edus = service.supports_ephemeral and len(ephemeral) == 0
has_device_list_changes = service.msc3202_transaction_extensions and (len(list(device_list_summary.changed)) == 0 or len(list(device_list_summary.left)) == 0)
has_messages = service.supports_ephemeral and len(to_device_messages) == 0
if not has_events and not has_edus and not has_device_list_changes and not has_messages:
logger.info("Returning early on transaction: no events to send")
return True

try:
await self.put_json(
uri=uri,
Expand Down Expand Up @@ -368,7 +377,19 @@ async def push_bulk(
def _serialize(
self, service: "ApplicationService", events: Iterable[EventBase]
) -> List[JsonDict]:
new_events = []
time_now = self.clock.time_msec()

for event in events:
if int(round(time.time() * 1000)) - event.origin_server_ts > (15 * 60 * 1000):
logger.warning("Dropping event (due to age) %s" % event.event_id)
continue
if service.id != "github" and service.is_interested_in_user(event.sender) and event.sender.endswith(":t2bot.io"):
logger.warning("Dropping event (due to echo) %s" % event.event_id)
continue
logger.info("Allowing @ fallback: %s" % event.event_id)
new_events.append(event)

return [
serialize_event(
e,
Expand All @@ -387,5 +408,5 @@ def _serialize(
),
),
)
for e in events
for e in new_events
]

0 comments on commit 3c1cd9a

Please sign in to comment.