-
Notifications
You must be signed in to change notification settings - Fork 436
Implement non-blocking async DNS resolver #2060
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||
|
||||||
#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 | ||||||
|
||||||
#else | ||||||
#error("The GRPCHTTP2Core module was unable to identify your C library.") | ||||||
#endif | ||||||
|
||||||
|
||||||
/// An asynchronous non-blocking DNS resolver built on top of the libc `getaddrinfo` function. | ||||||
@available(macOS 10.15, *) | ||||||
|
||||||
package enum SimpleAsyncDNSResolver { | ||||||
|
||||||
private static let dispatchQueue = DispatchQueue( | ||||||
label: "io.grpc.SimpleAsyncDNSResolver.dispatchQueue" | ||||||
|
||||||
) | ||||||
|
||||||
/// 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 IP addresses and port numbers. | |
/// Resolves a hostname and port number to a list of socket addresses. |
Outdated
There was a problem hiding this comment.
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)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use explicit self
dispatchQueue.async { | |
Self.dispatchQueue.async { |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: socketAddressList
-> socketAddresses
There was a problem hiding this comment.
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
😄
Outdated
There was a problem hiding this comment.
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?
Outdated
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Outdated
There was a problem hiding this comment.
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) {
// ...
}
}
Outdated
There was a problem hiding this comment.
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
Outdated
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
Outdated
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, right
Outdated
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, of course.
Outdated
There was a problem hiding this comment.
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>
Outdated
There was a problem hiding this comment.
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?
Outdated
There was a problem hiding this comment.
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?
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 { | ||
|
||
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) | ||
} | ||
} |
There was a problem hiding this comment.
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