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. 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 08199a5e8df9..71d1aec11158 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 @@ -70,6 +71,7 @@ def start_worker_reactor(appname, config): daemonize=config.worker_daemonize, cpu_affinity=config.worker_cpu_affinity, print_pidfile=config.print_pidfile, + use_getaddrinfo_for_dns=config.use_getaddrinfo_for_dns, logger=logger, ) @@ -82,6 +84,7 @@ def start_reactor( daemonize, cpu_affinity, print_pidfile, + use_getaddrinfo_for_dns, logger, ): """ Run the reactor in the main process @@ -96,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 + 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 """ @@ -127,6 +131,10 @@ def run(): change_resource_limit(soft_file_limit) if gc_thresholds: gc.set_threshold(*gc_thresholds) + + if use_getaddrinfo_for_dns: + reactor.installResolver(twisted.names.client) + reactor.run() if daemonize: diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index 1045d28949e2..667deecd8392 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, + 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 7874cd9da71c..dad4d3714471 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -56,6 +56,12 @@ def read_config(self, config): self.public_baseurl = config.get("public_baseurl") self.cpu_affinity = config.get("cpu_affinity") + # 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 # "disable" federation