Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement non-blocking async DNS resolver #2060

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 240 additions & 0 deletions Sources/GRPCHTTP2Core/Client/Resolver/SimpleAsyncDNSResolver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/*
* Copyright 2024, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Dispatch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add appropriate access levels to the imports? Should be possible for both of these to be private


#if canImport(Darwin)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
#if canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#endif
import CNIOLinux
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need CNIOLinux here

#else
#error("The GRPCHTTP2Core module was unable to identify your C library.")
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The simplest way to do these checks if to just use canImports:

#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#else
#error(...)
#endif


/// An asynchronous non-blocking DNS resolver built on top of the libc `getaddrinfo` function.
@available(macOS 10.15, *)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add the availability for all platforms. The easiest way to do this is to remove the @available, see where Xcode complains and then jump-to-definition on that symbol and copy its @available annotation.

package enum SimpleAsyncDNSResolver {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to include Simple or Async in the name, we can just call it DNSResolver

private static let dispatchQueue = DispatchQueue(
label: "io.grpc.SimpleAsyncDNSResolver.dispatchQueue"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to include dispatchQueue in the name: io.grpc.DNSResolver is fine

)

/// Resolves a hostname and port number to a list of IP addresses and port numbers.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Resolves a hostname and port number to a list of IP addresses and port numbers.
/// Resolves a hostname and port number to a list of socket addresses.

///
/// This method is non-blocking. As calls to `getaddrinfo` are blocking, this method executes `getaddrinfo` in a
/// `DispatchQueue` and uses a `CheckedContinuation` to interface with the execution.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't really need to include the implementation details here (DispatchQueue/CheckedContinuation)

package static func resolve(host: String, port: Int) async throws -> [SocketAddress] {
if Task.isCancelled {
return []
}

return try await withCheckedThrowingContinuation { continuation in
dispatchQueue.async {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use explicit self

Suggested change
dispatchQueue.async {
Self.dispatchQueue.async {

do {
let result = try Self.resolveBlocking(host: host, port: port)
continuation.resume(returning: result)
} catch {
continuation.resume(throwing: error)
}
}
}
}

/// Resolves a hostname and port number to a list of IP addresses and port numbers.
///
/// Calls to `getaddrinfo` are blocking and this method calls `getaddrinfo` directly. Hence, this method is also blocking.
private static func resolveBlocking(host: String, port: Int) throws -> [SocketAddress] {
var result: UnsafeMutablePointer<addrinfo>?
defer {
if let result {
// Release memory allocated by a successful call to getaddrinfo
freeaddrinfo(result)
}
}

var hints = addrinfo()
hints.ai_socktype = SOCK_STREAM
hints.ai_protocol = IPPROTO_TCP

let errorCode = getaddrinfo(host, String(port), &hints, &result)

guard errorCode == 0, var result else {
throw SimpleAsyncDNSResolverError(code: errorCode)
}

var socketAddressList = [SocketAddress]()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: socketAddressList -> socketAddresses

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to make it more distinct from another identifier socketAddress 😄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move all of the parsing of the result into a separate function?


while true {
let addressBytes = UnsafeRawPointer(result.pointee.ai_addr)
let socketAddress: SocketAddress?

switch result.pointee.ai_family { // Enum with two cases
case AF_INET: // IPv4 address
let ipv4NetworkAddressStructure = addressBytes!.load(as: sockaddr_in.self)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it safe to ! this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressBytes is nil only when result.pointee.ai_addr is nil. However, I can see that force-unwrapping is unnecessary as a more suitable init for UnsafeRawPointer can be used instead.

let ipv4PresentationAddress = Self.convertFromNetworkToPresentationFormat(
address: ipv4NetworkAddressStructure.sin_addr,
family: AF_INET,
length: INET_ADDRSTRLEN
)

socketAddress = .ipv4(
.init(
host: ipv4PresentationAddress,
port: Int(in_port_t(bigEndian: ipv4NetworkAddressStructure.sin_port))
)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than doing the conversion here, can we move it to an extension on the IPv4 address type? Same for the IPv6 address, e.g.

extension SocketAddress.IPv4 {
  init(_ address: sockaddr_in) { 
    // ...
  }
}

case AF_INET6: // IPv6 address
let ipv6NetworkAddressStructure = addressBytes!.load(as: sockaddr_in6.self)
let ipv6PresentationAddress = Self.convertFromNetworkToPresentationFormat(
address: ipv6NetworkAddressStructure.sin6_addr,
family: AF_INET6,
length: INET6_ADDRSTRLEN
)

socketAddress = .ipv6(
.init(
host: ipv6PresentationAddress,
port: Int(in_port_t(bigEndian: ipv6NetworkAddressStructure.sin6_port))
)
)
default:
socketAddress = nil
}

if let socketAddress {
socketAddressList.append(socketAddress)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just be append the address in each case


guard let nextResult = result.pointee.ai_next else { break }
result = nextResult
}

return socketAddressList
}

/// Converts an address from a network format to a presentation format using `inet_ntop`.
private static func convertFromNetworkToPresentationFormat<T>(
address: T,
family: Int32,
length: Int32
) -> String {
var resultingAddressBytes = [Int8](repeating: 0, count: Int(length))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be a bit clearer to call these presentationBytes (it's then more obvious what it is in relation to the function name)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're also passing this to a C function which is expected CChar (which is typealiased to Int8) – let's use CChar here because that's what the C function is expecting.


return withUnsafePointer(to: address) { addressPtr in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this function should be generic and then just get a pointer to whatever is passed in, it's a bit strange. Instead the caller should be responsible getting a pointer to the address structure and passing that into this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right

return resultingAddressBytes.withUnsafeMutableBufferPointer {
(resultingAddressBytesPtr: inout UnsafeMutableBufferPointer<Int8>) -> String in

// Convert
inet_ntop(family, addressPtr, resultingAddressBytesPtr.baseAddress!, socklen_t(length))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't ignore the return value from inet_ntop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, of course.


// Create the result string from now-filled resultingAddressBytes.
return resultingAddressBytesPtr.baseAddress!.withMemoryRebound(
to: UInt8.self,
capacity: Int(length)
) { resultingAddressBytesPtr -> String in
String(cString: resultingAddressBytesPtr)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do this without rebinding: String has an overload which accepts UnsafePointer<CChar>

}
}
}
}

/// `Error` that may be thrown based on the error code returned by `getaddrinfo`.
package enum SimpleAsyncDNSResolverError: Error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this Hashable as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we embed this within the DNSResolver type?

/// Address family for nodename not supported.
case addressFamilyForNodenameNotSupported

/// Temporary failure in name resolution.
case temporaryFailure

/// Invalid value for `ai_flags`.
case invalidAIFlags

/// Invalid value for `hints`.
case invalidHints

/// Non-recoverable failure in name resolution.
case nonRecoverableFailure

/// `ai_family` not supported.
case aiFamilyNotSupported

/// Memory allocation failure.
case memoryAllocationFailure

/// No address associated with nodename.
case noAddressAssociatedWithNodename

/// `hostname` or `servname` not provided, or not known.
case hostnameOrServnameNotProvidedOrNotKnown

/// Argument buffer overflow.
case argumentBufferOverflow

/// Resolved protocol is unknown.
case resolvedProtocolIsUnknown

/// `servname` not supported for `ai_socktype`.
case servnameNotSupportedForSocktype

/// `ai_socktype` not supported.
case socktypeNotSupported

/// System error returned in `errno`.
case systemError

/// Unknown error.
case unknown

package init(code: Int32) {
switch code {
case EAI_ADDRFAMILY:
self = .addressFamilyForNodenameNotSupported
case EAI_AGAIN:
self = .temporaryFailure
case EAI_BADFLAGS:
self = .invalidAIFlags
case EAI_BADHINTS:
self = .invalidHints
case EAI_FAIL:
self = .nonRecoverableFailure
case EAI_FAMILY:
self = .aiFamilyNotSupported
case EAI_MEMORY:
self = .memoryAllocationFailure
case EAI_NODATA:
self = .noAddressAssociatedWithNodename
case EAI_NONAME:
self = .hostnameOrServnameNotProvidedOrNotKnown
case EAI_OVERFLOW:
self = .argumentBufferOverflow
case EAI_PROTOCOL:
self = .resolvedProtocolIsUnknown
case EAI_SERVICE:
self = .servnameNotSupportedForSocktype
case EAI_SOCKTYPE:
self = .socktypeNotSupported
case EAI_SYSTEM:
self = .systemError
default:
self = .unknown
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2024, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import GRPCHTTP2Core
import XCTest

class SimpleAsyncDNSResolverTests: XCTestCase {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using XCTest can we use swift-testing instead? It'd be good to write new tests using this moving forwards.

There are some docs here and some examples here.

func testResolve() async throws {
let expectedResult: [SocketAddress] = [
.ipv6(host: "::1", port: 80),
.ipv4(host: "127.0.0.1", port: 80),
]

let result = try await SimpleAsyncDNSResolver.resolve(host: "localhost", port: 80)

XCTAssertEqual(result, expectedResult)
}
}
Loading