@@ -9,18 +9,9 @@ enum DNSResolverError: Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class DNSResolver {
|
class DNSResolver {
|
||||||
let endpoints: [Endpoint?]
|
|
||||||
let dispatchGroup: DispatchGroup
|
|
||||||
var dispatchWorkItems: [DispatchWorkItem]
|
|
||||||
|
|
||||||
init(endpoints: [Endpoint?]) {
|
static func isAllEndpointsAlreadyResolved(endpoints: [Endpoint?]) -> Bool {
|
||||||
self.endpoints = endpoints
|
for endpoint in endpoints {
|
||||||
self.dispatchWorkItems = []
|
|
||||||
self.dispatchGroup = DispatchGroup()
|
|
||||||
}
|
|
||||||
|
|
||||||
func isAllEndpointsAlreadyResolved() -> Bool {
|
|
||||||
for endpoint in self.endpoints {
|
|
||||||
guard let endpoint = endpoint else { continue }
|
guard let endpoint = endpoint else { continue }
|
||||||
if (!endpoint.hasHostAsIPAddress()) {
|
if (!endpoint.hasHostAsIPAddress()) {
|
||||||
return false
|
return false
|
||||||
@@ -29,17 +20,15 @@ class DNSResolver {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveSync() throws -> [Endpoint?] {
|
static func resolveSync(endpoints: [Endpoint?]) throws -> [Endpoint?] {
|
||||||
let endpoints = self.endpoints
|
let dispatchGroup: DispatchGroup = DispatchGroup()
|
||||||
let dispatchGroup = self.dispatchGroup
|
|
||||||
dispatchWorkItems = []
|
|
||||||
|
|
||||||
if (isAllEndpointsAlreadyResolved()) {
|
if (isAllEndpointsAlreadyResolved(endpoints: endpoints)) {
|
||||||
return endpoints
|
return endpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
var resolvedEndpoints: [Endpoint?] = Array<Endpoint?>(repeating: nil, count: endpoints.count)
|
var resolvedEndpoints: [Endpoint?] = Array<Endpoint?>(repeating: nil, count: endpoints.count)
|
||||||
for (i, endpoint) in self.endpoints.enumerated() {
|
for (i, endpoint) in endpoints.enumerated() {
|
||||||
guard let endpoint = endpoint else { continue }
|
guard let endpoint = endpoint else { continue }
|
||||||
if (endpoint.hasHostAsIPAddress()) {
|
if (endpoint.hasHostAsIPAddress()) {
|
||||||
resolvedEndpoints[i] = endpoint
|
resolvedEndpoints[i] = endpoint
|
||||||
@@ -47,7 +36,6 @@ class DNSResolver {
|
|||||||
let workItem = DispatchWorkItem {
|
let workItem = DispatchWorkItem {
|
||||||
resolvedEndpoints[i] = DNSResolver.resolveSync(endpoint: endpoint)
|
resolvedEndpoints[i] = DNSResolver.resolveSync(endpoint: endpoint)
|
||||||
}
|
}
|
||||||
dispatchWorkItems.append(workItem)
|
|
||||||
DispatchQueue.global(qos: .userInitiated).async(group: dispatchGroup, execute: workItem)
|
DispatchQueue.global(qos: .userInitiated).async(group: dispatchGroup, execute: workItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,16 +60,6 @@ class DNSResolver {
|
|||||||
}
|
}
|
||||||
return resolvedEndpoints
|
return resolvedEndpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
func cancel() {
|
|
||||||
for workItem in dispatchWorkItems {
|
|
||||||
workItem.cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deinit {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DNSResolver {
|
extension DNSResolver {
|
||||||
|
|||||||
Reference in New Issue
Block a user