This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Move DNS lookups into separate thread pool #11177
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2ab0e5
Move DNS lookups into separate thread pool
erikjohnston 2b3a8ce
Newsfile
erikjohnston e6e7819
getThreadPool expects a function
erikjohnston 6f08f13
Start threadpool
erikjohnston 9fd961d
Update synapse/util/gai_resolver.py
erikjohnston 9659440
Update changelog.d/11177.bugfix
erikjohnston d502181
Move to top
erikjohnston 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 @@ | ||
| Fix a performance regression introduced in v1.44.0 which could cause client requests to time out when making large numbers of outbound requests. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # This is a direct lift from | ||
| # https://github.com/twisted/twisted/blob/release-21.2.0-10091/src/twisted/internet/_resolver.py. | ||
| # We copy it here as we need to instantiate `GAIResolver` manually, but it is a | ||
| # private class. | ||
|
|
||
|
|
||
| from socket import ( | ||
| AF_INET, | ||
| AF_INET6, | ||
| AF_UNSPEC, | ||
| SOCK_DGRAM, | ||
| SOCK_STREAM, | ||
| gaierror, | ||
| getaddrinfo, | ||
| ) | ||
|
|
||
| from zope.interface import implementer | ||
|
|
||
| from twisted.internet.address import IPv4Address, IPv6Address | ||
| from twisted.internet.interfaces import IHostnameResolver, IHostResolution | ||
| from twisted.internet.threads import deferToThreadPool | ||
|
|
||
|
|
||
| @implementer(IHostResolution) | ||
| class HostResolution: | ||
| """ | ||
| The in-progress resolution of a given hostname. | ||
| """ | ||
|
|
||
| def __init__(self, name): | ||
| """ | ||
| Create a L{HostResolution} with the given name. | ||
| """ | ||
| self.name = name | ||
|
|
||
| def cancel(self): | ||
| # IHostResolution.cancel | ||
| raise NotImplementedError() | ||
|
|
||
|
|
||
| _any = frozenset([IPv4Address, IPv6Address]) | ||
|
|
||
| _typesToAF = { | ||
| frozenset([IPv4Address]): AF_INET, | ||
| frozenset([IPv6Address]): AF_INET6, | ||
| _any: AF_UNSPEC, | ||
| } | ||
|
|
||
| _afToType = { | ||
| AF_INET: IPv4Address, | ||
| AF_INET6: IPv6Address, | ||
| } | ||
|
|
||
| _transportToSocket = { | ||
| "TCP": SOCK_STREAM, | ||
| "UDP": SOCK_DGRAM, | ||
| } | ||
|
|
||
| _socktypeToType = { | ||
| SOCK_STREAM: "TCP", | ||
| SOCK_DGRAM: "UDP", | ||
| } | ||
|
|
||
|
|
||
| @implementer(IHostnameResolver) | ||
| class GAIResolver: | ||
| """ | ||
| L{IHostnameResolver} implementation that resolves hostnames by calling | ||
| L{getaddrinfo} in a thread. | ||
| """ | ||
|
|
||
| def __init__(self, reactor, getThreadPool=None, getaddrinfo=getaddrinfo): | ||
| """ | ||
| Create a L{GAIResolver}. | ||
| @param reactor: the reactor to schedule result-delivery on | ||
| @type reactor: L{IReactorThreads} | ||
| @param getThreadPool: a function to retrieve the thread pool to use for | ||
| scheduling name resolutions. If not supplied, the use the given | ||
| C{reactor}'s thread pool. | ||
| @type getThreadPool: 0-argument callable returning a | ||
| L{twisted.python.threadpool.ThreadPool} | ||
| @param getaddrinfo: a reference to the L{getaddrinfo} to use - mainly | ||
| parameterized for testing. | ||
| @type getaddrinfo: callable with the same signature as L{getaddrinfo} | ||
| """ | ||
| self._reactor = reactor | ||
| self._getThreadPool = ( | ||
| reactor.getThreadPool if getThreadPool is None else getThreadPool | ||
| ) | ||
| self._getaddrinfo = getaddrinfo | ||
|
|
||
| def resolveHostName( | ||
| self, | ||
| resolutionReceiver, | ||
| hostName, | ||
| portNumber=0, | ||
| addressTypes=None, | ||
| transportSemantics="TCP", | ||
| ): | ||
| """ | ||
| See L{IHostnameResolver.resolveHostName} | ||
| @param resolutionReceiver: see interface | ||
| @param hostName: see interface | ||
| @param portNumber: see interface | ||
| @param addressTypes: see interface | ||
| @param transportSemantics: see interface | ||
| @return: see interface | ||
| """ | ||
| pool = self._getThreadPool() | ||
| addressFamily = _typesToAF[ | ||
| _any if addressTypes is None else frozenset(addressTypes) | ||
| ] | ||
| socketType = _transportToSocket[transportSemantics] | ||
|
|
||
| def get(): | ||
| try: | ||
| return self._getaddrinfo( | ||
| hostName, portNumber, addressFamily, socketType | ||
| ) | ||
| except gaierror: | ||
| return [] | ||
|
|
||
| d = deferToThreadPool(self._reactor, pool, get) | ||
| resolution = HostResolution(hostName) | ||
| resolutionReceiver.resolutionBegan(resolution) | ||
|
|
||
| @d.addCallback | ||
| def deliverResults(result): | ||
| for family, socktype, _proto, _cannoname, sockaddr in result: | ||
| addrType = _afToType[family] | ||
| resolutionReceiver.addressResolved( | ||
| addrType(_socktypeToType.get(socktype, "TCP"), *sockaddr) | ||
| ) | ||
| resolutionReceiver.resolutionComplete() | ||
|
|
||
| return resolution | ||
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.
Uh oh!
There was an error while loading. Please reload this page.