From 40a3794de31c55c6b64bdfac33d3137f3ca69aec Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Wed, 21 Jul 2021 10:22:18 +0100 Subject: [PATCH] GetaddrinfoResolver: don't block EventLoops (#1887) Motivation: GetaddrinfoResolver is blocking the calling thread for anything that uses a `ClientBootstrap`. Often, `ClientBootstrap`s are used on EventLoops which obviously creates a problem because you mustn't block EventLoops. Modifications: - Offload the `getaddrinfo` call to a `DispatchQueue`. - Keep the semantics: * only do one concurrent `getaddrinfo` call per SelectableEventLoop * prevent thread explosion on `SelectableEventLoop` Result: Less EL blocking. Co-authored-by: George Barnett Co-authored-by: Cory Benfield --- Sources/NIO/GetaddrinfoResolver.swift | 40 +++++++++++++++++++++------ Sources/NIO/SocketAddresses.swift | 2 ++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Sources/NIO/GetaddrinfoResolver.swift b/Sources/NIO/GetaddrinfoResolver.swift index 90ab81116..21cc16120 100644 --- a/Sources/NIO/GetaddrinfoResolver.swift +++ b/Sources/NIO/GetaddrinfoResolver.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftNIO open source project // -// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors +// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -16,11 +16,15 @@ /// /// This is the lowest-common-denominator resolver available to NIO. It's not really a very good /// solution because the `getaddrinfo` call blocks during the DNS resolution, meaning that this resolver -/// will block an event loop thread for as long as it takes to perform the getaddrinfo call. However, it -/// does have the advantage of automatically conforming to RFC 6724, which removes some of the work +/// will block a thread for as long as it takes to perform the getaddrinfo call. To prevent it from blocking `EventLoop` +/// threads, it will offload the blocking `getaddrinfo` calls to a `DispatchQueue`. +/// One advantage from leveraging `getaddrinfo` is the automatic conformance to RFC 6724, which removes some of the work /// needed to implement it. /// /// This resolver is a single-use object: it can only be used to perform a single host resolution. + +import Dispatch + #if os(Linux) || os(FreeBSD) || os(Android) import CNIOLinux #endif @@ -39,6 +43,9 @@ import struct WinSDK.SOCKADDR_IN import struct WinSDK.SOCKADDR_IN6 #endif +// A thread-specific variable where we store the offload queue if we're on an `SelectableEventLoop`. +let offloadQueueTSV = ThreadSpecificVariable() + internal class GetaddrinfoResolver: Resolver { private let v4Future: EventLoopPromise<[SocketAddress]> @@ -83,10 +90,27 @@ internal class GetaddrinfoResolver: Resolver { /// - port: The port we'll be connecting to. /// - returns: An `EventLoopFuture` that fires with the result of the lookup. func initiateAAAAQuery(host: String, port: Int) -> EventLoopFuture<[SocketAddress]> { - resolve(host: host, port: port) + self.offloadQueue().async { + self.resolveBlocking(host: host, port: port) + } return v6Future.futureResult } + private func offloadQueue() -> DispatchQueue { + if let offloadQueue = offloadQueueTSV.currentValue { + return offloadQueue + } else { + if MultiThreadedEventLoopGroup.currentEventLoop != nil { + // Okay, we're on an SelectableEL thread. Let's stuff our queue into the thread local. + let offloadQueue = DispatchQueue(label: "io.swiftnio.GetaddrinfoResolver.offloadQueue") + offloadQueueTSV.currentValue = offloadQueue + return offloadQueue + } else { + return DispatchQueue.global() + } + } + } + /// Cancel all outstanding DNS queries. /// /// This method is called whenever queries that have not completed no longer have their @@ -101,7 +125,7 @@ internal class GetaddrinfoResolver: Resolver { /// - parameters: /// - host: The hostname to do the DNS queries on. /// - port: The port we'll be connecting to. - private func resolve(host: String, port: Int) { + private func resolveBlocking(host: String, port: Int) { #if os(Windows) host.withCString(encodedAs: UTF16.self) { wszHost in String(port).withCString(encodedAs: UTF16.self) { wszPort in @@ -118,7 +142,7 @@ internal class GetaddrinfoResolver: Resolver { } if let pResult = pResult { - parseResults(pResult, host: host) + self.parseAndPublishResults(pResult, host: host) FreeAddrInfoW(pResult) } else { self.fail(SocketAddressError.unsupported) @@ -137,7 +161,7 @@ internal class GetaddrinfoResolver: Resolver { } if let info = info { - parseResults(info, host: host) + self.parseAndPublishResults(info, host: host) freeaddrinfo(info) } else { /* this is odd, getaddrinfo returned NULL */ @@ -157,7 +181,7 @@ internal class GetaddrinfoResolver: Resolver { internal typealias CAddrInfo = addrinfo #endif - private func parseResults(_ info: UnsafeMutablePointer, host: String) { + private func parseAndPublishResults(_ info: UnsafeMutablePointer, host: String) { var v4Results: [SocketAddress] = [] var v6Results: [SocketAddress] = [] diff --git a/Sources/NIO/SocketAddresses.swift b/Sources/NIO/SocketAddresses.swift index 30851efc5..61f0accd2 100644 --- a/Sources/NIO/SocketAddresses.swift +++ b/Sources/NIO/SocketAddresses.swift @@ -365,6 +365,8 @@ public enum SocketAddress: CustomStringConvertible { /// Creates a new `SocketAddress` for the given host (which will be resolved) and port. /// + /// - warning: This is a blocking call, so please avoid calling this from an `EventLoop`. + /// /// - parameters: /// - host: the hostname which should be resolved. /// - port: the port itself