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

Commit e84fe35

Browse files
committed
Merge pull request #105 from matrix-org/erikj-perf
Add a Twisted Service to synapse.app.homeserver
2 parents 019422e + c37ecee commit e84fe35

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

synapse/app/homeserver.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from synapse.python_dependencies import check_requirements
2727

2828
from twisted.internet import reactor
29+
from twisted.application import service
2930
from twisted.enterprise import adbapi
3031
from twisted.web.resource import Resource
3132
from twisted.web.static import File
@@ -295,10 +296,19 @@ def change_resource_limit(soft_file_no):
295296
logger.warn("Failed to set file limit: %s", e)
296297

297298

298-
def setup():
299+
def setup(config_options):
300+
"""
301+
Args:
302+
config_options_options: The options passed to Synapse. Usually
303+
`sys.argv[1:]`.
304+
should_run (bool): Whether to start the reactor.
305+
306+
Returns:
307+
HomeServer
308+
"""
299309
config = HomeServerConfig.load_config(
300310
"Synapse Homeserver",
301-
sys.argv[1:],
311+
config_options,
302312
generate_section="Homeserver"
303313
)
304314

@@ -370,34 +380,55 @@ def setup():
370380
hs.get_datastore().start_profiling()
371381
hs.get_replication_layer().start_get_pdu_cache()
372382

373-
if config.daemonize:
374-
print config.pid_file
383+
return hs
384+
385+
386+
class SynapseService(service.Service):
387+
"""A twisted Service class that will start synapse. Used to run synapse
388+
via twistd and a .tac.
389+
"""
390+
def __init__(self, config):
391+
self.config = config
392+
393+
def startService(self):
394+
hs = setup(self.config)
395+
change_resource_limit(hs.config.soft_file_limit)
396+
397+
def stopService(self):
398+
return self._port.stopListening()
399+
400+
401+
def run(hs):
402+
403+
def in_thread():
404+
with LoggingContext("run"):
405+
change_resource_limit(hs.config.soft_file_limit)
406+
407+
reactor.run()
408+
409+
if hs.config.daemonize:
410+
411+
print hs.config.pid_file
375412

376413
daemon = Daemonize(
377414
app="synapse-homeserver",
378-
pid=config.pid_file,
379-
action=lambda: run(config),
415+
pid=hs.config.pid_file,
416+
action=lambda: in_thread(),
380417
auto_close_fds=False,
381418
verbose=True,
382419
logger=logger,
383420
)
384421

385422
daemon.start()
386423
else:
387-
run(config)
388-
389-
390-
def run(config):
391-
with LoggingContext("run"):
392-
change_resource_limit(config.soft_file_limit)
393-
394-
reactor.run()
424+
in_thread(hs.config)
395425

396426

397427
def main():
398428
with LoggingContext("main"):
399429
check_requirements()
400-
setup()
430+
hs = setup(sys.argv[1:])
431+
run(hs)
401432

402433

403434
if __name__ == '__main__':

0 commit comments

Comments
 (0)