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

Commit

Permalink
Process EDUs in parallel with PDUs. (#6697)
Browse files Browse the repository at this point in the history
* commit 'b5ce7f587':
  Process EDUs in parallel with PDUs. (#6697)
  • Loading branch information
anoadragon453 committed Mar 23, 2020
2 parents 09a8668 + b5ce7f5 commit 9edb5f9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.d/6697.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Don't block processing of incoming EDUs behind processing PDUs in the same transaction.
70 changes: 58 additions & 12 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import Dict

import six
from six import iteritems

from canonicaljson import json
from prometheus_client import Counter

from twisted.internet import defer
from twisted.internet.abstract import isIPAddress
from twisted.python import failure

Expand All @@ -41,15 +43,19 @@
from synapse.federation.persistence import TransactionActions
from synapse.federation.units import Edu, Transaction
from synapse.http.endpoint import parse_server_name
from synapse.logging.context import nested_logging_context
from synapse.logging.context import (
make_deferred_yieldable,
nested_logging_context,
run_in_background,
)
from synapse.logging.opentracing import log_kv, start_active_span_from_edu, trace
from synapse.logging.utils import log_function
from synapse.replication.http.federation import (
ReplicationFederationSendEduRestServlet,
ReplicationGetQueryRestServlet,
)
from synapse.types import get_domain_from_id
from synapse.util import glob_to_regex
from synapse.util import glob_to_regex, unwrapFirstError
from synapse.util.async_helpers import Linearizer, concurrently_execute
from synapse.util.caches.response_cache import ResponseCache

Expand Down Expand Up @@ -160,6 +166,43 @@ async def _handle_incoming_transaction(self, origin, transaction, request_time):
)
return 400, response

# We process PDUs and EDUs in parallel. This is important as we don't
# want to block things like to device messages from reaching clients
# behind the potentially expensive handling of PDUs.
pdu_results, _ = await make_deferred_yieldable(
defer.gatherResults(
[
run_in_background(
self._handle_pdus_in_txn, origin, transaction, request_time
),
run_in_background(self._handle_edus_in_txn, origin, transaction),
],
consumeErrors=True,
).addErrback(unwrapFirstError)
)

response = {"pdus": pdu_results}

logger.debug("Returning: %s", str(response))

await self.transaction_actions.set_response(origin, transaction, 200, response)
return 200, response

async def _handle_pdus_in_txn(
self, origin: str, transaction: Transaction, request_time: int
) -> Dict[str, dict]:
"""Process the PDUs in a received transaction.
Args:
origin: the server making the request
transaction: incoming transaction
request_time: timestamp that the HTTP request arrived at
Returns:
A map from event ID of a processed PDU to any errors we should
report back to the sending server.
"""

received_pdus_counter.inc(len(transaction.pdus))

origin_host, _ = parse_server_name(origin)
Expand Down Expand Up @@ -250,20 +293,23 @@ async def process_pdus_for_room(room_id):
process_pdus_for_room, pdus_by_room.keys(), TRANSACTION_CONCURRENCY_LIMIT
)

if hasattr(transaction, "edus"):
for edu in (Edu(**x) for x in transaction.edus):
await self.received_edu(origin, edu.edu_type, edu.content)
return pdu_results

response = {"pdus": pdu_results}
async def _handle_edus_in_txn(self, origin: str, transaction: Transaction):
"""Process the EDUs in a received transaction.
"""

logger.debug("Returning: %s", str(response))
async def _process_edu(edu_dict):
received_edus_counter.inc()

await self.transaction_actions.set_response(origin, transaction, 200, response)
return 200, response
edu = Edu(**edu_dict)
await self.registry.on_edu(edu.edu_type, origin, edu.content)

async def received_edu(self, origin, edu_type, content):
received_edus_counter.inc()
await self.registry.on_edu(edu_type, origin, content)
await concurrently_execute(
_process_edu,
getattr(transaction, "edus", []),
TRANSACTION_CONCURRENCY_LIMIT,
)

async def on_context_state_request(self, origin, room_id, event_id):
origin_host, _ = parse_server_name(origin)
Expand Down

0 comments on commit 9edb5f9

Please sign in to comment.