-
Notifications
You must be signed in to change notification settings - Fork 394
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
Open
MadLittleMods
wants to merge
15
commits into
develop
Choose a base branch
from
maddlittlemods/split-hs-creation-and-setup
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
73ddfba
Split homeserver creation and setup
MadLittleMods 3521c0e
Move start related things to `run()`
MadLittleMods e651544
Rename `run` -> `start` to match start things inside
MadLittleMods 29457a2
It's better if we do everything in `setup`
MadLittleMods b1f4478
Add plans
MadLittleMods 211765f
Move db_pool `start_doing_background_updates` to `start_background_ta…
MadLittleMods 2cf1f40
Add docstring
MadLittleMods 0967fdf
Revert "Move db_pool `start_doing_background_updates` to `start_backg…
MadLittleMods 0f50d26
Add changelog
MadLittleMods e4857c0
Merge branch 'develop' into maddlittlemods/split-hs-creation-and-setup
MadLittleMods ad3d981
Point out what not to do
MadLittleMods 18b42fb
Refactor in tests
MadLittleMods 32e5f4b
Update docstrings
MadLittleMods 16c7967
No need to pass in the whole homeserver (just pass homeserver config)
MadLittleMods 614307b
Merge branch 'develop' into maddlittlemods/split-hs-creation-and-setup
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Split homeserver creation (`create_homeserver`) and setup (`setup`). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
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, | ||
) -> 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__": | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this exit as it's unreachable since we
raise
just above