DNSResolver: DNS resolution can now happen synchronously
This commit is contained in:
parent
8f35d70482
commit
7bde4a6300
|
@ -4,6 +4,10 @@
|
||||||
import Network
|
import Network
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
enum DNSResolverError: Error {
|
||||||
|
case dnsResolutionFailed(hostnames: [String])
|
||||||
|
}
|
||||||
|
|
||||||
class DNSResolver {
|
class DNSResolver {
|
||||||
let endpoints: [Endpoint?]
|
let endpoints: [Endpoint?]
|
||||||
let dispatchGroup: DispatchGroup
|
let dispatchGroup: DispatchGroup
|
||||||
|
@ -32,10 +36,11 @@ class DNSResolver {
|
||||||
return resolvedEndpoints
|
return resolvedEndpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolve(completionHandler: @escaping ([Endpoint?]?) -> Void) {
|
func resolveSync() throws -> [Endpoint?] {
|
||||||
let endpoints = self.endpoints
|
let endpoints = self.endpoints
|
||||||
let dispatchGroup = self.dispatchGroup
|
let dispatchGroup = self.dispatchGroup
|
||||||
dispatchWorkItems = []
|
dispatchWorkItems = []
|
||||||
|
|
||||||
var resolvedEndpoints: [Endpoint?] = Array<Endpoint?>(repeating: nil, count: endpoints.count)
|
var resolvedEndpoints: [Endpoint?] = Array<Endpoint?>(repeating: nil, count: endpoints.count)
|
||||||
var isResolvedByDNSRequest: [Bool] = Array<Bool>(repeating: false, count: endpoints.count)
|
var isResolvedByDNSRequest: [Bool] = Array<Bool>(repeating: false, count: endpoints.count)
|
||||||
for (i, endpoint) in self.endpoints.enumerated() {
|
for (i, endpoint) in self.endpoints.enumerated() {
|
||||||
|
@ -54,26 +59,26 @@ class DNSResolver {
|
||||||
DispatchQueue.global(qos: .userInitiated).async(group: dispatchGroup, execute: workItem)
|
DispatchQueue.global(qos: .userInitiated).async(group: dispatchGroup, execute: workItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dispatchGroup.notify(queue: .main) {
|
|
||||||
|
dispatchGroup.wait() // TODO: Timeout?
|
||||||
|
|
||||||
|
var hostnamesWithDnsResolutionFailure: [String] = []
|
||||||
assert(endpoints.count == resolvedEndpoints.count)
|
assert(endpoints.count == resolvedEndpoints.count)
|
||||||
for (i, endpoint) in endpoints.enumerated() {
|
for tuple in zip(endpoints, resolvedEndpoints) {
|
||||||
guard let endpoint = endpoint, let resolvedEndpoint = resolvedEndpoints[i] else {
|
let endpoint = tuple.0
|
||||||
completionHandler(nil)
|
let resolvedEndpoint = tuple.1
|
||||||
return
|
if let endpoint = endpoint {
|
||||||
}
|
if (resolvedEndpoint == nil) {
|
||||||
if (isResolvedByDNSRequest[i]) {
|
// DNS resolution failed
|
||||||
DNSResolver.cache.setObject(resolvedEndpoint.stringRepresentation() as NSString,
|
guard let hostname = endpoint.hostname() else { fatalError() }
|
||||||
forKey: endpoint.stringRepresentation() as NSString)
|
hostnamesWithDnsResolutionFailure.append(hostname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let numberOfEndpointsToResolve = endpoints.compactMap { $0 }.count
|
|
||||||
let numberOfResolvedEndpoints = resolvedEndpoints.compactMap { $0 }.count
|
|
||||||
if (numberOfResolvedEndpoints < numberOfEndpointsToResolve) {
|
|
||||||
completionHandler(nil)
|
|
||||||
} else {
|
|
||||||
completionHandler(resolvedEndpoints)
|
|
||||||
}
|
}
|
||||||
|
if (!hostnamesWithDnsResolutionFailure.isEmpty) {
|
||||||
|
throw DNSResolverError.dnsResolutionFailed(hostnames: hostnamesWithDnsResolutionFailure)
|
||||||
}
|
}
|
||||||
|
return resolvedEndpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
func cancel() {
|
func cancel() {
|
||||||
|
|
Loading…
Reference in New Issue