-
Notifications
You must be signed in to change notification settings - Fork 396
Split homeserver creation and setup #19015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
73ddfba
3521c0e
e651544
29457a2
b1f4478
211765f
2cf1f40
0967fdf
0f50d26
e4857c0
ad3d981
18b42fb
32e5f4b
16c7967
614307b
b2a82e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Split homeserver creation (`create_homeserver`) and setup (`setup`). | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,10 @@ def gz_wrap(r: Resource) -> Resource: | |
|
||
|
||
class SynapseHomeServer(HomeServer): | ||
""" | ||
Homeserver class for the main Synapse process. | ||
""" | ||
|
||
DATASTORE_CLASS = DataStore | ||
|
||
def _listener_http( | ||
|
@@ -345,23 +349,17 @@ def load_or_generate_config(argv_options: List[str]) -> HomeServerConfig: | |
return config | ||
|
||
|
||
def setup( | ||
def create_homeserver( | ||
config: HomeServerConfig, | ||
reactor: Optional[ISynapseReactor] = None, | ||
freeze: bool = True, | ||
) -> SynapseHomeServer: | ||
""" | ||
Create and setup a Synapse homeserver instance given a configuration. | ||
Create a homeserver instance for the Synapse main process. | ||
|
||
Args: | ||
config: The configuration for the homeserver. | ||
reactor: Optionally provide a reactor to use. Can be useful in different | ||
scenarios that you want control over the reactor, such as tests. | ||
freeze: whether to freeze the homeserver base objects in the garbage collector. | ||
May improve garbage collection performance by marking objects with an effectively | ||
static lifetime as frozen so they don't need to be considered for cleanup. | ||
If you ever want to `shutdown` the homeserver, this needs to be | ||
False otherwise the homeserver cannot be garbage collected after `shutdown`. | ||
|
||
Returns: | ||
A homeserver instance. | ||
|
@@ -372,7 +370,6 @@ def setup( | |
"You have specified `worker_app` in the config but are attempting to start a non-worker " | ||
"instance. Please use `python -m synapse.app.generic_worker` instead (or remove the option if this is the main process)." | ||
) | ||
sys.exit(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this exit as it's unreachable since we |
||
|
||
events.USE_FROZEN_DICTS = config.server.use_frozen_dicts | ||
synapse.util.caches.TRACK_MEMORY_USAGE = config.caches.track_memory_usage | ||
|
@@ -397,24 +394,50 @@ def setup( | |
) | ||
|
||
hs = SynapseHomeServer( | ||
config.server.server_name, | ||
hostname=config.server.server_name, | ||
config=config, | ||
reactor=reactor, | ||
) | ||
|
||
setup_logging(hs, config, use_worker_options=False) | ||
return hs | ||
|
||
# Start the tracer | ||
init_tracer(hs) # noqa | ||
|
||
def setup( | ||
hs: SynapseHomeServer, | ||
*, | ||
freeze: bool = True, | ||
) -> None: | ||
""" | ||
Setup a Synapse homeserver instance given a configuration. | ||
|
||
Args: | ||
hs: The homeserver to setup. | ||
freeze: whether to freeze the homeserver base objects in the garbage collector. | ||
May improve garbage collection performance by marking objects with an effectively | ||
static lifetime as frozen so they don't need to be considered for cleanup. | ||
If you ever want to `shutdown` the homeserver, this needs to be | ||
False otherwise the homeserver cannot be garbage collected after `shutdown`. | ||
|
||
Returns: | ||
A homeserver instance. | ||
""" | ||
|
||
setup_logging(hs, hs.config, use_worker_options=False) | ||
|
||
# Log after we've configured logging. | ||
logger.info("Setting up server") | ||
|
||
# Start the tracer | ||
init_tracer(hs) # noqa | ||
|
||
try: | ||
hs.setup() | ||
except Exception as e: | ||
handle_startup_exception(e) | ||
|
||
async def start() -> None: | ||
async def _start_when_reactor_running() -> None: | ||
# TODO: Feels like this should be moved somewhere else. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a future TODO |
||
# | ||
# Load the OIDC provider metadatas, if OIDC is enabled. | ||
if hs.config.oidc.oidc_enabled: | ||
oidc = hs.get_oidc_handler() | ||
|
@@ -423,21 +446,31 @@ async def start() -> None: | |
|
||
await _base.start(hs, freeze) | ||
|
||
# TODO: This should be moved to `SynapseHomeServer.start_background_tasks` (not | ||
# `HomeServer.start_background_tasks`) (this way it matches the behavior of only | ||
# running on `main`) | ||
hs.get_datastores().main.db_pool.updates.start_doing_background_updates() | ||
|
||
register_start(hs, start) | ||
# Register a callback to be invoked once the reactor is running | ||
register_start(hs, _start_when_reactor_running) | ||
|
||
return hs | ||
|
||
def start_reactor( | ||
config: HomeServerConfig, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To document why I made the change to pass in the
|
||
) -> None: | ||
""" | ||
Start the reactor (Twisted event-loop). | ||
|
||
def run(hs: HomeServer) -> None: | ||
Args: | ||
config: The configuration for the homeserver. | ||
""" | ||
_base.start_reactor( | ||
"synapse-homeserver", | ||
soft_file_limit=hs.config.server.soft_file_limit, | ||
gc_thresholds=hs.config.server.gc_thresholds, | ||
pid_file=hs.config.server.pid_file, | ||
daemonize=hs.config.server.daemonize, | ||
print_pidfile=hs.config.server.print_pidfile, | ||
soft_file_limit=config.server.soft_file_limit, | ||
gc_thresholds=config.server.gc_thresholds, | ||
pid_file=config.server.pid_file, | ||
daemonize=config.server.daemonize, | ||
print_pidfile=config.server.print_pidfile, | ||
logger=logger, | ||
) | ||
|
||
|
@@ -448,13 +481,14 @@ def main() -> None: | |
with LoggingContext(name="main", server_name=homeserver_config.server.server_name): | ||
# check base requirements | ||
check_requirements() | ||
hs = setup(homeserver_config) | ||
hs = create_homeserver(homeserver_config) | ||
setup(hs) | ||
|
||
# redirect stdio to the logs, if configured. | ||
if not hs.config.logging.no_redirect_stdio: | ||
redirect_stdio_to_logs() | ||
|
||
run(hs) | ||
start_reactor(homeserver_config) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
Uh oh!
There was an error while loading. Please reload this page.