DNSResolver: Simplify

This commit is contained in:
Roopesh Chander 2018-11-08 16:40:38 +05:30
parent 40ce2604ea
commit df7197473e
1 changed files with 6 additions and 28 deletions

View File

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