From 703bd63f2c0ae2ab49518438409d77dbf9bb090c Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Tue, 15 Feb 2022 12:06:13 +0000 Subject: [PATCH 1/3] Limit concurrent AS joins --- synapse/handlers/room_member.py | 44 +++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index bf1a47efb020..d1d43998de5d 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -82,6 +82,7 @@ def __init__(self, hs: "HomeServer"): self.event_auth_handler = hs.get_event_auth_handler() self.member_linearizer: Linearizer = Linearizer(name="member") + self.member_as_limiter = Linearizer(max_count=10, name="member_as_limiter") self.clock = hs.get_clock() self.spam_checker = hs.get_spam_checker() @@ -500,25 +501,30 @@ async def update_membership( key = (room_id,) - with (await self.member_linearizer.queue(key)): - result = await self.update_membership_locked( - requester, - target, - room_id, - action, - txn_id=txn_id, - remote_room_hosts=remote_room_hosts, - third_party_signed=third_party_signed, - ratelimit=ratelimit, - content=content, - new_room=new_room, - require_consent=require_consent, - outlier=outlier, - historical=historical, - allow_no_prev_events=allow_no_prev_events, - prev_event_ids=prev_event_ids, - auth_event_ids=auth_event_ids, - ) + as_id = object() + if requester.app_service: + as_id = requester.app_service.id + + with (await self.member_as_limiter.queue(as_id)): + with (await self.member_linearizer.queue(key)): + result = await self.update_membership_locked( + requester, + target, + room_id, + action, + txn_id=txn_id, + remote_room_hosts=remote_room_hosts, + third_party_signed=third_party_signed, + ratelimit=ratelimit, + content=content, + new_room=new_room, + require_consent=require_consent, + outlier=outlier, + historical=historical, + allow_no_prev_events=allow_no_prev_events, + prev_event_ids=prev_event_ids, + auth_event_ids=auth_event_ids, + ) return result From 2f4ef830ad505d8455dcb329751d7c6fabd144e7 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Tue, 15 Feb 2022 12:08:06 +0000 Subject: [PATCH 2/3] Changelog --- changelog.d/11996.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11996.misc diff --git a/changelog.d/11996.misc b/changelog.d/11996.misc new file mode 100644 index 000000000000..53f156b75e1f --- /dev/null +++ b/changelog.d/11996.misc @@ -0,0 +1 @@ +Limit concurrent applications service joins. From 2c6db66250110e0a7d87951f0b76406ec8b9bfb0 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Wed, 16 Feb 2022 10:46:17 +0000 Subject: [PATCH 3/3] Incorporate review --- changelog.d/11996.misc | 2 +- synapse/handlers/room_member.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/changelog.d/11996.misc b/changelog.d/11996.misc index 53f156b75e1f..6c675fd1931c 100644 --- a/changelog.d/11996.misc +++ b/changelog.d/11996.misc @@ -1 +1 @@ -Limit concurrent applications service joins. +Limit concurrent joins from applications services. \ No newline at end of file diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index d1d43998de5d..b2adc0f48be9 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -505,6 +505,8 @@ async def update_membership( if requester.app_service: as_id = requester.app_service.id + # We first linearise by the application service (to try to limit concurrent joins + # by application services), and then by room ID. with (await self.member_as_limiter.queue(as_id)): with (await self.member_linearizer.queue(key)): result = await self.update_membership_locked(