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

Commit b046747

Browse files
author
Sean Quah
committed
Remove unnecessary reactor reference from _PerHostRatelimiter
Fix up #14812 to avoid introducing a reference to the reactor. Signed-off-by: Sean Quah <[email protected]>
1 parent 73ff493 commit b046747

File tree

5 files changed

+7
-14
lines changed

5 files changed

+7
-14
lines changed

changelog.d/00000.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where Synapse would exhaust the stack when processing many federation requests where the remote homeserver has disconencted early.

synapse/rest/client/register.py

-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ def __init__(self, hs: "HomeServer"):
310310
self.hs = hs
311311
self.registration_handler = hs.get_registration_handler()
312312
self.ratelimiter = FederationRateLimiter(
313-
hs.get_reactor(),
314313
hs.get_clock(),
315314
FederationRatelimitSettings(
316315
# Time window of 2s

synapse/server.py

-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,6 @@ def get_replication_streams(self) -> Dict[str, Stream]:
768768
@cache_in_self
769769
def get_federation_ratelimiter(self) -> FederationRateLimiter:
770770
return FederationRateLimiter(
771-
self.get_reactor(),
772771
self.get_clock(),
773772
config=self.config.ratelimiting.rc_federation,
774773
metrics_name="federation_servlets",

synapse/util/ratelimitutils.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from typing_extensions import ContextManager
3535

3636
from twisted.internet import defer
37-
from twisted.internet.interfaces import IReactorTime
3837

3938
from synapse.api.errors import LimitExceededError
4039
from synapse.config.ratelimiting import FederationRatelimitSettings
@@ -147,14 +146,12 @@ class FederationRateLimiter:
147146

148147
def __init__(
149148
self,
150-
reactor: IReactorTime,
151149
clock: Clock,
152150
config: FederationRatelimitSettings,
153151
metrics_name: Optional[str] = None,
154152
):
155153
"""
156154
Args:
157-
reactor
158155
clock
159156
config
160157
metrics_name: The name of the rate limiter so we can differentiate it
@@ -166,7 +163,7 @@ def __init__(
166163

167164
def new_limiter() -> "_PerHostRatelimiter":
168165
return _PerHostRatelimiter(
169-
reactor=reactor, clock=clock, config=config, metrics_name=metrics_name
166+
clock=clock, config=config, metrics_name=metrics_name
170167
)
171168

172169
self.ratelimiters: DefaultDict[
@@ -197,22 +194,19 @@ def ratelimit(self, host: str) -> "_GeneratorContextManager[defer.Deferred[None]
197194
class _PerHostRatelimiter:
198195
def __init__(
199196
self,
200-
reactor: IReactorTime,
201197
clock: Clock,
202198
config: FederationRatelimitSettings,
203199
metrics_name: Optional[str] = None,
204200
):
205201
"""
206202
Args:
207-
reactor
208203
clock
209204
config
210205
metrics_name: The name of the rate limiter so we can differentiate it
211206
from the rest in the metrics. If `None`, we don't track metrics
212207
for this rate limiter.
213208
from the rest in the metrics
214209
"""
215-
self.reactor = reactor
216210
self.clock = clock
217211
self.metrics_name = metrics_name
218212

@@ -388,4 +382,4 @@ def start_next_request() -> None:
388382
except KeyError:
389383
pass
390384

391-
self.reactor.callLater(0.0, start_next_request)
385+
self.clock.call_later(0.0, start_next_request)

tests/util/test_ratelimitutils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_ratelimit(self) -> None:
3030
"""A simple test with the default values"""
3131
reactor, clock = get_clock()
3232
rc_config = build_rc_config()
33-
ratelimiter = FederationRateLimiter(reactor, clock, rc_config)
33+
ratelimiter = FederationRateLimiter(clock, rc_config)
3434

3535
with ratelimiter.ratelimit("testhost") as d1:
3636
# shouldn't block
@@ -40,7 +40,7 @@ def test_concurrent_limit(self) -> None:
4040
"""Test what happens when we hit the concurrent limit"""
4141
reactor, clock = get_clock()
4242
rc_config = build_rc_config({"rc_federation": {"concurrent": 2}})
43-
ratelimiter = FederationRateLimiter(reactor, clock, rc_config)
43+
ratelimiter = FederationRateLimiter(clock, rc_config)
4444

4545
with ratelimiter.ratelimit("testhost") as d1:
4646
# shouldn't block
@@ -67,7 +67,7 @@ def test_sleep_limit(self) -> None:
6767
rc_config = build_rc_config(
6868
{"rc_federation": {"sleep_limit": 2, "sleep_delay": 500}}
6969
)
70-
ratelimiter = FederationRateLimiter(reactor, clock, rc_config)
70+
ratelimiter = FederationRateLimiter(clock, rc_config)
7171

7272
with ratelimiter.ratelimit("testhost") as d1:
7373
# shouldn't block
@@ -98,7 +98,7 @@ def test_lots_of_queued_things(self) -> None:
9898
}
9999
}
100100
)
101-
ratelimiter = FederationRateLimiter(reactor, clock, rc_config)
101+
ratelimiter = FederationRateLimiter(clock, rc_config)
102102

103103
with ratelimiter.ratelimit("testhost") as d:
104104
# shouldn't block

0 commit comments

Comments
 (0)