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

Commit

Permalink
Deduplicate some code in synapse.app (#4567)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkowl authored Feb 8, 2019
1 parent d008330 commit 9cd33d2
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 158 deletions.
1 change: 1 addition & 0 deletions changelog.d/4567.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduce duplication of ``synapse.app`` code.
63 changes: 63 additions & 0 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,35 @@

import gc
import logging
import signal
import sys
import traceback

import psutil
from daemonize import Daemonize

from twisted.internet import error, reactor

from synapse.app import check_bind_error
from synapse.crypto import context_factory
from synapse.util import PreserveLoggingContext
from synapse.util.rlimit import change_resource_limit

logger = logging.getLogger(__name__)

_sighup_callbacks = []


def register_sighup(func):
"""
Register a function to be called when a SIGHUP occurs.
Args:
func (function): Function to be called when sent a SIGHUP signal.
Will be called with a single argument, the homeserver.
"""
_sighup_callbacks.append(func)


def start_worker_reactor(appname, config):
""" Run the reactor in the main process
Expand Down Expand Up @@ -189,3 +205,50 @@ def listen_ssl(

logger.info("Synapse now listening on port %d (TLS)", port)
return r


def refresh_certificate(hs):
"""
Refresh the TLS certificates that Synapse is using by re-reading them from
disk and updating the TLS context factories to use them.
"""
logging.info("Loading certificate from disk...")
hs.config.read_certificate_from_disk()
hs.tls_server_context_factory = context_factory.ServerContextFactory(hs.config)
hs.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
hs.config
)
logging.info("Certificate loaded.")


def start(hs, listeners=None):
"""
Start a Synapse server or worker.
Args:
hs (synapse.server.HomeServer)
listeners (list[dict]): Listener configuration ('listeners' in homeserver.yaml)
"""
try:
# Set up the SIGHUP machinery.
if hasattr(signal, "SIGHUP"):
def handle_sighup(*args, **kwargs):
for i in _sighup_callbacks:
i(hs)

signal.signal(signal.SIGHUP, handle_sighup)

register_sighup(refresh_certificate)

# Load the certificate from disk.
refresh_certificate(hs)

# It is now safe to start your Synapse.
hs.start_listening(listeners)
hs.get_datastore().start_profiling()
except Exception:
traceback.print_exc(file=sys.stderr)
reactor = hs.get_reactor()
if reactor.running:
reactor.stop()
sys.exit(1)
7 changes: 1 addition & 6 deletions synapse/app/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,7 @@ def start(config_options):
)

ps.setup()
ps.start_listening(config.worker_listeners)

def start():
ps.get_datastore().start_profiling()

reactor.callWhenRunning(start)
reactor.callWhenRunning(_base.start, ps, config.worker_listeners)

_base.start_worker_reactor("synapse-appservice", config)

Expand Down
13 changes: 1 addition & 12 deletions synapse/app/client_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from synapse.config._base import ConfigError
from synapse.config.homeserver import HomeServerConfig
from synapse.config.logger import setup_logging
from synapse.crypto import context_factory
from synapse.http.server import JsonResource
from synapse.http.site import SynapseSite
from synapse.metrics import RegistryProxy
Expand Down Expand Up @@ -173,17 +172,7 @@ def start(config_options):
)

ss.setup()

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)

_base.start_worker_reactor("synapse-client-reader", config)

Expand Down
13 changes: 1 addition & 12 deletions synapse/app/event_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from synapse.config._base import ConfigError
from synapse.config.homeserver import HomeServerConfig
from synapse.config.logger import setup_logging
from synapse.crypto import context_factory
from synapse.http.server import JsonResource
from synapse.http.site import SynapseSite
from synapse.metrics import RegistryProxy
Expand Down Expand Up @@ -194,17 +193,7 @@ def start(config_options):
)

ss.setup()

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)

_base.start_worker_reactor("synapse-event-creator", config)

Expand Down
13 changes: 1 addition & 12 deletions synapse/app/federation_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from synapse.config._base import ConfigError
from synapse.config.homeserver import HomeServerConfig
from synapse.config.logger import setup_logging
from synapse.crypto import context_factory
from synapse.federation.transport.server import TransportLayerServer
from synapse.http.site import SynapseSite
from synapse.metrics import RegistryProxy
Expand Down Expand Up @@ -160,17 +159,7 @@ def start(config_options):
)

ss.setup()

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)

_base.start_worker_reactor("synapse-federation-reader", config)

Expand Down
12 changes: 1 addition & 11 deletions synapse/app/federation_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from synapse.config._base import ConfigError
from synapse.config.homeserver import HomeServerConfig
from synapse.config.logger import setup_logging
from synapse.crypto import context_factory
from synapse.federation import send_queue
from synapse.http.site import SynapseSite
from synapse.metrics import RegistryProxy
Expand Down Expand Up @@ -192,17 +191,8 @@ def start(config_options):
)

ss.setup()
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
_base.start_worker_reactor("synapse-federation-sender", config)


Expand Down
13 changes: 1 addition & 12 deletions synapse/app/frontend_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from synapse.config._base import ConfigError
from synapse.config.homeserver import HomeServerConfig
from synapse.config.logger import setup_logging
from synapse.crypto import context_factory
from synapse.http.server import JsonResource
from synapse.http.servlet import RestServlet, parse_json_object_from_request
from synapse.http.site import SynapseSite
Expand Down Expand Up @@ -250,17 +249,7 @@ def start(config_options):
)

ss.setup()

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)

_base.start_worker_reactor("synapse-frontend-proxy", config)

Expand Down
54 changes: 4 additions & 50 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import gc
import logging
import os
import signal
import sys
import traceback

Expand All @@ -28,7 +27,6 @@

from twisted.application import service
from twisted.internet import defer, reactor
from twisted.protocols.tls import TLSMemoryBIOFactory
from twisted.web.resource import EncodingResourceWrapper, NoResource
from twisted.web.server import GzipEncoderFactory
from twisted.web.static import File
Expand All @@ -49,7 +47,6 @@
from synapse.app._base import listen_ssl, listen_tcp, quit_with_error
from synapse.config._base import ConfigError
from synapse.config.homeserver import HomeServerConfig
from synapse.crypto import context_factory
from synapse.federation.transport.server import TransportLayerServer
from synapse.http.additional_resource import AdditionalResource
from synapse.http.server import RootRedirect
Expand Down Expand Up @@ -241,10 +238,10 @@ def _configure_named_resource(self, name, compress=False):

return resources

def start_listening(self):
def start_listening(self, listeners):
config = self.get_config()

for listener in config.listeners:
for listener in listeners:
if listener["type"] == "http":
self._listening_services.extend(
self._listener_http(config, listener)
Expand Down Expand Up @@ -328,20 +325,11 @@ def setup(config_options):
# generating config files and shouldn't try to continue.
sys.exit(0)

sighup_callbacks = []
synapse.config.logger.setup_logging(
config,
use_worker_options=False,
register_sighup=sighup_callbacks.append
use_worker_options=False
)

def handle_sighup(*args, **kwargs):
for i in sighup_callbacks:
i(*args, **kwargs)

if hasattr(signal, "SIGHUP"):
signal.signal(signal.SIGHUP, handle_sighup)

events.USE_FROZEN_DICTS = config.use_frozen_dicts

database_engine = create_engine(config.database_config)
Expand Down Expand Up @@ -377,31 +365,6 @@ def handle_sighup(*args, **kwargs):

hs.setup()

def refresh_certificate(*args):
"""
Refresh the TLS certificates that Synapse is using by re-reading them
from disk and updating the TLS context factories to use them.
"""
logging.info("Reloading certificate from disk...")
hs.config.read_certificate_from_disk()
hs.tls_server_context_factory = context_factory.ServerContextFactory(config)
hs.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
logging.info("Certificate reloaded.")

logging.info("Updating context factories...")
for i in hs._listening_services:
if isinstance(i.factory, TLSMemoryBIOFactory):
i.factory = TLSMemoryBIOFactory(
hs.tls_server_context_factory,
False,
i.factory.wrappedFactory
)
logging.info("Context factories updated.")

sighup_callbacks.append(refresh_certificate)

@defer.inlineCallbacks
def start():
try:
Expand All @@ -425,18 +388,9 @@ def start():
):
yield acme.provision_certificate()

# Read the certificate from disk and build the context factories for
# TLS.
hs.config.read_certificate_from_disk()
hs.tls_server_context_factory = context_factory.ServerContextFactory(config)
hs.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
_base.start(hs, config.listeners)

# It is now safe to start your Synapse.
hs.start_listening()
hs.get_pusherpool().start()
hs.get_datastore().start_profiling()
hs.get_datastore().start_doing_background_updates()
except Exception as e:
# If a DeferredList failed (like in listening on the ACME listener),
Expand Down
13 changes: 1 addition & 12 deletions synapse/app/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from synapse.config._base import ConfigError
from synapse.config.homeserver import HomeServerConfig
from synapse.config.logger import setup_logging
from synapse.crypto import context_factory
from synapse.http.site import SynapseSite
from synapse.metrics import RegistryProxy
from synapse.metrics.resource import METRICS_PREFIX, MetricsResource
Expand Down Expand Up @@ -160,17 +159,7 @@ def start(config_options):
)

ss.setup()

def start():
ss.config.read_certificate_from_disk()
ss.tls_server_context_factory = context_factory.ServerContextFactory(config)
ss.tls_client_options_factory = context_factory.ClientTLSOptionsFactory(
config
)
ss.start_listening(config.worker_listeners)
ss.get_datastore().start_profiling()

reactor.callWhenRunning(start)
reactor.callWhenRunning(_base.start, ss, config.worker_listeners)

_base.start_worker_reactor("synapse-media-repository", config)

Expand Down
3 changes: 1 addition & 2 deletions synapse/app/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,10 @@ def start(config_options):
)

ps.setup()
ps.start_listening(config.worker_listeners)

def start():
_base.start(ps, config.worker_listeners)
ps.get_pusherpool().start()
ps.get_datastore().start_profiling()

reactor.callWhenRunning(start)

Expand Down
Loading

0 comments on commit 9cd33d2

Please sign in to comment.