-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Clean-up distributor code #8216
Changes from 5 commits
fe586de
026a25f
4179b38
6c27891
5cdd2e7
a0640de
b400db3
41ced4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Simplify the distributor code to avoid unnecessary work. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
from synapse.http.servlet import parse_json_object_from_request | ||
from synapse.replication.http._base import ReplicationEndpoint | ||
from synapse.types import JsonDict, Requester, UserID | ||
from synapse.util.distributor import user_joined_room, user_left_room | ||
from synapse.util.distributor import user_left_room | ||
|
||
if TYPE_CHECKING: | ||
from synapse.server import HomeServer | ||
|
@@ -181,9 +181,9 @@ async def _serialize_payload(room_id, user_id, change): | |
Args: | ||
room_id (str) | ||
user_id (str) | ||
change (str): Either "joined" or "left" | ||
change (str): "left" | ||
""" | ||
assert change in ("joined", "left") | ||
assert change == "left" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make more sense to update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shrugface. Maybe. Probably better to do some more complete refactoring to sort out the |
||
|
||
return {} | ||
|
||
|
@@ -192,9 +192,7 @@ def _handle_request(self, request, room_id, user_id, change): | |
|
||
user = UserID.from_string(user_id) | ||
|
||
if change == "joined": | ||
user_joined_room(self.distributor, user, room_id) | ||
elif change == "left": | ||
if change == "left": | ||
user_left_room(self.distributor, user, room_id) | ||
else: | ||
raise Exception("Unrecognized change: %r", change) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,7 @@ | |
import inspect | ||
import logging | ||
|
||
from twisted.internet import defer | ||
from twisted.internet.defer import Deferred, fail, succeed | ||
from twisted.python import failure | ||
|
||
from synapse.logging.context import make_deferred_yieldable, run_in_background | ||
from synapse.logging.context import run_in_background | ||
from synapse.metrics.background_process_metrics import run_as_background_process | ||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -29,11 +25,6 @@ def user_left_room(distributor, user, room_id): | |
distributor.fire("user_left_room", user=user, room_id=room_id) | ||
|
||
|
||
# XXX: this is no longer used. We should probably kill it. | ||
def user_joined_room(distributor, user, room_id): | ||
distributor.fire("user_joined_room", user=user, room_id=room_id) | ||
|
||
|
||
class Distributor(object): | ||
"""A central dispatch point for loosely-connected pieces of code to | ||
register, observe, and fire signals. | ||
|
@@ -81,28 +72,6 @@ def fire(self, name, *args, **kwargs): | |
run_as_background_process(name, self.signals[name].fire, *args, **kwargs) | ||
|
||
|
||
def maybeAwaitableDeferred(f, *args, **kw): | ||
""" | ||
Invoke a function that may or may not return a Deferred or an Awaitable. | ||
|
||
This is a modified version of twisted.internet.defer.maybeDeferred. | ||
""" | ||
try: | ||
result = f(*args, **kw) | ||
except Exception: | ||
return fail(failure.Failure(captureVars=Deferred.debug)) | ||
|
||
if isinstance(result, Deferred): | ||
return result | ||
# Handle the additional case of an awaitable being returned. | ||
elif inspect.isawaitable(result): | ||
return defer.ensureDeferred(result) | ||
elif isinstance(result, failure.Failure): | ||
return fail(result) | ||
else: | ||
return succeed(result) | ||
|
||
|
||
class Signal(object): | ||
"""A Signal is a dispatch point that stores a list of callables as | ||
observers of it. | ||
|
@@ -128,31 +97,21 @@ def fire(self, *args, **kwargs): | |
"""Invokes every callable in the observer list, passing in the args and | ||
kwargs. Exceptions thrown by observers are logged but ignored. It is | ||
not an error to fire a signal with no observers. | ||
""" | ||
|
||
Returns a Deferred that will complete when all the observers have | ||
completed.""" | ||
|
||
def do(observer): | ||
def eb(failure): | ||
async def do(observer): | ||
try: | ||
result = observer(*args, **kwargs) | ||
if inspect.isawaitable(result): | ||
result = await result | ||
return result | ||
except Exception as e: | ||
logger.warning( | ||
"%s signal observer %s failed: %r", | ||
self.name, | ||
observer, | ||
failure, | ||
exc_info=( | ||
failure.type, | ||
failure.value, | ||
failure.getTracebackObject(), | ||
), | ||
"%s signal observer %s failed: %r", self.name, observer, e, | ||
) | ||
|
||
return maybeAwaitableDeferred(observer, *args, **kwargs).addErrback(eb) | ||
|
||
deferreds = [run_in_background(do, o) for o in self.observers] | ||
|
||
return make_deferred_yieldable( | ||
defer.gatherResults(deferreds, consumeErrors=True) | ||
) | ||
for observer in self.observers: | ||
run_in_background(do, observer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is changing the semantics so that we no longer wait for the observers to complete. Is that deliberate? what is the logic behind it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reasoning behind this nothing uses the I can back out 6c27891 if you'd like. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/matrix-org/synapse/pull/8216/files/b400db3b5bd2d365d4e68b73f7d3f2a0530dd8a7#diff-755d6db2f812691f4b7ea7fd62894893R72 calls Signal.fire (and uses the deferred, via run_as_background_process) ?
I think that would be correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good call. 👍 I guess I was looking at places that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reverted in 41ced4c |
||
|
||
def __repr__(self): | ||
return "<Signal name=%r>" % (self.name,) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These variables were referenced anywhere else.