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

Commit

Permalink
pass a reactor into SynapseSite
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Apr 23, 2021
1 parent 3186324 commit fa2df73
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 24 deletions.
1 change: 1 addition & 0 deletions changelog.d/9874.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pass a reactor into `SynapseSite` to make testing easier.
1 change: 1 addition & 0 deletions synapse/app/generic_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ def _listen_http(self, listener_config: ListenerConfig):
listener_config,
root_resource,
self.version_string,
reactor=self.get_reactor(),
),
reactor=self.get_reactor(),
)
Expand Down
25 changes: 10 additions & 15 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,20 @@ def _listener_http(self, config: HomeServerConfig, listener_config: ListenerConf
else:
root_resource = OptionsResource()

root_resource = create_resource_tree(resources, root_resource)
site = SynapseSite(
"synapse.access.%s.%s" % ("https" if tls else "http", site_tag),
site_tag,
listener_config,
create_resource_tree(resources, root_resource),
self.version_string,
reactor=self.get_reactor(),
)

if tls:
ports = listen_ssl(
bind_addresses,
port,
SynapseSite(
"synapse.access.https.%s" % (site_tag,),
site_tag,
listener_config,
root_resource,
self.version_string,
),
site,
self.tls_server_context_factory,
reactor=self.get_reactor(),
)
Expand All @@ -148,13 +149,7 @@ def _listener_http(self, config: HomeServerConfig, listener_config: ListenerConf
ports = listen_tcp(
bind_addresses,
port,
SynapseSite(
"synapse.access.http.%s" % (site_tag,),
site_tag,
listener_config,
root_resource,
self.version_string,
),
site,
reactor=self.get_reactor(),
)
logger.info("Synapse now listening on TCP port %d", port)
Expand Down
37 changes: 28 additions & 9 deletions synapse/http/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import attr
from zope.interface import implementer

from twisted.internet.interfaces import IAddress
from twisted.internet.interfaces import IAddress, IReactorTime
from twisted.python.failure import Failure
from twisted.web.resource import IResource
from twisted.web.server import Request, Site

from synapse.config.server import ListenerConfig
Expand Down Expand Up @@ -485,21 +486,39 @@ class _XForwardedForAddress:

class SynapseSite(Site):
"""
Subclass of a twisted http Site that does access logging with python's
standard logging
Synapse-specific twisted http Site
This does two main things.
First, it replaces the requestFactory in use so that we build SynapseRequests
instead of regular t.w.server.Requests. All of the constructor params are really
just parameters for SynapseRequest.
Second, it inhibits the log() method called by Request.finish, since SynapseRequest
does its own logging.
"""

def __init__(
self,
logger_name,
site_tag,
logger_name: str,
site_tag: str,
config: ListenerConfig,
resource,
resource: IResource,
server_version_string,
*args,
**kwargs,
reactor: IReactorTime,
):
Site.__init__(self, resource, *args, **kwargs)
"""
Args:
logger_name: The name of the logger to use for access logs.
site_tag: A tag to use for this site - mostly in access logs.
config: Configuration for the HTTP listener corresponding to this site
resource: The base of the resource tree to be used for serving requests on
this site
server_version_string: A string to present for the Server header
reactor: reactor to be used to manage connection timeouts
"""
Site.__init__(self, resource, reactor=reactor)

self.site_tag = site_tag

Expand Down
1 change: 1 addition & 0 deletions tests/replication/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ def make_worker_hs(
config=worker_hs.config.server.listeners[0],
resource=resource,
server_version_string="1",
reactor=self.reactor,
)

if worker_hs.config.redis.redis_enabled:
Expand Down
1 change: 1 addition & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def _make_request(self, method, path):
parse_listener_def({"type": "http", "port": 0}),
self.resource,
"1.0",
reactor=self.reactor,
)

# render the request and return the channel
Expand Down
1 change: 1 addition & 0 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def setUp(self):
config=self.hs.config.server.listeners[0],
resource=self.resource,
server_version_string="1",
reactor=self.reactor,
)

from tests.rest.client.v1.utils import RestHelper
Expand Down

0 comments on commit fa2df73

Please sign in to comment.