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

Commit 975fd8c

Browse files
committed
Create a SynapseReactor type which incorporates the necessary reactor interfaces.
This allows passing around the proper type instead of pretending that ReactorBase has the proper methods.
1 parent aee1076 commit 975fd8c

File tree

8 files changed

+32
-12
lines changed

8 files changed

+32
-12
lines changed

changelog.d/9528.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix incorrect type hints.

synapse/handlers/acme.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ async def start_listening(self) -> None:
7373
"Listening for ACME requests on %s:%i", host, self.hs.config.acme_port
7474
)
7575
try:
76-
self.reactor.listenTCP(self.hs.config.acme_port, srv, interface=host)
76+
self.reactor.listenTCP(
77+
self.hs.config.acme_port, srv, backlog=50, interface=host
78+
)
7779
except twisted.internet.error.CannotListenError as e:
7880
check_bind_error(e, host, bind_addresses)
7981

synapse/http/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from synapse.http.proxyagent import ProxyAgent
6464
from synapse.logging.context import make_deferred_yieldable
6565
from synapse.logging.opentracing import set_tag, start_active_span, tags
66+
from synapse.types import ISynapseReactor
6667
from synapse.util import json_decoder
6768
from synapse.util.async_helpers import timeout_deferred
6869

@@ -199,7 +200,7 @@ def resolutionComplete() -> None:
199200
return r
200201

201202

202-
@implementer(IReactorPluggableNameResolver)
203+
@implementer(ISynapseReactor)
203204
class BlacklistingReactorWrapper:
204205
"""
205206
A Reactor wrapper which will prevent DNS resolution to blacklisted IP
@@ -324,7 +325,7 @@ def __init__(
324325
# filters out blacklisted IP addresses, to prevent DNS rebinding.
325326
self.reactor = BlacklistingReactorWrapper(
326327
hs.get_reactor(), self._ip_whitelist, self._ip_blacklist
327-
)
328+
) # type: ISynapseReactor
328329
else:
329330
self.reactor = hs.get_reactor()
330331

synapse/http/federation/matrix_federation_agent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from synapse.http.federation.srv_resolver import Server, SrvResolver
3636
from synapse.http.federation.well_known_resolver import WellKnownResolver
3737
from synapse.logging.context import make_deferred_yieldable, run_in_background
38+
from synapse.types import ISynapseReactor
3839
from synapse.util import Clock
3940

4041
logger = logging.getLogger(__name__)
@@ -68,7 +69,7 @@ class MatrixFederationAgent:
6869

6970
def __init__(
7071
self,
71-
reactor: IReactorCore,
72+
reactor: ISynapseReactor,
7273
tls_client_options_factory: Optional[FederationPolicyForHTTPS],
7374
user_agent: bytes,
7475
ip_blacklist: IPSet,

synapse/http/matrixfederationclient.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
start_active_span,
6060
tags,
6161
)
62-
from synapse.types import JsonDict
62+
from synapse.types import ISynapseReactor, JsonDict
6363
from synapse.util import json_decoder
6464
from synapse.util.async_helpers import timeout_deferred
6565
from synapse.util.metrics import Measure
@@ -237,14 +237,14 @@ def __init__(self, hs, tls_client_options_factory):
237237
# addresses, to prevent DNS rebinding.
238238
self.reactor = BlacklistingReactorWrapper(
239239
hs.get_reactor(), None, hs.config.federation_ip_range_blacklist
240-
)
240+
) # type: ISynapseReactor
241241

242242
user_agent = hs.version_string
243243
if hs.config.user_agent_suffix:
244244
user_agent = "%s %s" % (user_agent, hs.config.user_agent_suffix)
245245
user_agent = user_agent.encode("ascii")
246246

247-
self.agent = MatrixFederationAgent(
247+
federation_agent = MatrixFederationAgent(
248248
self.reactor,
249249
tls_client_options_factory,
250250
user_agent,
@@ -254,7 +254,7 @@ def __init__(self, hs, tls_client_options_factory):
254254
# Use a BlacklistingAgentWrapper to prevent circumventing the IP
255255
# blacklist via IP literals in server names
256256
self.agent = BlacklistingAgentWrapper(
257-
self.agent,
257+
federation_agent,
258258
ip_blacklist=hs.config.federation_ip_range_blacklist,
259259
)
260260

synapse/replication/tcp/redis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,6 @@ def lazyConnection(
328328
factory.continueTrying = reconnect
329329

330330
reactor = hs.get_reactor()
331-
reactor.connectTCP(host, port, factory, 30)
331+
reactor.connectTCP(host, port, factory, timeout=30, bindAddress=None)
332332

333333
return factory.handler

synapse/server.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
cast,
3737
)
3838

39-
import twisted.internet.base
4039
import twisted.internet.tcp
4140
from twisted.mail.smtp import sendmail
4241
from twisted.web.iweb import IPolicyForHTTPS
@@ -129,7 +128,7 @@
129128
from synapse.state import StateHandler, StateResolutionHandler
130129
from synapse.storage import Databases, DataStore, Storage
131130
from synapse.streams.events import EventSources
132-
from synapse.types import DomainSpecificString
131+
from synapse.types import DomainSpecificString, ISynapseReactor
133132
from synapse.util import Clock
134133
from synapse.util.distributor import Distributor
135134
from synapse.util.ratelimitutils import FederationRateLimiter
@@ -290,7 +289,7 @@ def setup_background_tasks(self) -> None:
290289
for i in self.REQUIRED_ON_BACKGROUND_TASK_STARTUP:
291290
getattr(self, "get_" + i + "_handler")()
292291

293-
def get_reactor(self) -> twisted.internet.base.ReactorBase:
292+
def get_reactor(self) -> ISynapseReactor:
294293
"""
295294
Fetch the Twisted reactor in use by this HomeServer.
296295
"""

synapse/types.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535
import attr
3636
from signedjson.key import decode_verify_key_bytes
3737
from unpaddedbase64 import decode_base64
38+
from zope.interface import Interface
39+
40+
from twisted.internet.interfaces import (
41+
IReactorCore,
42+
IReactorPluggableNameResolver,
43+
IReactorTCP,
44+
IReactorTime,
45+
)
3846

3947
from synapse.api.errors import Codes, SynapseError
4048
from synapse.util.stringutils import parse_and_validate_server_name
@@ -67,6 +75,14 @@ class Collection(Iterable[T_co], Container[T_co], Sized): # type: ignore
6775
JsonDict = Dict[str, Any]
6876

6977

78+
# Note that this seems to require inheriting *directly* from Interface in order
79+
# for mypy-zope to realize it is an interface.
80+
class ISynapseReactor(
81+
IReactorTCP, IReactorPluggableNameResolver, IReactorTime, IReactorCore, Interface
82+
):
83+
"""The interfaces necessary for Synapse to function."""
84+
85+
7086
class Requester(
7187
namedtuple(
7288
"Requester",

0 commit comments

Comments
 (0)