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

Commit

Permalink
Run push_receipts_to_remotes as background job (#4707)
Browse files Browse the repository at this point in the history
I suspect the CPU usage metrics for this are going to /dev/null at the moment.
  • Loading branch information
richvdh authored Feb 21, 2019
1 parent 16e0680 commit 6d65659
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
1 change: 1 addition & 0 deletions changelog.d/4707.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run push_receipts_to_remotes as background job.
68 changes: 34 additions & 34 deletions synapse/handlers/receipts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

from twisted.internet import defer

from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.types import get_domain_from_id
from synapse.util import logcontext

from ._base import BaseHandler

Expand Down Expand Up @@ -59,7 +59,9 @@ def received_client_receipt(self, room_id, receipt_type, user_id,
if is_new:
# fire off a process in the background to send the receipt to
# remote servers
self._push_remotes([receipt])
run_as_background_process(
'push_receipts_to_remotes', self._push_remotes, receipt
)

@defer.inlineCallbacks
def _received_remote_receipt(self, origin, content):
Expand Down Expand Up @@ -125,44 +127,42 @@ def _handle_new_receipts(self, receipts):

defer.returnValue(True)

@logcontext.preserve_fn # caller should not yield on this
@defer.inlineCallbacks
def _push_remotes(self, receipts):
"""Given a list of receipts, works out which remote servers should be
def _push_remotes(self, receipt):
"""Given a receipt, works out which remote servers should be
poked and pokes them.
"""
try:
# TODO: Some of this stuff should be coallesced.
for receipt in receipts:
room_id = receipt["room_id"]
receipt_type = receipt["receipt_type"]
user_id = receipt["user_id"]
event_ids = receipt["event_ids"]
data = receipt["data"]

users = yield self.state.get_current_user_in_room(room_id)
remotedomains = set(get_domain_from_id(u) for u in users)
remotedomains = remotedomains.copy()
remotedomains.discard(self.server_name)

logger.debug("Sending receipt to: %r", remotedomains)

for domain in remotedomains:
self.federation.send_edu(
destination=domain,
edu_type="m.receipt",
content={
room_id: {
receipt_type: {
user_id: {
"event_ids": event_ids,
"data": data,
}
# TODO: optimise this to move some of the work to the workers.
room_id = receipt["room_id"]
receipt_type = receipt["receipt_type"]
user_id = receipt["user_id"]
event_ids = receipt["event_ids"]
data = receipt["data"]

users = yield self.state.get_current_user_in_room(room_id)
remotedomains = set(get_domain_from_id(u) for u in users)
remotedomains = remotedomains.copy()
remotedomains.discard(self.server_name)

logger.debug("Sending receipt to: %r", remotedomains)

for domain in remotedomains:
self.federation.send_edu(
destination=domain,
edu_type="m.receipt",
content={
room_id: {
receipt_type: {
user_id: {
"event_ids": event_ids,
"data": data,
}
},
}
},
key=(room_id, receipt_type, user_id),
)
},
key=(room_id, receipt_type, user_id),
)
except Exception:
logger.exception("Error pushing receipts to remote servers")

Expand Down

0 comments on commit 6d65659

Please sign in to comment.