2019-03-13 16:02:56 -04:00
|
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
|
#
|
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
|
#
|
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2019-03-13 16:02:56 -04:00
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
import datetime
|
|
|
|
|
import logging
|
2024-07-01 07:48:36 -04:00
|
|
|
|
from collections import OrderedDict
|
2021-12-02 11:18:10 -05:00
|
|
|
|
from types import TracebackType
|
|
|
|
|
from typing import TYPE_CHECKING, Dict, Hashable, Iterable, List, Optional, Tuple, Type
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
|
import attr
|
2019-03-13 16:02:56 -04:00
|
|
|
|
from prometheus_client import Counter
|
|
|
|
|
|
2022-05-27 07:14:36 -04:00
|
|
|
|
from synapse.api.constants import EduTypes
|
2019-03-13 16:02:56 -04:00
|
|
|
|
from synapse.api.errors import (
|
|
|
|
|
FederationDeniedError,
|
|
|
|
|
HttpResponseException,
|
|
|
|
|
RequestSendFailed,
|
|
|
|
|
)
|
2020-08-17 07:24:46 -04:00
|
|
|
|
from synapse.api.presence import UserPresenceState
|
2019-03-13 16:02:56 -04:00
|
|
|
|
from synapse.events import EventBase
|
|
|
|
|
from synapse.federation.units import Edu
|
|
|
|
|
from synapse.handlers.presence import format_user_presence_state
|
2021-05-11 06:04:03 -04:00
|
|
|
|
from synapse.logging import issue9533_logger
|
2021-04-01 12:08:21 -04:00
|
|
|
|
from synapse.logging.opentracing import SynapseTags, set_tag
|
2019-03-13 16:02:56 -04:00
|
|
|
|
from synapse.metrics import sent_transactions_counter
|
|
|
|
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
2022-11-28 09:40:17 -05:00
|
|
|
|
from synapse.types import JsonDict, ReadReceipt
|
2019-03-13 16:02:56 -04:00
|
|
|
|
from synapse.util.retryutils import NotRetryingDestination, get_retry_limiter
|
2022-06-07 09:35:56 -04:00
|
|
|
|
from synapse.visibility import filter_events_for_server
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
2020-05-05 09:15:57 -04:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
import synapse.server
|
|
|
|
|
|
2019-05-09 06:01:41 -04:00
|
|
|
|
# This is defined in the Matrix spec and enforced by the receiver.
|
|
|
|
|
MAX_EDUS_PER_TRANSACTION = 100
|
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sent_edus_counter = Counter(
|
2019-05-09 11:27:02 -04:00
|
|
|
|
"synapse_federation_client_sent_edus", "Total number of EDUs successfully sent"
|
2019-03-13 16:02:56 -04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sent_edus_by_type = Counter(
|
|
|
|
|
"synapse_federation_client_sent_edus_by_type",
|
|
|
|
|
"Number of sent EDUs successfully sent, by event type",
|
|
|
|
|
["type"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-09-04 12:14:09 -04:00
|
|
|
|
# If the retry interval is larger than this then we enter "catchup" mode
|
|
|
|
|
CATCHUP_RETRY_INTERVAL = 60 * 60 * 1000
|
|
|
|
|
|
2024-07-01 07:48:36 -04:00
|
|
|
|
# Limit how many presence states we add to each presence EDU, to ensure that
|
|
|
|
|
# they are bounded in size.
|
|
|
|
|
MAX_PRESENCE_STATES_PER_EDU = 50
|
|
|
|
|
|
2023-09-04 12:14:09 -04:00
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
|
class PerDestinationQueue:
|
2019-03-13 16:02:56 -04:00
|
|
|
|
"""
|
|
|
|
|
Manages the per-destination transmission queues.
|
|
|
|
|
|
|
|
|
|
Args:
|
2020-02-07 08:56:38 -05:00
|
|
|
|
hs
|
|
|
|
|
transaction_sender
|
|
|
|
|
destination: the server_name of the destination that we are managing
|
2019-03-13 16:02:56 -04:00
|
|
|
|
transmission for.
|
|
|
|
|
"""
|
2019-05-09 11:27:02 -04:00
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
hs: "synapse.server.HomeServer",
|
|
|
|
|
transaction_manager: "synapse.federation.sender.TransactionManager",
|
|
|
|
|
destination: str,
|
|
|
|
|
):
|
2019-03-13 16:02:56 -04:00
|
|
|
|
self._server_name = hs.hostname
|
|
|
|
|
self._clock = hs.get_clock()
|
2022-06-07 09:35:56 -04:00
|
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
2022-02-23 06:04:02 -05:00
|
|
|
|
self._store = hs.get_datastores().main
|
2019-03-13 16:02:56 -04:00
|
|
|
|
self._transaction_manager = transaction_manager
|
2020-07-10 13:26:36 -04:00
|
|
|
|
self._instance_name = hs.get_instance_name()
|
2020-07-29 18:22:13 -04:00
|
|
|
|
self._federation_shard_config = hs.config.worker.federation_shard_config
|
2021-03-18 11:52:26 -04:00
|
|
|
|
self._state = hs.get_state_handler()
|
2020-07-10 13:26:36 -04:00
|
|
|
|
|
|
|
|
|
self._should_send_on_this_instance = True
|
2020-07-16 09:06:28 -04:00
|
|
|
|
if not self._federation_shard_config.should_handle(
|
2020-07-10 13:26:36 -04:00
|
|
|
|
self._instance_name, destination
|
|
|
|
|
):
|
|
|
|
|
# We don't raise an exception here to avoid taking out any other
|
|
|
|
|
# processing. We have a guard in `attempt_new_transaction` that
|
|
|
|
|
# ensure we don't start sending stuff.
|
|
|
|
|
logger.error(
|
|
|
|
|
"Create a per destination queue for %s on wrong worker",
|
|
|
|
|
destination,
|
|
|
|
|
)
|
|
|
|
|
self._should_send_on_this_instance = False
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
|
|
|
|
self._destination = destination
|
|
|
|
|
self.transmission_loop_running = False
|
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
|
# Flag to signal to any running transmission loop that there is new data
|
|
|
|
|
# queued up to be sent.
|
|
|
|
|
self._new_data_to_send = False
|
|
|
|
|
|
2020-09-15 04:07:19 -04:00
|
|
|
|
# True whilst we are sending events that the remote homeserver missed
|
|
|
|
|
# because it was unreachable. We start in this state so we can perform
|
|
|
|
|
# catch-up at startup.
|
|
|
|
|
# New events will only be sent once this is finished, at which point
|
|
|
|
|
# _catching_up is flipped to False.
|
2021-07-15 06:02:43 -04:00
|
|
|
|
self._catching_up: bool = True
|
2020-09-15 04:07:19 -04:00
|
|
|
|
|
|
|
|
|
# The stream_ordering of the most recent PDU that was discarded due to
|
|
|
|
|
# being in catch-up mode.
|
2021-07-15 06:02:43 -04:00
|
|
|
|
self._catchup_last_skipped: int = 0
|
2020-09-15 04:07:19 -04:00
|
|
|
|
|
|
|
|
|
# Cache of the last successfully-transmitted stream ordering for this
|
|
|
|
|
# destination (we are the only updater so this is safe)
|
2021-07-15 06:02:43 -04:00
|
|
|
|
self._last_successful_stream_ordering: Optional[int] = None
|
2020-09-15 04:07:19 -04:00
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
|
# a queue of pending PDUs
|
2021-07-15 06:02:43 -04:00
|
|
|
|
self._pending_pdus: List[EventBase] = []
|
2020-05-21 07:56:27 -04:00
|
|
|
|
|
|
|
|
|
# XXX this is never actually used: see
|
|
|
|
|
# https://github.com/matrix-org/synapse/issues/7549
|
2021-07-15 06:02:43 -04:00
|
|
|
|
self._pending_edus: List[Edu] = []
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
|
|
|
|
# Pending EDUs by their "key". Keyed EDUs are EDUs that get clobbered
|
|
|
|
|
# based on their key (e.g. typing events by room_id)
|
|
|
|
|
# Map of (edu_type, key) -> Edu
|
2021-07-15 06:02:43 -04:00
|
|
|
|
self._pending_edus_keyed: Dict[Tuple[str, Hashable], Edu] = {}
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
|
|
|
|
# Map of user_id -> UserPresenceState of pending presence to be sent to this
|
|
|
|
|
# destination
|
2024-07-01 07:48:36 -04:00
|
|
|
|
self._pending_presence: OrderedDict[str, UserPresenceState] = OrderedDict()
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
2022-11-28 09:40:17 -05:00
|
|
|
|
# List of room_id -> receipt_type -> user_id -> receipt_dict,
|
|
|
|
|
#
|
|
|
|
|
# Each receipt can only have a single receipt per
|
|
|
|
|
# (room ID, receipt type, user ID, thread ID) tuple.
|
|
|
|
|
self._pending_receipt_edus: List[Dict[str, Dict[str, Dict[str, dict]]]] = []
|
2019-03-20 12:02:25 -04:00
|
|
|
|
self._rrs_pending_flush = False
|
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
|
# stream_id of last successfully sent to-device message.
|
|
|
|
|
# NB: may be a long or an int.
|
|
|
|
|
self._last_device_stream_id = 0
|
|
|
|
|
|
|
|
|
|
# stream_id of last successfully sent device list update.
|
|
|
|
|
self._last_device_list_stream_id = 0
|
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
def __str__(self) -> str:
|
2019-03-20 12:02:25 -04:00
|
|
|
|
return "PerDestinationQueue[%s]" % self._destination
|
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
def pending_pdu_count(self) -> int:
|
2019-03-13 16:02:56 -04:00
|
|
|
|
return len(self._pending_pdus)
|
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
def pending_edu_count(self) -> int:
|
2019-03-13 16:02:56 -04:00
|
|
|
|
return (
|
|
|
|
|
len(self._pending_edus)
|
|
|
|
|
+ len(self._pending_presence)
|
|
|
|
|
+ len(self._pending_edus_keyed)
|
|
|
|
|
)
|
|
|
|
|
|
2021-04-28 06:04:38 -04:00
|
|
|
|
def send_pdu(self, pdu: EventBase) -> None:
|
|
|
|
|
"""Add a PDU to the queue, and start the transmission loop if necessary
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
|
|
|
|
Args:
|
2021-04-28 06:04:38 -04:00
|
|
|
|
pdu: pdu to send
|
2019-03-13 16:02:56 -04:00
|
|
|
|
"""
|
2020-09-15 04:07:19 -04:00
|
|
|
|
if not self._catching_up or self._last_successful_stream_ordering is None:
|
|
|
|
|
# only enqueue the PDU if we are not catching up (False) or do not
|
|
|
|
|
# yet know if we have anything to catch up (None)
|
2021-04-28 06:04:38 -04:00
|
|
|
|
self._pending_pdus.append(pdu)
|
2020-09-15 04:07:19 -04:00
|
|
|
|
else:
|
2021-04-28 06:04:38 -04:00
|
|
|
|
assert pdu.internal_metadata.stream_ordering
|
|
|
|
|
self._catchup_last_skipped = pdu.internal_metadata.stream_ordering
|
2020-09-15 04:07:19 -04:00
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
|
self.attempt_new_transaction()
|
|
|
|
|
|
2021-07-15 06:52:56 -04:00
|
|
|
|
def send_presence(
|
|
|
|
|
self, states: Iterable[UserPresenceState], start_loop: bool = True
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Add presence updates to the queue.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
states: Presence updates to send
|
|
|
|
|
start_loop: Whether to start the transmission loop if not already
|
|
|
|
|
running.
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
|
|
|
|
Args:
|
2020-02-07 08:56:38 -05:00
|
|
|
|
states: presence to send
|
2019-03-13 16:02:56 -04:00
|
|
|
|
"""
|
2019-05-09 11:27:02 -04:00
|
|
|
|
self._pending_presence.update({state.user_id: state for state in states})
|
2021-07-15 06:52:56 -04:00
|
|
|
|
self._new_data_to_send = True
|
|
|
|
|
|
|
|
|
|
if start_loop:
|
|
|
|
|
self.attempt_new_transaction()
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
def queue_read_receipt(self, receipt: ReadReceipt) -> None:
|
2019-03-20 12:02:25 -04:00
|
|
|
|
"""Add a RR to the list to be sent. Doesn't start the transmission loop yet
|
|
|
|
|
(see flush_read_receipts_for_room)
|
|
|
|
|
|
|
|
|
|
Args:
|
2020-02-07 08:56:38 -05:00
|
|
|
|
receipt: receipt to be queued
|
2019-03-20 12:02:25 -04:00
|
|
|
|
"""
|
2022-11-28 09:40:17 -05:00
|
|
|
|
serialized_receipt: JsonDict = {
|
|
|
|
|
"event_ids": receipt.event_ids,
|
|
|
|
|
"data": receipt.data,
|
|
|
|
|
}
|
|
|
|
|
if receipt.thread_id is not None:
|
|
|
|
|
serialized_receipt["data"]["thread_id"] = receipt.thread_id
|
|
|
|
|
|
|
|
|
|
# Find which EDU to add this receipt to. There's three situations depending
|
|
|
|
|
# on the (room ID, receipt type, user, thread ID) tuple:
|
|
|
|
|
#
|
|
|
|
|
# 1. If it fully matches, clobber the information.
|
|
|
|
|
# 2. If it is missing, add the information.
|
|
|
|
|
# 3. If the subset tuple of (room ID, receipt type, user) matches, check
|
|
|
|
|
# the next EDU (or add a new EDU).
|
|
|
|
|
for edu in self._pending_receipt_edus:
|
|
|
|
|
receipt_content = edu.setdefault(receipt.room_id, {}).setdefault(
|
|
|
|
|
receipt.receipt_type, {}
|
|
|
|
|
)
|
|
|
|
|
# If this room ID, receipt type, user ID is not in this EDU, OR if
|
|
|
|
|
# the full tuple matches, use the current EDU.
|
|
|
|
|
if (
|
|
|
|
|
receipt.user_id not in receipt_content
|
|
|
|
|
or receipt_content[receipt.user_id].get("thread_id")
|
|
|
|
|
== receipt.thread_id
|
|
|
|
|
):
|
|
|
|
|
receipt_content[receipt.user_id] = serialized_receipt
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# If no matching EDU was found, create a new one.
|
|
|
|
|
else:
|
|
|
|
|
self._pending_receipt_edus.append(
|
|
|
|
|
{
|
|
|
|
|
receipt.room_id: {
|
|
|
|
|
receipt.receipt_type: {receipt.user_id: serialized_receipt}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
2019-03-20 12:02:25 -04:00
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
def flush_read_receipts_for_room(self, room_id: str) -> None:
|
2022-11-28 09:40:17 -05:00
|
|
|
|
# If there are any pending receipts for this room then force-flush them
|
|
|
|
|
# in a new transaction.
|
|
|
|
|
for edu in self._pending_receipt_edus:
|
|
|
|
|
if room_id in edu:
|
|
|
|
|
self._rrs_pending_flush = True
|
|
|
|
|
self.attempt_new_transaction()
|
|
|
|
|
# No use in checking remaining EDUs if the room was found.
|
|
|
|
|
break
|
2019-03-20 12:02:25 -04:00
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
def send_keyed_edu(self, edu: Edu, key: Hashable) -> None:
|
2019-03-13 16:02:56 -04:00
|
|
|
|
self._pending_edus_keyed[(edu.edu_type, key)] = edu
|
|
|
|
|
self.attempt_new_transaction()
|
|
|
|
|
|
2021-12-02 11:18:10 -05:00
|
|
|
|
def send_edu(self, edu: Edu) -> None:
|
2019-03-13 16:02:56 -04:00
|
|
|
|
self._pending_edus.append(edu)
|
|
|
|
|
self.attempt_new_transaction()
|
|
|
|
|
|
2022-03-04 06:48:15 -05:00
|
|
|
|
def mark_new_data(self) -> None:
|
|
|
|
|
"""Marks that the destination has new data to send, without starting a
|
|
|
|
|
new transaction.
|
|
|
|
|
|
2022-05-26 07:09:16 -04:00
|
|
|
|
If a transaction loop is already in progress then a new transaction will
|
2022-03-04 06:48:15 -05:00
|
|
|
|
be attempted when the current one finishes.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self._new_data_to_send = True
|
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
def attempt_new_transaction(self) -> None:
|
2019-03-13 16:02:56 -04:00
|
|
|
|
"""Try to start a new transaction to this destination
|
|
|
|
|
|
|
|
|
|
If there is already a transaction in progress to this destination,
|
|
|
|
|
returns immediately. Otherwise kicks off the process of sending a
|
|
|
|
|
transaction in the background.
|
|
|
|
|
"""
|
2020-09-03 14:23:07 -04:00
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
|
# Mark that we (may) have new things to send, so that any running
|
|
|
|
|
# transmission loop will recheck whether there is stuff to send.
|
|
|
|
|
self._new_data_to_send = True
|
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
|
if self.transmission_loop_running:
|
|
|
|
|
# XXX: this can get stuck on by a never-ending
|
|
|
|
|
# request at which point pending_pdus just keeps growing.
|
|
|
|
|
# we need application-layer timeouts of some flavour of these
|
|
|
|
|
# requests
|
2019-05-09 11:27:02 -04:00
|
|
|
|
logger.debug("TX [%s] Transaction already in progress", self._destination)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
return
|
|
|
|
|
|
2020-07-10 13:26:36 -04:00
|
|
|
|
if not self._should_send_on_this_instance:
|
|
|
|
|
# We don't raise an exception here to avoid taking out any other
|
|
|
|
|
# processing.
|
|
|
|
|
logger.error(
|
|
|
|
|
"Trying to start a transaction to %s on wrong worker", self._destination
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
|
logger.debug("TX [%s] Starting transaction loop", self._destination)
|
|
|
|
|
|
|
|
|
|
run_as_background_process(
|
|
|
|
|
"federation_transaction_transmission_loop",
|
|
|
|
|
self._transaction_transmission_loop,
|
|
|
|
|
)
|
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
async def _transaction_transmission_loop(self) -> None:
|
2024-07-09 06:04:40 -04:00
|
|
|
|
pending_pdus: List[EventBase] = []
|
2019-03-13 16:02:56 -04:00
|
|
|
|
try:
|
|
|
|
|
self.transmission_loop_running = True
|
|
|
|
|
|
|
|
|
|
# This will throw if we wouldn't retry. We do this here so we fail
|
|
|
|
|
# quickly, but we will later check this again in the http client,
|
|
|
|
|
# hence why we throw the result away.
|
2020-02-07 08:56:38 -05:00
|
|
|
|
await get_retry_limiter(self._destination, self._clock, self._store)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
2020-09-15 04:07:19 -04:00
|
|
|
|
if self._catching_up:
|
|
|
|
|
# we potentially need to catch-up first
|
|
|
|
|
await self._catch_up_transmission_loop()
|
|
|
|
|
if self._catching_up:
|
|
|
|
|
# not caught up yet
|
|
|
|
|
return
|
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
|
while True:
|
2021-03-15 10:42:40 -04:00
|
|
|
|
self._new_data_to_send = False
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
|
async with _TransactionQueueManager(self) as (
|
2024-07-09 06:04:40 -04:00
|
|
|
|
pending_pdus, # noqa: F811
|
2021-03-15 10:42:40 -04:00
|
|
|
|
pending_edus,
|
|
|
|
|
):
|
|
|
|
|
if not pending_pdus and not pending_edus:
|
|
|
|
|
logger.debug("TX [%s] Nothing to send", self._destination)
|
|
|
|
|
|
|
|
|
|
# If we've gotten told about new things to send during
|
|
|
|
|
# checking for things to send, we try looking again.
|
|
|
|
|
# Otherwise new PDUs or EDUs might arrive in the meantime,
|
|
|
|
|
# but not get sent because we hold the
|
|
|
|
|
# `transmission_loop_running` flag.
|
|
|
|
|
if self._new_data_to_send:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
return
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
|
if pending_pdus:
|
|
|
|
|
logger.debug(
|
|
|
|
|
"TX [%s] len(pending_pdus_by_dest[dest]) = %d",
|
|
|
|
|
self._destination,
|
|
|
|
|
len(pending_pdus),
|
2019-03-13 16:02:56 -04:00
|
|
|
|
)
|
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
|
await self._transaction_manager.send_new_transaction(
|
|
|
|
|
self._destination, pending_pdus, pending_edus
|
2019-05-09 11:27:02 -04:00
|
|
|
|
)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
|
|
|
|
sent_transactions_counter.inc()
|
|
|
|
|
sent_edus_counter.inc(len(pending_edus))
|
|
|
|
|
for edu in pending_edus:
|
|
|
|
|
sent_edus_by_type.labels(edu.edu_type).inc()
|
2019-06-13 08:52:08 -04:00
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
|
except NotRetryingDestination as e:
|
|
|
|
|
logger.debug(
|
|
|
|
|
"TX [%s] not ready for retry yet (next retry at %s) - "
|
|
|
|
|
"dropping transaction for now",
|
|
|
|
|
self._destination,
|
|
|
|
|
datetime.datetime.fromtimestamp(
|
|
|
|
|
(e.retry_last_ts + e.retry_interval) / 1000.0
|
|
|
|
|
),
|
|
|
|
|
)
|
2020-08-13 07:35:04 -04:00
|
|
|
|
|
2023-09-04 12:14:09 -04:00
|
|
|
|
if e.retry_interval > CATCHUP_RETRY_INTERVAL:
|
2020-08-13 07:35:04 -04:00
|
|
|
|
# we won't retry for another hour!
|
|
|
|
|
# (this suggests a significant outage)
|
2020-09-15 04:07:19 -04:00
|
|
|
|
# We drop pending EDUs because otherwise they will
|
2020-08-13 07:35:04 -04:00
|
|
|
|
# rack up indefinitely.
|
2020-09-15 04:07:19 -04:00
|
|
|
|
# (Dropping PDUs is already performed by `_start_catching_up`.)
|
2020-08-13 07:35:04 -04:00
|
|
|
|
# Note that:
|
|
|
|
|
# - the EDUs that are being dropped here are those that we can
|
|
|
|
|
# afford to drop (specifically, only typing notifications,
|
|
|
|
|
# read receipts and presence updates are being dropped here)
|
|
|
|
|
# - Other EDUs such as to_device messages are queued with a
|
|
|
|
|
# different mechanism
|
|
|
|
|
# - this is all volatile state that would be lost if the
|
|
|
|
|
# federation sender restarted anyway
|
|
|
|
|
|
|
|
|
|
# dropping read receipts is a bit sad but should be solved
|
|
|
|
|
# through another mechanism, because this is all volatile!
|
|
|
|
|
self._pending_edus = []
|
|
|
|
|
self._pending_edus_keyed = {}
|
2024-07-01 07:48:36 -04:00
|
|
|
|
self._pending_presence.clear()
|
2022-11-28 09:40:17 -05:00
|
|
|
|
self._pending_receipt_edus = []
|
2020-09-15 04:07:19 -04:00
|
|
|
|
|
2021-03-15 10:42:40 -04:00
|
|
|
|
self._start_catching_up()
|
2019-03-13 16:02:56 -04:00
|
|
|
|
except FederationDeniedError as e:
|
|
|
|
|
logger.info(e)
|
|
|
|
|
except HttpResponseException as e:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"TX [%s] Received %d response to transaction: %s",
|
2019-05-09 11:27:02 -04:00
|
|
|
|
self._destination,
|
|
|
|
|
e.code,
|
|
|
|
|
e,
|
2019-03-13 16:02:56 -04:00
|
|
|
|
)
|
2020-09-15 04:07:19 -04:00
|
|
|
|
|
2019-03-13 16:02:56 -04:00
|
|
|
|
except RequestSendFailed as e:
|
2019-05-09 11:27:02 -04:00
|
|
|
|
logger.warning(
|
|
|
|
|
"TX [%s] Failed to send transaction: %s", self._destination, e
|
|
|
|
|
)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
|
2020-09-03 14:23:07 -04:00
|
|
|
|
for p in pending_pdus:
|
2019-05-09 11:27:02 -04:00
|
|
|
|
logger.info(
|
|
|
|
|
"Failed to send event %s to %s", p.event_id, self._destination
|
|
|
|
|
)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
except Exception:
|
2019-05-09 11:27:02 -04:00
|
|
|
|
logger.exception("TX [%s] Failed to send transaction", self._destination)
|
2020-09-03 14:23:07 -04:00
|
|
|
|
for p in pending_pdus:
|
2019-05-09 11:27:02 -04:00
|
|
|
|
logger.info(
|
|
|
|
|
"Failed to send event %s to %s", p.event_id, self._destination
|
|
|
|
|
)
|
2019-03-13 16:02:56 -04:00
|
|
|
|
finally:
|
|
|
|
|
# We want to be *very* sure we clear this after we stop processing
|
|
|
|
|
self.transmission_loop_running = False
|
|
|
|
|
|
2020-09-15 04:07:19 -04:00
|
|
|
|
async def _catch_up_transmission_loop(self) -> None:
|
|
|
|
|
first_catch_up_check = self._last_successful_stream_ordering is None
|
|
|
|
|
|
|
|
|
|
if first_catch_up_check:
|
|
|
|
|
# first catchup so get last_successful_stream_ordering from database
|
|
|
|
|
self._last_successful_stream_ordering = (
|
|
|
|
|
await self._store.get_destination_last_successful_stream_ordering(
|
|
|
|
|
self._destination
|
2021-02-16 17:32:34 -05:00
|
|
|
|
)
|
2020-09-15 04:07:19 -04:00
|
|
|
|
)
|
|
|
|
|
|
2022-02-23 06:04:02 -05:00
|
|
|
|
_tmp_last_successful_stream_ordering = self._last_successful_stream_ordering
|
|
|
|
|
if _tmp_last_successful_stream_ordering is None:
|
2020-09-15 04:07:19 -04:00
|
|
|
|
# if it's still None, then this means we don't have the information
|
|
|
|
|
# in our database we haven't successfully sent a PDU to this server
|
|
|
|
|
# (at least since the introduction of the feature tracking
|
|
|
|
|
# last_successful_stream_ordering).
|
|
|
|
|
# Sadly, this means we can't do anything here as we don't know what
|
|
|
|
|
# needs catching up — so catching up is futile; let's stop.
|
|
|
|
|
self._catching_up = False
|
|
|
|
|
return
|
|
|
|
|
|
2022-02-23 06:04:02 -05:00
|
|
|
|
last_successful_stream_ordering: int = _tmp_last_successful_stream_ordering
|
|
|
|
|
|
2020-09-15 04:07:19 -04:00
|
|
|
|
# get at most 50 catchup room/PDUs
|
|
|
|
|
while True:
|
|
|
|
|
event_ids = await self._store.get_catch_up_room_event_ids(
|
2022-02-21 13:37:04 -05:00
|
|
|
|
self._destination, last_successful_stream_ordering
|
2020-09-15 04:07:19 -04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not event_ids:
|
|
|
|
|
# No more events to catch up on, but we can't ignore the chance
|
|
|
|
|
# of a race condition, so we check that no new events have been
|
|
|
|
|
# skipped due to us being in catch-up mode
|
|
|
|
|
|
2022-02-21 13:37:04 -05:00
|
|
|
|
if self._catchup_last_skipped > last_successful_stream_ordering:
|
2020-09-15 04:07:19 -04:00
|
|
|
|
# another event has been skipped because we were in catch-up mode
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# we are done catching up!
|
|
|
|
|
self._catching_up = False
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if first_catch_up_check:
|
|
|
|
|
# as this is our check for needing catch-up, we may have PDUs in
|
|
|
|
|
# the queue from before we *knew* we had to do catch-up, so
|
|
|
|
|
# clear those out now.
|
|
|
|
|
self._start_catching_up()
|
|
|
|
|
|
|
|
|
|
# fetch the relevant events from the event store
|
|
|
|
|
# - redacted behaviour of REDACT is fine, since we only send metadata
|
|
|
|
|
# of redacted events to the destination.
|
|
|
|
|
# - don't need to worry about rejected events as we do not actively
|
|
|
|
|
# forward received events over federation.
|
|
|
|
|
catchup_pdus = await self._store.get_events_as_list(event_ids)
|
|
|
|
|
if not catchup_pdus:
|
|
|
|
|
raise AssertionError(
|
|
|
|
|
"No events retrieved when we asked for %r. "
|
|
|
|
|
"This should not happen." % event_ids
|
|
|
|
|
)
|
|
|
|
|
|
2022-06-07 09:35:56 -04:00
|
|
|
|
logger.info(
|
|
|
|
|
"Catching up destination %s with %d PDUs",
|
|
|
|
|
self._destination,
|
|
|
|
|
len(catchup_pdus),
|
|
|
|
|
)
|
|
|
|
|
|
2021-03-18 11:52:26 -04:00
|
|
|
|
# We send transactions with events from one room only, as its likely
|
|
|
|
|
# that the remote will have to do additional processing, which may
|
|
|
|
|
# take some time. It's better to give it small amounts of work
|
|
|
|
|
# rather than risk the request timing out and repeatedly being
|
|
|
|
|
# retried, and not making any progress.
|
|
|
|
|
#
|
|
|
|
|
# Note: `catchup_pdus` will have exactly one PDU per room.
|
|
|
|
|
for pdu in catchup_pdus:
|
2023-03-13 08:31:19 -04:00
|
|
|
|
# The PDU from the DB will be the newest PDU in the room from
|
|
|
|
|
# *this server* that we tried---but were unable---to send to the remote.
|
2021-03-18 11:52:26 -04:00
|
|
|
|
# servers may have sent lots of events since then, and we want
|
|
|
|
|
# to try and tell the remote only about the *latest* events in
|
|
|
|
|
# the room. This is so that it doesn't get inundated by events
|
|
|
|
|
# from various parts of the DAG, which all need to be processed.
|
|
|
|
|
#
|
|
|
|
|
# Note: this does mean that in large rooms a server coming back
|
|
|
|
|
# online will get sent the same events from all the different
|
|
|
|
|
# servers, but the remote will correctly deduplicate them and
|
|
|
|
|
# handle it only once.
|
|
|
|
|
|
|
|
|
|
# Step 1, fetch the current extremities
|
|
|
|
|
extrems = await self._store.get_prev_events_for_room(pdu.room_id)
|
|
|
|
|
|
|
|
|
|
if pdu.event_id in extrems:
|
|
|
|
|
# If the event is in the extremities, then great! We can just
|
|
|
|
|
# use that without having to do further checks.
|
|
|
|
|
room_catchup_pdus = [pdu]
|
2023-03-13 08:31:19 -04:00
|
|
|
|
elif await self._store.is_partial_state_room(pdu.room_id):
|
|
|
|
|
# We can't be sure which events the destination should
|
|
|
|
|
# see using only partial state. Avoid doing so, and just retry
|
|
|
|
|
# sending our the newest PDU the remote is missing from us.
|
|
|
|
|
room_catchup_pdus = [pdu]
|
2021-03-18 11:52:26 -04:00
|
|
|
|
else:
|
|
|
|
|
# If not, fetch the extremities and figure out which we can
|
|
|
|
|
# send.
|
|
|
|
|
extrem_events = await self._store.get_events_as_list(extrems)
|
|
|
|
|
|
|
|
|
|
new_pdus = []
|
|
|
|
|
for p in extrem_events:
|
|
|
|
|
# We pulled this from the DB, so it'll be non-null
|
|
|
|
|
assert p.internal_metadata.stream_ordering
|
|
|
|
|
|
|
|
|
|
# Filter out events that happened before the remote went
|
|
|
|
|
# offline
|
|
|
|
|
if (
|
|
|
|
|
p.internal_metadata.stream_ordering
|
2022-02-21 13:37:04 -05:00
|
|
|
|
< last_successful_stream_ordering
|
2021-03-18 11:52:26 -04:00
|
|
|
|
):
|
|
|
|
|
continue
|
2020-09-15 04:07:19 -04:00
|
|
|
|
|
2021-03-18 11:52:26 -04:00
|
|
|
|
new_pdus.append(p)
|
|
|
|
|
|
2022-06-07 09:35:56 -04:00
|
|
|
|
# Filter out events where the server is not in the room,
|
|
|
|
|
# e.g. it may have left/been kicked. *Ideally* we'd pull
|
|
|
|
|
# out the kick and send that, but it's a rare edge case
|
|
|
|
|
# so we don't bother for now (the server that sent the
|
|
|
|
|
# kick should send it out if its online).
|
|
|
|
|
new_pdus = await filter_events_for_server(
|
|
|
|
|
self._storage_controllers,
|
|
|
|
|
self._destination,
|
2022-11-21 10:46:14 -05:00
|
|
|
|
self._server_name,
|
2022-06-07 09:35:56 -04:00
|
|
|
|
new_pdus,
|
|
|
|
|
redact=False,
|
2023-03-10 10:31:25 -05:00
|
|
|
|
filter_out_erased_senders=True,
|
|
|
|
|
filter_out_remote_partial_state_events=True,
|
2022-06-07 09:35:56 -04:00
|
|
|
|
)
|
|
|
|
|
|
2021-03-18 11:52:26 -04:00
|
|
|
|
# If we've filtered out all the extremities, fall back to
|
|
|
|
|
# sending the original event. This should ensure that the
|
|
|
|
|
# server gets at least some of missed events (especially if
|
|
|
|
|
# the other sending servers are up).
|
|
|
|
|
if new_pdus:
|
|
|
|
|
room_catchup_pdus = new_pdus
|
2021-03-24 12:12:47 -04:00
|
|
|
|
else:
|
|
|
|
|
room_catchup_pdus = [pdu]
|
2021-03-18 11:52:26 -04:00
|
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
|
"Catching up rooms to %s: %r", self._destination, pdu.room_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await self._transaction_manager.send_new_transaction(
|
|
|
|
|
self._destination, room_catchup_pdus, []
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sent_transactions_counter.inc()
|
|
|
|
|
|
|
|
|
|
# We pulled this from the DB, so it'll be non-null
|
|
|
|
|
assert pdu.internal_metadata.stream_ordering
|
|
|
|
|
|
|
|
|
|
# Note that we mark the last successful stream ordering as that
|
|
|
|
|
# from the *original* PDU, rather than the PDU(s) we actually
|
|
|
|
|
# send. This is because we use it to mark our position in the
|
|
|
|
|
# queue of missed PDUs to process.
|
2022-02-21 13:37:04 -05:00
|
|
|
|
last_successful_stream_ordering = pdu.internal_metadata.stream_ordering
|
2021-03-18 11:52:26 -04:00
|
|
|
|
|
2022-02-21 13:37:04 -05:00
|
|
|
|
self._last_successful_stream_ordering = last_successful_stream_ordering
|
2021-03-18 11:52:26 -04:00
|
|
|
|
await self._store.set_destination_last_successful_stream_ordering(
|
2022-02-21 13:37:04 -05:00
|
|
|
|
self._destination, last_successful_stream_ordering
|
2021-03-18 11:52:26 -04:00
|
|
|
|
)
|
2020-09-15 04:07:19 -04:00
|
|
|
|
|
2022-11-28 09:40:17 -05:00
|
|
|
|
def _get_receipt_edus(self, force_flush: bool, limit: int) -> Iterable[Edu]:
|
|
|
|
|
if not self._pending_receipt_edus:
|
2019-03-20 12:02:25 -04:00
|
|
|
|
return
|
|
|
|
|
if not force_flush and not self._rrs_pending_flush:
|
|
|
|
|
# not yet time for this lot
|
|
|
|
|
return
|
|
|
|
|
|
2022-11-28 09:40:17 -05:00
|
|
|
|
# Send at most limit EDUs for receipts.
|
|
|
|
|
for content in self._pending_receipt_edus[:limit]:
|
|
|
|
|
yield Edu(
|
|
|
|
|
origin=self._server_name,
|
|
|
|
|
destination=self._destination,
|
|
|
|
|
edu_type=EduTypes.RECEIPT,
|
|
|
|
|
content=content,
|
|
|
|
|
)
|
|
|
|
|
self._pending_receipt_edus = self._pending_receipt_edus[limit:]
|
|
|
|
|
|
|
|
|
|
# If there are still pending read-receipts, don't reset the pending flush
|
|
|
|
|
# flag.
|
|
|
|
|
if not self._pending_receipt_edus:
|
|
|
|
|
self._rrs_pending_flush = False
|
2019-03-20 12:02:25 -04:00
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
def _pop_pending_edus(self, limit: int) -> List[Edu]:
|
2019-03-20 12:02:25 -04:00
|
|
|
|
pending_edus = self._pending_edus
|
|
|
|
|
pending_edus, self._pending_edus = pending_edus[:limit], pending_edus[limit:]
|
|
|
|
|
return pending_edus
|
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
async def _get_device_update_edus(self, limit: int) -> Tuple[List[Edu], int]:
|
2019-05-09 06:01:41 -04:00
|
|
|
|
last_device_list = self._last_device_list_stream_id
|
2019-06-06 18:54:00 -04:00
|
|
|
|
|
|
|
|
|
# Retrieve list of new device updates to send to the destination
|
2020-02-07 08:56:38 -05:00
|
|
|
|
now_stream_id, results = await self._store.get_device_updates_by_remote(
|
2019-06-06 18:54:00 -04:00
|
|
|
|
self._destination, last_device_list, limit=limit
|
2019-03-13 16:02:56 -04:00
|
|
|
|
)
|
|
|
|
|
edus = [
|
|
|
|
|
Edu(
|
|
|
|
|
origin=self._server_name,
|
|
|
|
|
destination=self._destination,
|
2019-05-22 16:42:00 -04:00
|
|
|
|
edu_type=edu_type,
|
2019-03-13 16:02:56 -04:00
|
|
|
|
content=content,
|
|
|
|
|
)
|
2019-05-22 16:42:00 -04:00
|
|
|
|
for (edu_type, content) in results
|
2019-03-13 16:02:56 -04:00
|
|
|
|
]
|
|
|
|
|
|
2019-10-30 14:57:34 -04:00
|
|
|
|
assert len(edus) <= limit, "get_device_updates_by_remote returned too many EDUs"
|
2019-05-09 06:01:41 -04:00
|
|
|
|
|
2021-09-23 06:59:07 -04:00
|
|
|
|
return edus, now_stream_id
|
2019-06-13 08:52:08 -04:00
|
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
|
async def _get_to_device_message_edus(self, limit: int) -> Tuple[List[Edu], int]:
|
2019-05-09 06:01:41 -04:00
|
|
|
|
last_device_stream_id = self._last_device_stream_id
|
|
|
|
|
to_device_stream_id = self._store.get_to_device_stream_token()
|
2020-02-07 08:56:38 -05:00
|
|
|
|
contents, stream_id = await self._store.get_new_device_msgs_for_remote(
|
2019-05-09 11:27:02 -04:00
|
|
|
|
self._destination, last_device_stream_id, to_device_stream_id, limit
|
2019-03-13 16:02:56 -04:00
|
|
|
|
)
|
2021-04-01 12:08:21 -04:00
|
|
|
|
for content in contents:
|
|
|
|
|
message_id = content.get("message_id")
|
|
|
|
|
if not message_id:
|
|
|
|
|
continue
|
|
|
|
|
|
2022-12-06 04:52:55 -05:00
|
|
|
|
set_tag(SynapseTags.TO_DEVICE_EDU_ID, message_id)
|
2021-04-01 12:08:21 -04:00
|
|
|
|
|
2019-06-13 08:52:08 -04:00
|
|
|
|
edus = [
|
2019-03-13 16:02:56 -04:00
|
|
|
|
Edu(
|
|
|
|
|
origin=self._server_name,
|
|
|
|
|
destination=self._destination,
|
2022-05-27 07:14:36 -04:00
|
|
|
|
edu_type=EduTypes.DIRECT_TO_DEVICE,
|
2019-03-13 16:02:56 -04:00
|
|
|
|
content=content,
|
|
|
|
|
)
|
2019-05-09 06:01:41 -04:00
|
|
|
|
for content in contents
|
2019-06-13 08:52:08 -04:00
|
|
|
|
]
|
2019-05-09 06:01:41 -04:00
|
|
|
|
|
2021-05-11 06:04:03 -04:00
|
|
|
|
if edus:
|
|
|
|
|
issue9533_logger.debug(
|
|
|
|
|
"Sending %i to-device messages to %s, up to stream id %i",
|
|
|
|
|
len(edus),
|
|
|
|
|
self._destination,
|
|
|
|
|
stream_id,
|
|
|
|
|
)
|
|
|
|
|
|
2021-09-23 06:59:07 -04:00
|
|
|
|
return edus, stream_id
|
2020-09-15 04:07:19 -04:00
|
|
|
|
|
|
|
|
|
def _start_catching_up(self) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Marks this destination as being in catch-up mode.
|
|
|
|
|
|
|
|
|
|
This throws away the PDU queue.
|
|
|
|
|
"""
|
|
|
|
|
self._catching_up = True
|
|
|
|
|
self._pending_pdus = []
|
2021-03-15 10:42:40 -04:00
|
|
|
|
|
|
|
|
|
|
2022-01-13 08:49:28 -05:00
|
|
|
|
@attr.s(slots=True, auto_attribs=True)
|
2021-03-15 10:42:40 -04:00
|
|
|
|
class _TransactionQueueManager:
|
|
|
|
|
"""A helper async context manager for pulling stuff off the queues and
|
|
|
|
|
tracking what was last successfully sent, etc.
|
|
|
|
|
"""
|
|
|
|
|
|
2022-01-13 08:49:28 -05:00
|
|
|
|
queue: PerDestinationQueue
|
2021-03-15 10:42:40 -04:00
|
|
|
|
|
2022-01-13 08:49:28 -05:00
|
|
|
|
_device_stream_id: Optional[int] = None
|
|
|
|
|
_device_list_id: Optional[int] = None
|
|
|
|
|
_last_stream_ordering: Optional[int] = None
|
|
|
|
|
_pdus: List[EventBase] = attr.Factory(list)
|
2021-03-15 10:42:40 -04:00
|
|
|
|
|
|
|
|
|
async def __aenter__(self) -> Tuple[List[EventBase], List[Edu]]:
|
|
|
|
|
# First we calculate the EDUs we want to send, if any.
|
|
|
|
|
|
2022-11-28 09:40:17 -05:00
|
|
|
|
# There's a maximum number of EDUs that can be sent with a transaction,
|
|
|
|
|
# generally device updates and to-device messages get priority, but we
|
|
|
|
|
# want to ensure that there's room for some other EDUs as well.
|
|
|
|
|
#
|
|
|
|
|
# This is done by:
|
|
|
|
|
#
|
|
|
|
|
# * Add a presence EDU, if one exists.
|
|
|
|
|
# * Add up-to a small limit of read receipt EDUs.
|
|
|
|
|
# * Add to-device EDUs, but leave some space for device list updates.
|
|
|
|
|
# * Add device list updates EDUs.
|
|
|
|
|
# * If there's any remaining room, add other EDUs.
|
|
|
|
|
pending_edus = []
|
|
|
|
|
|
|
|
|
|
# Add presence EDU.
|
|
|
|
|
if self.queue._pending_presence:
|
2024-07-01 07:48:36 -04:00
|
|
|
|
# Only send max 50 presence entries in the EDU, to bound the amount
|
|
|
|
|
# of data we're sending.
|
|
|
|
|
presence_to_add: List[JsonDict] = []
|
|
|
|
|
while (
|
|
|
|
|
self.queue._pending_presence
|
|
|
|
|
and len(presence_to_add) < MAX_PRESENCE_STATES_PER_EDU
|
|
|
|
|
):
|
|
|
|
|
_, presence = self.queue._pending_presence.popitem(last=False)
|
|
|
|
|
presence_to_add.append(
|
|
|
|
|
format_user_presence_state(presence, self.queue._clock.time_msec())
|
|
|
|
|
)
|
|
|
|
|
|
2022-11-28 09:40:17 -05:00
|
|
|
|
pending_edus.append(
|
|
|
|
|
Edu(
|
|
|
|
|
origin=self.queue._server_name,
|
|
|
|
|
destination=self.queue._destination,
|
|
|
|
|
edu_type=EduTypes.PRESENCE,
|
2024-07-01 07:48:36 -04:00
|
|
|
|
content={"push": presence_to_add},
|
2022-11-28 09:40:17 -05:00
|
|
|
|
)
|
|
|
|
|
)
|
2021-03-15 10:42:40 -04:00
|
|
|
|
|
2022-11-28 09:40:17 -05:00
|
|
|
|
# Add read receipt EDUs.
|
|
|
|
|
pending_edus.extend(self.queue._get_receipt_edus(force_flush=False, limit=5))
|
|
|
|
|
edu_limit = MAX_EDUS_PER_TRANSACTION - len(pending_edus)
|
|
|
|
|
|
|
|
|
|
# Next, prioritize to-device messages so that existing encryption channels
|
2022-09-27 10:17:41 -04:00
|
|
|
|
# work. We also keep a few slots spare (by reducing the limit) so that
|
|
|
|
|
# we can still trickle out some device list updates.
|
2021-03-15 10:42:40 -04:00
|
|
|
|
(
|
|
|
|
|
to_device_edus,
|
|
|
|
|
device_stream_id,
|
2022-11-28 09:40:17 -05:00
|
|
|
|
) = await self.queue._get_to_device_message_edus(edu_limit - 10)
|
2021-03-15 10:42:40 -04:00
|
|
|
|
|
|
|
|
|
if to_device_edus:
|
|
|
|
|
self._device_stream_id = device_stream_id
|
|
|
|
|
else:
|
|
|
|
|
self.queue._last_device_stream_id = device_stream_id
|
|
|
|
|
|
2022-11-28 09:40:17 -05:00
|
|
|
|
pending_edus.extend(to_device_edus)
|
|
|
|
|
edu_limit -= len(to_device_edus)
|
2022-09-27 10:17:41 -04:00
|
|
|
|
|
2022-11-28 09:40:17 -05:00
|
|
|
|
# Add device list update EDUs.
|
2022-09-27 10:17:41 -04:00
|
|
|
|
device_update_edus, dev_list_id = await self.queue._get_device_update_edus(
|
2022-11-28 09:40:17 -05:00
|
|
|
|
edu_limit
|
2022-09-27 10:17:41 -04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if device_update_edus:
|
|
|
|
|
self._device_list_id = dev_list_id
|
|
|
|
|
else:
|
|
|
|
|
self.queue._last_device_list_stream_id = dev_list_id
|
|
|
|
|
|
2022-11-28 09:40:17 -05:00
|
|
|
|
pending_edus.extend(device_update_edus)
|
|
|
|
|
edu_limit -= len(device_update_edus)
|
2021-03-15 10:42:40 -04:00
|
|
|
|
|
|
|
|
|
# Finally add any other types of EDUs if there is room.
|
2022-11-28 09:40:17 -05:00
|
|
|
|
other_edus = self.queue._pop_pending_edus(edu_limit)
|
|
|
|
|
pending_edus.extend(other_edus)
|
|
|
|
|
edu_limit -= len(other_edus)
|
|
|
|
|
while edu_limit > 0 and self.queue._pending_edus_keyed:
|
2021-03-15 10:42:40 -04:00
|
|
|
|
_, val = self.queue._pending_edus_keyed.popitem()
|
|
|
|
|
pending_edus.append(val)
|
2022-11-28 09:40:17 -05:00
|
|
|
|
edu_limit -= 1
|
2021-03-15 10:42:40 -04:00
|
|
|
|
|
|
|
|
|
# Now we look for any PDUs to send, by getting up to 50 PDUs from the
|
|
|
|
|
# queue
|
|
|
|
|
self._pdus = self.queue._pending_pdus[:50]
|
|
|
|
|
|
|
|
|
|
if not self._pdus and not pending_edus:
|
|
|
|
|
return [], []
|
|
|
|
|
|
|
|
|
|
# if we've decided to send a transaction anyway, and we have room, we
|
|
|
|
|
# may as well send any pending RRs
|
2022-11-28 09:40:17 -05:00
|
|
|
|
if edu_limit:
|
|
|
|
|
pending_edus.extend(
|
|
|
|
|
self.queue._get_receipt_edus(force_flush=True, limit=edu_limit)
|
|
|
|
|
)
|
2021-03-15 10:42:40 -04:00
|
|
|
|
|
|
|
|
|
if self._pdus:
|
|
|
|
|
self._last_stream_ordering = self._pdus[
|
|
|
|
|
-1
|
|
|
|
|
].internal_metadata.stream_ordering
|
|
|
|
|
assert self._last_stream_ordering
|
|
|
|
|
|
|
|
|
|
return self._pdus, pending_edus
|
|
|
|
|
|
2021-12-02 11:18:10 -05:00
|
|
|
|
async def __aexit__(
|
|
|
|
|
self,
|
|
|
|
|
exc_type: Optional[Type[BaseException]],
|
|
|
|
|
exc: Optional[BaseException],
|
|
|
|
|
tb: Optional[TracebackType],
|
|
|
|
|
) -> None:
|
2021-03-15 10:42:40 -04:00
|
|
|
|
if exc_type is not None:
|
|
|
|
|
# Failed to send transaction, so we bail out.
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Successfully sent transactions, so we remove pending PDUs from the queue
|
|
|
|
|
if self._pdus:
|
|
|
|
|
self.queue._pending_pdus = self.queue._pending_pdus[len(self._pdus) :]
|
|
|
|
|
|
|
|
|
|
# Succeeded to send the transaction so we record where we have sent up
|
|
|
|
|
# to in the various streams
|
|
|
|
|
|
|
|
|
|
if self._device_stream_id:
|
|
|
|
|
await self.queue._store.delete_device_msgs_for_remote(
|
|
|
|
|
self.queue._destination, self._device_stream_id
|
|
|
|
|
)
|
|
|
|
|
self.queue._last_device_stream_id = self._device_stream_id
|
|
|
|
|
|
|
|
|
|
# also mark the device updates as sent
|
|
|
|
|
if self._device_list_id:
|
|
|
|
|
logger.info(
|
|
|
|
|
"Marking as sent %r %r", self.queue._destination, self._device_list_id
|
|
|
|
|
)
|
|
|
|
|
await self.queue._store.mark_as_sent_devices_by_remote(
|
|
|
|
|
self.queue._destination, self._device_list_id
|
|
|
|
|
)
|
|
|
|
|
self.queue._last_device_list_stream_id = self._device_list_id
|
|
|
|
|
|
|
|
|
|
if self._last_stream_ordering:
|
|
|
|
|
# we sent some PDUs and it was successful, so update our
|
|
|
|
|
# last_successful_stream_ordering in the destinations table.
|
|
|
|
|
await self.queue._store.set_destination_last_successful_stream_ordering(
|
|
|
|
|
self.queue._destination, self._last_stream_ordering
|
|
|
|
|
)
|