mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-10-01 11:49:51 -04:00
Remove some unused distributor signals (#8216)
Removes the `user_joined_room` and stops calling it since there are no observers. Also cleans-up some other unused signals and related code.
This commit is contained in:
parent
c9dbee50ae
commit
2ea1c68249
1
changelog.d/8216.misc
Normal file
1
changelog.d/8216.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Simplify the distributor code to avoid unnecessary work.
|
@ -39,10 +39,6 @@ class EventStreamHandler(BaseHandler):
|
|||||||
def __init__(self, hs: "HomeServer"):
|
def __init__(self, hs: "HomeServer"):
|
||||||
super(EventStreamHandler, self).__init__(hs)
|
super(EventStreamHandler, self).__init__(hs)
|
||||||
|
|
||||||
self.distributor = hs.get_distributor()
|
|
||||||
self.distributor.declare("started_user_eventstream")
|
|
||||||
self.distributor.declare("stopped_user_eventstream")
|
|
||||||
|
|
||||||
self.clock = hs.get_clock()
|
self.clock = hs.get_clock()
|
||||||
|
|
||||||
self.notifier = hs.get_notifier()
|
self.notifier = hs.get_notifier()
|
||||||
|
@ -69,7 +69,6 @@ from synapse.replication.http.federation import (
|
|||||||
ReplicationFederationSendEventsRestServlet,
|
ReplicationFederationSendEventsRestServlet,
|
||||||
ReplicationStoreRoomOnInviteRestServlet,
|
ReplicationStoreRoomOnInviteRestServlet,
|
||||||
)
|
)
|
||||||
from synapse.replication.http.membership import ReplicationUserJoinedLeftRoomRestServlet
|
|
||||||
from synapse.state import StateResolutionStore, resolve_events_with_store
|
from synapse.state import StateResolutionStore, resolve_events_with_store
|
||||||
from synapse.storage.databases.main.events_worker import EventRedactBehaviour
|
from synapse.storage.databases.main.events_worker import EventRedactBehaviour
|
||||||
from synapse.types import (
|
from synapse.types import (
|
||||||
@ -80,7 +79,6 @@ from synapse.types import (
|
|||||||
get_domain_from_id,
|
get_domain_from_id,
|
||||||
)
|
)
|
||||||
from synapse.util.async_helpers import Linearizer, concurrently_execute
|
from synapse.util.async_helpers import Linearizer, concurrently_execute
|
||||||
from synapse.util.distributor import user_joined_room
|
|
||||||
from synapse.util.retryutils import NotRetryingDestination
|
from synapse.util.retryutils import NotRetryingDestination
|
||||||
from synapse.util.stringutils import shortstr
|
from synapse.util.stringutils import shortstr
|
||||||
from synapse.visibility import filter_events_for_server
|
from synapse.visibility import filter_events_for_server
|
||||||
@ -141,9 +139,6 @@ class FederationHandler(BaseHandler):
|
|||||||
self._replication = hs.get_replication_data_handler()
|
self._replication = hs.get_replication_data_handler()
|
||||||
|
|
||||||
self._send_events = ReplicationFederationSendEventsRestServlet.make_client(hs)
|
self._send_events = ReplicationFederationSendEventsRestServlet.make_client(hs)
|
||||||
self._notify_user_membership_change = ReplicationUserJoinedLeftRoomRestServlet.make_client(
|
|
||||||
hs
|
|
||||||
)
|
|
||||||
self._clean_room_for_join_client = ReplicationCleanRoomRestServlet.make_client(
|
self._clean_room_for_join_client = ReplicationCleanRoomRestServlet.make_client(
|
||||||
hs
|
hs
|
||||||
)
|
)
|
||||||
@ -704,31 +699,10 @@ class FederationHandler(BaseHandler):
|
|||||||
logger.debug("[%s %s] Processing event: %s", room_id, event_id, event)
|
logger.debug("[%s %s] Processing event: %s", room_id, event_id, event)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
context = await self._handle_new_event(origin, event, state=state)
|
await self._handle_new_event(origin, event, state=state)
|
||||||
except AuthError as e:
|
except AuthError as e:
|
||||||
raise FederationError("ERROR", e.code, e.msg, affected=event.event_id)
|
raise FederationError("ERROR", e.code, e.msg, affected=event.event_id)
|
||||||
|
|
||||||
if event.type == EventTypes.Member:
|
|
||||||
if event.membership == Membership.JOIN:
|
|
||||||
# Only fire user_joined_room if the user has acutally
|
|
||||||
# joined the room. Don't bother if the user is just
|
|
||||||
# changing their profile info.
|
|
||||||
newly_joined = True
|
|
||||||
|
|
||||||
prev_state_ids = await context.get_prev_state_ids()
|
|
||||||
|
|
||||||
prev_state_id = prev_state_ids.get((event.type, event.state_key))
|
|
||||||
if prev_state_id:
|
|
||||||
prev_state = await self.store.get_event(
|
|
||||||
prev_state_id, allow_none=True
|
|
||||||
)
|
|
||||||
if prev_state and prev_state.membership == Membership.JOIN:
|
|
||||||
newly_joined = False
|
|
||||||
|
|
||||||
if newly_joined:
|
|
||||||
user = UserID.from_string(event.state_key)
|
|
||||||
await self.user_joined_room(user, room_id)
|
|
||||||
|
|
||||||
# For encrypted messages we check that we know about the sending device,
|
# For encrypted messages we check that we know about the sending device,
|
||||||
# if we don't then we mark the device cache for that user as stale.
|
# if we don't then we mark the device cache for that user as stale.
|
||||||
if event.type == EventTypes.Encrypted:
|
if event.type == EventTypes.Encrypted:
|
||||||
@ -1550,11 +1524,6 @@ class FederationHandler(BaseHandler):
|
|||||||
event.signatures,
|
event.signatures,
|
||||||
)
|
)
|
||||||
|
|
||||||
if event.type == EventTypes.Member:
|
|
||||||
if event.content["membership"] == Membership.JOIN:
|
|
||||||
user = UserID.from_string(event.state_key)
|
|
||||||
await self.user_joined_room(user, event.room_id)
|
|
||||||
|
|
||||||
prev_state_ids = await context.get_prev_state_ids()
|
prev_state_ids = await context.get_prev_state_ids()
|
||||||
|
|
||||||
state_ids = list(prev_state_ids.values())
|
state_ids = list(prev_state_ids.values())
|
||||||
@ -2984,16 +2953,6 @@ class FederationHandler(BaseHandler):
|
|||||||
else:
|
else:
|
||||||
await self.store.clean_room_for_join(room_id)
|
await self.store.clean_room_for_join(room_id)
|
||||||
|
|
||||||
async def user_joined_room(self, user: UserID, room_id: str) -> None:
|
|
||||||
"""Called when a new user has joined the room
|
|
||||||
"""
|
|
||||||
if self.config.worker_app:
|
|
||||||
await self._notify_user_membership_change(
|
|
||||||
room_id=room_id, user_id=user.to_string(), change="joined"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
user_joined_room(self.distributor, user, room_id)
|
|
||||||
|
|
||||||
async def get_room_complexity(
|
async def get_room_complexity(
|
||||||
self, remote_room_hosts: List[str], room_id: str
|
self, remote_room_hosts: List[str], room_id: str
|
||||||
) -> Optional[dict]:
|
) -> Optional[dict]:
|
||||||
|
@ -40,7 +40,7 @@ from synapse.events.validator import EventValidator
|
|||||||
from synapse.storage.roommember import RoomsForUser
|
from synapse.storage.roommember import RoomsForUser
|
||||||
from synapse.types import JsonDict, Requester, RoomAlias, RoomID, StateMap, UserID
|
from synapse.types import JsonDict, Requester, RoomAlias, RoomID, StateMap, UserID
|
||||||
from synapse.util.async_helpers import Linearizer
|
from synapse.util.async_helpers import Linearizer
|
||||||
from synapse.util.distributor import user_joined_room, user_left_room
|
from synapse.util.distributor import user_left_room
|
||||||
|
|
||||||
from ._base import BaseHandler
|
from ._base import BaseHandler
|
||||||
|
|
||||||
@ -148,17 +148,6 @@ class RoomMemberHandler:
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
async def _user_joined_room(self, target: UserID, room_id: str) -> None:
|
|
||||||
"""Notifies distributor on master process that the user has joined the
|
|
||||||
room.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
target
|
|
||||||
room_id
|
|
||||||
"""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def _user_left_room(self, target: UserID, room_id: str) -> None:
|
async def _user_left_room(self, target: UserID, room_id: str) -> None:
|
||||||
"""Notifies distributor on master process that the user has left the
|
"""Notifies distributor on master process that the user has left the
|
||||||
@ -221,7 +210,6 @@ class RoomMemberHandler:
|
|||||||
|
|
||||||
prev_member_event_id = prev_state_ids.get((EventTypes.Member, user_id), None)
|
prev_member_event_id = prev_state_ids.get((EventTypes.Member, user_id), None)
|
||||||
|
|
||||||
newly_joined = False
|
|
||||||
if event.membership == Membership.JOIN:
|
if event.membership == Membership.JOIN:
|
||||||
newly_joined = True
|
newly_joined = True
|
||||||
if prev_member_event_id:
|
if prev_member_event_id:
|
||||||
@ -246,12 +234,7 @@ class RoomMemberHandler:
|
|||||||
requester, event, context, extra_users=[target], ratelimit=ratelimit,
|
requester, event, context, extra_users=[target], ratelimit=ratelimit,
|
||||||
)
|
)
|
||||||
|
|
||||||
if event.membership == Membership.JOIN and newly_joined:
|
if event.membership == Membership.LEAVE:
|
||||||
# Only fire user_joined_room if the user has actually joined the
|
|
||||||
# room. Don't bother if the user is just changing their profile
|
|
||||||
# info.
|
|
||||||
await self._user_joined_room(target, room_id)
|
|
||||||
elif event.membership == Membership.LEAVE:
|
|
||||||
if prev_member_event_id:
|
if prev_member_event_id:
|
||||||
prev_member_event = await self.store.get_event(prev_member_event_id)
|
prev_member_event = await self.store.get_event(prev_member_event_id)
|
||||||
if prev_member_event.membership == Membership.JOIN:
|
if prev_member_event.membership == Membership.JOIN:
|
||||||
@ -726,17 +709,7 @@ class RoomMemberHandler:
|
|||||||
(EventTypes.Member, event.state_key), None
|
(EventTypes.Member, event.state_key), None
|
||||||
)
|
)
|
||||||
|
|
||||||
if event.membership == Membership.JOIN:
|
if event.membership == Membership.LEAVE:
|
||||||
# Only fire user_joined_room if the user has actually joined the
|
|
||||||
# room. Don't bother if the user is just changing their profile
|
|
||||||
# info.
|
|
||||||
newly_joined = True
|
|
||||||
if prev_member_event_id:
|
|
||||||
prev_member_event = await self.store.get_event(prev_member_event_id)
|
|
||||||
newly_joined = prev_member_event.membership != Membership.JOIN
|
|
||||||
if newly_joined:
|
|
||||||
await self._user_joined_room(target_user, room_id)
|
|
||||||
elif event.membership == Membership.LEAVE:
|
|
||||||
if prev_member_event_id:
|
if prev_member_event_id:
|
||||||
prev_member_event = await self.store.get_event(prev_member_event_id)
|
prev_member_event = await self.store.get_event(prev_member_event_id)
|
||||||
if prev_member_event.membership == Membership.JOIN:
|
if prev_member_event.membership == Membership.JOIN:
|
||||||
@ -1002,10 +975,9 @@ class RoomMemberHandler:
|
|||||||
|
|
||||||
class RoomMemberMasterHandler(RoomMemberHandler):
|
class RoomMemberMasterHandler(RoomMemberHandler):
|
||||||
def __init__(self, hs):
|
def __init__(self, hs):
|
||||||
super(RoomMemberMasterHandler, self).__init__(hs)
|
super().__init__(hs)
|
||||||
|
|
||||||
self.distributor = hs.get_distributor()
|
self.distributor = hs.get_distributor()
|
||||||
self.distributor.declare("user_joined_room")
|
|
||||||
self.distributor.declare("user_left_room")
|
self.distributor.declare("user_left_room")
|
||||||
|
|
||||||
async def _is_remote_room_too_complex(
|
async def _is_remote_room_too_complex(
|
||||||
@ -1085,7 +1057,6 @@ class RoomMemberMasterHandler(RoomMemberHandler):
|
|||||||
event_id, stream_id = await self.federation_handler.do_invite_join(
|
event_id, stream_id = await self.federation_handler.do_invite_join(
|
||||||
remote_room_hosts, room_id, user.to_string(), content
|
remote_room_hosts, room_id, user.to_string(), content
|
||||||
)
|
)
|
||||||
await self._user_joined_room(user, room_id)
|
|
||||||
|
|
||||||
# Check the room we just joined wasn't too large, if we didn't fetch the
|
# Check the room we just joined wasn't too large, if we didn't fetch the
|
||||||
# complexity of it before.
|
# complexity of it before.
|
||||||
@ -1228,11 +1199,6 @@ class RoomMemberMasterHandler(RoomMemberHandler):
|
|||||||
)
|
)
|
||||||
return event.event_id, stream_id
|
return event.event_id, stream_id
|
||||||
|
|
||||||
async def _user_joined_room(self, target: UserID, room_id: str) -> None:
|
|
||||||
"""Implements RoomMemberHandler._user_joined_room
|
|
||||||
"""
|
|
||||||
user_joined_room(self.distributor, target, room_id)
|
|
||||||
|
|
||||||
async def _user_left_room(self, target: UserID, room_id: str) -> None:
|
async def _user_left_room(self, target: UserID, room_id: str) -> None:
|
||||||
"""Implements RoomMemberHandler._user_left_room
|
"""Implements RoomMemberHandler._user_left_room
|
||||||
"""
|
"""
|
||||||
|
@ -57,8 +57,6 @@ class RoomMemberWorkerHandler(RoomMemberHandler):
|
|||||||
content=content,
|
content=content,
|
||||||
)
|
)
|
||||||
|
|
||||||
await self._user_joined_room(user, room_id)
|
|
||||||
|
|
||||||
return ret["event_id"], ret["stream_id"]
|
return ret["event_id"], ret["stream_id"]
|
||||||
|
|
||||||
async def remote_reject_invite(
|
async def remote_reject_invite(
|
||||||
@ -81,13 +79,6 @@ class RoomMemberWorkerHandler(RoomMemberHandler):
|
|||||||
)
|
)
|
||||||
return ret["event_id"], ret["stream_id"]
|
return ret["event_id"], ret["stream_id"]
|
||||||
|
|
||||||
async def _user_joined_room(self, target: UserID, room_id: str) -> None:
|
|
||||||
"""Implements RoomMemberHandler._user_joined_room
|
|
||||||
"""
|
|
||||||
await self._notify_change_client(
|
|
||||||
user_id=target.to_string(), room_id=room_id, change="joined"
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _user_left_room(self, target: UserID, room_id: str) -> None:
|
async def _user_left_room(self, target: UserID, room_id: str) -> None:
|
||||||
"""Implements RoomMemberHandler._user_left_room
|
"""Implements RoomMemberHandler._user_left_room
|
||||||
"""
|
"""
|
||||||
|
@ -19,7 +19,7 @@ from typing import TYPE_CHECKING, Optional
|
|||||||
from synapse.http.servlet import parse_json_object_from_request
|
from synapse.http.servlet import parse_json_object_from_request
|
||||||
from synapse.replication.http._base import ReplicationEndpoint
|
from synapse.replication.http._base import ReplicationEndpoint
|
||||||
from synapse.types import JsonDict, Requester, UserID
|
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:
|
if TYPE_CHECKING:
|
||||||
from synapse.server import HomeServer
|
from synapse.server import HomeServer
|
||||||
@ -181,9 +181,9 @@ class ReplicationUserJoinedLeftRoomRestServlet(ReplicationEndpoint):
|
|||||||
Args:
|
Args:
|
||||||
room_id (str)
|
room_id (str)
|
||||||
user_id (str)
|
user_id (str)
|
||||||
change (str): Either "joined" or "left"
|
change (str): "left"
|
||||||
"""
|
"""
|
||||||
assert change in ("joined", "left")
|
assert change == "left"
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@ -192,9 +192,7 @@ class ReplicationUserJoinedLeftRoomRestServlet(ReplicationEndpoint):
|
|||||||
|
|
||||||
user = UserID.from_string(user_id)
|
user = UserID.from_string(user_id)
|
||||||
|
|
||||||
if change == "joined":
|
if change == "left":
|
||||||
user_joined_room(self.distributor, user, room_id)
|
|
||||||
elif change == "left":
|
|
||||||
user_left_room(self.distributor, user, room_id)
|
user_left_room(self.distributor, user, room_id)
|
||||||
else:
|
else:
|
||||||
raise Exception("Unrecognized change: %r", change)
|
raise Exception("Unrecognized change: %r", change)
|
||||||
|
@ -16,8 +16,6 @@ import inspect
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from twisted.internet import defer
|
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 make_deferred_yieldable, run_in_background
|
||||||
from synapse.metrics.background_process_metrics import run_as_background_process
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
||||||
@ -29,11 +27,6 @@ def user_left_room(distributor, user, room_id):
|
|||||||
distributor.fire("user_left_room", user=user, room_id=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:
|
class Distributor:
|
||||||
"""A central dispatch point for loosely-connected pieces of code to
|
"""A central dispatch point for loosely-connected pieces of code to
|
||||||
register, observe, and fire signals.
|
register, observe, and fire signals.
|
||||||
@ -81,28 +74,6 @@ class Distributor:
|
|||||||
run_as_background_process(name, self.signals[name].fire, *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:
|
class Signal:
|
||||||
"""A Signal is a dispatch point that stores a list of callables as
|
"""A Signal is a dispatch point that stores a list of callables as
|
||||||
observers of it.
|
observers of it.
|
||||||
@ -132,22 +103,17 @@ class Signal:
|
|||||||
Returns a Deferred that will complete when all the observers have
|
Returns a Deferred that will complete when all the observers have
|
||||||
completed."""
|
completed."""
|
||||||
|
|
||||||
def do(observer):
|
async def do(observer):
|
||||||
def eb(failure):
|
try:
|
||||||
|
result = observer(*args, **kwargs)
|
||||||
|
if inspect.isawaitable(result):
|
||||||
|
result = await result
|
||||||
|
return result
|
||||||
|
except Exception as e:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"%s signal observer %s failed: %r",
|
"%s signal observer %s failed: %r", self.name, observer, e,
|
||||||
self.name,
|
|
||||||
observer,
|
|
||||||
failure,
|
|
||||||
exc_info=(
|
|
||||||
failure.type,
|
|
||||||
failure.value,
|
|
||||||
failure.getTracebackObject(),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return maybeAwaitableDeferred(observer, *args, **kwargs).addErrback(eb)
|
|
||||||
|
|
||||||
deferreds = [run_in_background(do, o) for o in self.observers]
|
deferreds = [run_in_background(do, o) for o in self.observers]
|
||||||
|
|
||||||
return make_deferred_yieldable(
|
return make_deferred_yieldable(
|
||||||
|
Loading…
Reference in New Issue
Block a user