From 3354a08e7c3ae4635f77d36f531e27b5f72e549e Mon Sep 17 00:00:00 2001 From: Benjamin Lee Date: Fri, 12 Apr 2019 15:48:41 -0700 Subject: [PATCH 1/4] Replaced GAIResolver with twisted.names.client for the default resolver --- synapse/app/_base.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/synapse/app/_base.py b/synapse/app/_base.py index 08199a5e8df9..2bf9945c4b6d 100644 --- a/synapse/app/_base.py +++ b/synapse/app/_base.py @@ -22,6 +22,7 @@ import psutil from daemonize import Daemonize +import twisted.names from twisted.internet import defer, error, reactor from twisted.protocols.tls import TLSMemoryBIOFactory @@ -127,6 +128,10 @@ def run(): change_resource_limit(soft_file_limit) if gc_thresholds: gc.set_threshold(*gc_thresholds) + + # change the default resolver to avoid blocking getaddrinfo calls + reactor.installResolver(twisted.names.client) + reactor.run() if daemonize: From c138c7a9cfef2e3b3985a98c9eda1f9ec2cf1a1a Mon Sep 17 00:00:00 2001 From: Benjamin Lee Date: Fri, 12 Apr 2019 16:05:31 -0700 Subject: [PATCH 2/4] Changelog entry --- changelog.d/5053.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/5053.misc diff --git a/changelog.d/5053.misc b/changelog.d/5053.misc new file mode 100644 index 000000000000..067975ce25e1 --- /dev/null +++ b/changelog.d/5053.misc @@ -0,0 +1 @@ +Replace blocking getaddrinfo hostname resolver with the non-blocking resolver from twisted.names.client. From f4a6ab2cc4f6783ad3b8e1632d6ad5cd37ced097 Mon Sep 17 00:00:00 2001 From: Benjamin Lee Date: Tue, 16 Apr 2019 14:39:22 -0700 Subject: [PATCH 3/4] Enable/disable getaddrinfo depending on a config option --- synapse/app/_base.py | 7 +++++-- synapse/app/homeserver.py | 1 + synapse/config/server.py | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/synapse/app/_base.py b/synapse/app/_base.py index 2bf9945c4b6d..8eea395ed9e7 100644 --- a/synapse/app/_base.py +++ b/synapse/app/_base.py @@ -71,6 +71,7 @@ def start_worker_reactor(appname, config): daemonize=config.worker_daemonize, cpu_affinity=config.worker_cpu_affinity, print_pidfile=config.print_pidfile, + no_getaddrinfo=config.no_getaddrinfo, logger=logger, ) @@ -83,6 +84,7 @@ def start_reactor( daemonize, cpu_affinity, print_pidfile, + no_getaddrinfo, logger, ): """ Run the reactor in the main process @@ -97,6 +99,7 @@ def start_reactor( pid_file (str): name of pid file to write to if daemonize is True daemonize (bool): true to run the reactor in a background process cpu_affinity (int|None): cpu affinity mask + no_getaddrinfo (bool): use async hostname resolver instead of getaddrinfo print_pidfile (bool): whether to print the pid file, if daemonize is True logger (logging.Logger): logger instance to pass to Daemonize """ @@ -129,8 +132,8 @@ def run(): if gc_thresholds: gc.set_threshold(*gc_thresholds) - # change the default resolver to avoid blocking getaddrinfo calls - reactor.installResolver(twisted.names.client) + if no_getaddrinfo: + reactor.installResolver(twisted.names.client) reactor.run() diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index 1045d28949e2..ec9bcff01542 100755 --- a/synapse/app/homeserver.py +++ b/synapse/app/homeserver.py @@ -647,6 +647,7 @@ def start_generate_monthly_active_users(): daemonize=hs.config.daemonize, cpu_affinity=hs.config.cpu_affinity, print_pidfile=hs.config.print_pidfile, + no_getaddrinfo=hs.config.no_getaddrinfo, logger=logger, ) diff --git a/synapse/config/server.py b/synapse/config/server.py index 7874cd9da71c..81a83cfe1c5d 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -56,6 +56,10 @@ def read_config(self, config): self.public_baseurl = config.get("public_baseurl") self.cpu_affinity = config.get("cpu_affinity") + # Whether to use the asynchronous DNS resolver instead of getaddrinfo. + # This can potentially improve performance. + self.no_getaddrinfo = config.get("no_getaddrinfo", False) + # Whether to send federation traffic out in this process. This only # applies to some federation traffic, and so shouldn't be used to # "disable" federation From c0aff077c3b6ce901d00694d096703d117c666ec Mon Sep 17 00:00:00 2001 From: Benjamin Lee Date: Mon, 13 May 2019 16:36:08 -0700 Subject: [PATCH 4/4] renamed no_getaddrinfo to use_getaddrinfo_for_dns and added to example config --- docs/sample_config.yaml | 6 ++++++ synapse/app/_base.py | 8 ++++---- synapse/app/homeserver.py | 2 +- synapse/config/server.py | 8 +++++--- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/docs/sample_config.yaml b/docs/sample_config.yaml index c4e5c4cf3993..21478fa50256 100644 --- a/docs/sample_config.yaml +++ b/docs/sample_config.yaml @@ -46,6 +46,12 @@ pid_file: DATADIR/homeserver.pid # #cpu_affinity: 0xFFFFFFFF +# Whether to use the default system resolver (getaddrinfo) instead of the twisted +# asynchronous dns resolver. Using the async resolver can potentially improve +# performance, but may also behave different from getaddrinfo. +# Defaults to True. Uncomment to use the twisted resolver instead. +#use_getaddrinfo_for_dns: False + # The path to the web client which will be served at /_matrix/client/ # if 'webclient' is configured under the 'listeners' configuration. # diff --git a/synapse/app/_base.py b/synapse/app/_base.py index 8eea395ed9e7..71d1aec11158 100644 --- a/synapse/app/_base.py +++ b/synapse/app/_base.py @@ -71,7 +71,7 @@ def start_worker_reactor(appname, config): daemonize=config.worker_daemonize, cpu_affinity=config.worker_cpu_affinity, print_pidfile=config.print_pidfile, - no_getaddrinfo=config.no_getaddrinfo, + use_getaddrinfo_for_dns=config.use_getaddrinfo_for_dns, logger=logger, ) @@ -84,7 +84,7 @@ def start_reactor( daemonize, cpu_affinity, print_pidfile, - no_getaddrinfo, + use_getaddrinfo_for_dns, logger, ): """ Run the reactor in the main process @@ -99,7 +99,7 @@ def start_reactor( pid_file (str): name of pid file to write to if daemonize is True daemonize (bool): true to run the reactor in a background process cpu_affinity (int|None): cpu affinity mask - no_getaddrinfo (bool): use async hostname resolver instead of getaddrinfo + use_getaddrinfo_for_dns (bool): use getaddrinfo instead of async resolver print_pidfile (bool): whether to print the pid file, if daemonize is True logger (logging.Logger): logger instance to pass to Daemonize """ @@ -132,7 +132,7 @@ def run(): if gc_thresholds: gc.set_threshold(*gc_thresholds) - if no_getaddrinfo: + if use_getaddrinfo_for_dns: reactor.installResolver(twisted.names.client) reactor.run() diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index ec9bcff01542..667deecd8392 100755 --- a/synapse/app/homeserver.py +++ b/synapse/app/homeserver.py @@ -647,7 +647,7 @@ def start_generate_monthly_active_users(): daemonize=hs.config.daemonize, cpu_affinity=hs.config.cpu_affinity, print_pidfile=hs.config.print_pidfile, - no_getaddrinfo=hs.config.no_getaddrinfo, + use_getaddrinfo_for_dns=hs.config.use_getaddrinfo_for_dns, logger=logger, ) diff --git a/synapse/config/server.py b/synapse/config/server.py index 81a83cfe1c5d..dad4d3714471 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -56,9 +56,11 @@ def read_config(self, config): self.public_baseurl = config.get("public_baseurl") self.cpu_affinity = config.get("cpu_affinity") - # Whether to use the asynchronous DNS resolver instead of getaddrinfo. - # This can potentially improve performance. - self.no_getaddrinfo = config.get("no_getaddrinfo", False) + # Whether to use the getaddrinfo instead of the asynchronous DNS + # resolver. Disabling this can potentially improve performance. + self.use_getaddrinfo_for_dns = config.get( + "use_getaddrinfo_for_dns", True + ) # Whether to send federation traffic out in this process. This only # applies to some federation traffic, and so shouldn't be used to