Merge pull request #4636 from matrix-org/rav/bind_address_fixes

Fix errors when using default bind_addresses with replication/metrics listeners
This commit is contained in:
Richard van der Hoff 2019-02-13 14:16:02 +00:00 committed by GitHub
commit 464c301584
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 22 deletions

1
changelog.d/4636.bugfix Normal file
View File

@ -0,0 +1 @@
Fix errors when using default bind_addresses with replication/metrics listeners.

View File

@ -153,9 +153,8 @@ def listen_metrics(bind_addresses, port):
from prometheus_client import start_http_server from prometheus_client import start_http_server
for host in bind_addresses: for host in bind_addresses:
reactor.callInThread(start_http_server, int(port), logger.info("Starting metrics listener on %s:%d", host, port)
addr=host, registry=RegistryProxy) start_http_server(port, addr=host, registry=RegistryProxy)
logger.info("Metrics now reporting on %s:%d", host, port)
def listen_tcp(bind_addresses, port, factory, reactor=reactor, backlog=50): def listen_tcp(bind_addresses, port, factory, reactor=reactor, backlog=50):
@ -163,21 +162,23 @@ def listen_tcp(bind_addresses, port, factory, reactor=reactor, backlog=50):
Create a TCP socket for a port and several addresses Create a TCP socket for a port and several addresses
Returns: Returns:
list (empty) list[twisted.internet.tcp.Port]: listening for TCP connections
""" """
r = []
for address in bind_addresses: for address in bind_addresses:
try: try:
r.append(
reactor.listenTCP( reactor.listenTCP(
port, port,
factory, factory,
backlog, backlog,
address address
) )
)
except error.CannotListenError as e: except error.CannotListenError as e:
check_bind_error(e, address, bind_addresses) check_bind_error(e, address, bind_addresses)
logger.info("Synapse now listening on TCP port %d", port) return r
return []
def listen_ssl( def listen_ssl(
@ -204,7 +205,6 @@ def listen_ssl(
except error.CannotListenError as e: except error.CannotListenError as e:
check_bind_error(e, address, bind_addresses) check_bind_error(e, address, bind_addresses)
logger.info("Synapse now listening on port %d (TLS)", port)
return r return r
@ -230,6 +230,10 @@ def refresh_certificate(hs):
# requests. This factory attribute is public but missing from # requests. This factory attribute is public but missing from
# Twisted's documentation. # Twisted's documentation.
if isinstance(i.factory, TLSMemoryBIOFactory): if isinstance(i.factory, TLSMemoryBIOFactory):
addr = i.getHost()
logger.info(
"Replacing TLS context factory on [%s]:%i", addr.host, addr.port,
)
# We want to replace TLS factories with a new one, with the new # We want to replace TLS factories with a new one, with the new
# TLS configuration. We do this by reaching in and pulling out # TLS configuration. We do this by reaching in and pulling out
# the wrappedFactory, and then re-wrapping it. # the wrappedFactory, and then re-wrapping it.

View File

@ -121,7 +121,7 @@ class SynapseHomeServer(HomeServer):
root_resource = create_resource_tree(resources, root_resource) root_resource = create_resource_tree(resources, root_resource)
if tls: if tls:
return listen_ssl( ports = listen_ssl(
bind_addresses, bind_addresses,
port, port,
SynapseSite( SynapseSite(
@ -134,9 +134,10 @@ class SynapseHomeServer(HomeServer):
self.tls_server_context_factory, self.tls_server_context_factory,
reactor=self.get_reactor(), reactor=self.get_reactor(),
) )
logger.info("Synapse now listening on TCP port %d (TLS)", port)
else: else:
return listen_tcp( ports = listen_tcp(
bind_addresses, bind_addresses,
port, port,
SynapseSite( SynapseSite(
@ -148,6 +149,9 @@ class SynapseHomeServer(HomeServer):
), ),
reactor=self.get_reactor(), reactor=self.get_reactor(),
) )
logger.info("Synapse now listening on TCP port %d", port)
return ports
def _configure_named_resource(self, name, compress=False): def _configure_named_resource(self, name, compress=False):
"""Build a resource map for a named resource """Build a resource map for a named resource
@ -262,14 +266,14 @@ class SynapseHomeServer(HomeServer):
) )
) )
elif listener["type"] == "replication": elif listener["type"] == "replication":
bind_addresses = listener["bind_addresses"] services = listen_tcp(
for address in bind_addresses: listener["bind_addresses"],
factory = ReplicationStreamProtocolFactory(self) listener["port"],
server_listener = reactor.listenTCP( ReplicationStreamProtocolFactory(self),
listener["port"], factory, interface=address
) )
for s in services:
reactor.addSystemEventTrigger( reactor.addSystemEventTrigger(
"before", "shutdown", server_listener.stopListening, "before", "shutdown", s.stopListening,
) )
elif listener["type"] == "metrics": elif listener["type"] == "metrics":
if not self.get_config().enable_metrics: if not self.get_config().enable_metrics:

View File

@ -151,6 +151,10 @@ class ServerConfig(Config):
# if we still have an empty list of addresses, use the default list # if we still have an empty list of addresses, use the default list
if not bind_addresses: if not bind_addresses:
if listener['type'] == 'metrics':
# the metrics listener doesn't support IPv6
bind_addresses.append('0.0.0.0')
else:
bind_addresses.extend(DEFAULT_BIND_ADDRESSES) bind_addresses.extend(DEFAULT_BIND_ADDRESSES)
self.listeners.append(listener) self.listeners.append(listener)