DNSResolver: Let's not cache DNS resolution results anymore
This commit is contained in:
parent
7bde4a6300
commit
40ce2604ea
|
@ -12,7 +12,6 @@ class DNSResolver {
|
||||||
let endpoints: [Endpoint?]
|
let endpoints: [Endpoint?]
|
||||||
let dispatchGroup: DispatchGroup
|
let dispatchGroup: DispatchGroup
|
||||||
var dispatchWorkItems: [DispatchWorkItem]
|
var dispatchWorkItems: [DispatchWorkItem]
|
||||||
static var cache = NSCache<NSString, NSString>()
|
|
||||||
|
|
||||||
init(endpoints: [Endpoint?]) {
|
init(endpoints: [Endpoint?]) {
|
||||||
self.endpoints = endpoints
|
self.endpoints = endpoints
|
||||||
|
@ -20,20 +19,14 @@ class DNSResolver {
|
||||||
self.dispatchGroup = DispatchGroup()
|
self.dispatchGroup = DispatchGroup()
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveWithoutNetworkRequests() -> [Endpoint?]? {
|
func isAllEndpointsAlreadyResolved() -> Bool {
|
||||||
var resolvedEndpoints: [Endpoint?] = Array<Endpoint?>(repeating: nil, count: endpoints.count)
|
for endpoint in self.endpoints {
|
||||||
for (i, endpoint) in self.endpoints.enumerated() {
|
|
||||||
guard let endpoint = endpoint else { continue }
|
guard let endpoint = endpoint else { continue }
|
||||||
if (endpoint.hasHostAsIPAddress()) {
|
if (!endpoint.hasHostAsIPAddress()) {
|
||||||
resolvedEndpoints[i] = endpoint
|
return false
|
||||||
} else if let resolvedEndpointStringInCache = DNSResolver.cache.object(forKey: endpoint.stringRepresentation() as NSString),
|
|
||||||
let resolvedEndpointInCache = Endpoint(from: resolvedEndpointStringInCache as String) {
|
|
||||||
resolvedEndpoints[i] = resolvedEndpointInCache
|
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resolvedEndpoints
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveSync() throws -> [Endpoint?] {
|
func resolveSync() throws -> [Endpoint?] {
|
||||||
|
@ -41,19 +34,18 @@ class DNSResolver {
|
||||||
let dispatchGroup = self.dispatchGroup
|
let dispatchGroup = self.dispatchGroup
|
||||||
dispatchWorkItems = []
|
dispatchWorkItems = []
|
||||||
|
|
||||||
|
if (isAllEndpointsAlreadyResolved()) {
|
||||||
|
return endpoints
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
for (i, endpoint) in self.endpoints.enumerated() {
|
for (i, endpoint) in self.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
|
||||||
} else if let resolvedEndpointStringInCache = DNSResolver.cache.object(forKey: endpoint.stringRepresentation() as NSString),
|
|
||||||
let resolvedEndpointInCache = Endpoint(from: resolvedEndpointStringInCache as String) {
|
|
||||||
resolvedEndpoints[i] = resolvedEndpointInCache
|
|
||||||
} else {
|
} else {
|
||||||
let workItem = DispatchWorkItem {
|
let workItem = DispatchWorkItem {
|
||||||
resolvedEndpoints[i] = DNSResolver.resolveSync(endpoint: endpoint)
|
resolvedEndpoints[i] = DNSResolver.resolveSync(endpoint: endpoint)
|
||||||
isResolvedByDNSRequest[i] = true
|
|
||||||
}
|
}
|
||||||
dispatchWorkItems.append(workItem)
|
dispatchWorkItems.append(workItem)
|
||||||
DispatchQueue.global(qos: .userInitiated).async(group: dispatchGroup, execute: workItem)
|
DispatchQueue.global(qos: .userInitiated).async(group: dispatchGroup, execute: workItem)
|
||||||
|
|
Loading…
Reference in New Issue