diff --git a/SignalServiceKit/src/Loki/API/OnionRequestAPI.swift b/SignalServiceKit/src/Loki/API/OnionRequestAPI.swift index b615a918c..cf1e5efb0 100644 --- a/SignalServiceKit/src/Loki/API/OnionRequestAPI.swift +++ b/SignalServiceKit/src/Loki/API/OnionRequestAPI.swift @@ -1,8 +1,8 @@ import PromiseKit -// TODO: Threading - internal enum OnionRequestAPI { + private static let workQueue = DispatchQueue.global() // TODO: We should probably move away from using the global queue for this + internal static var guardSnodes: Set = [] internal static var paths: Set = [] @@ -33,7 +33,7 @@ internal enum OnionRequestAPI { let hexEncodedPublicKey = getUserHexEncodedPublicKey() let parameters: JSON = [ "pubKey" : hexEncodedPublicKey ] let timeout: TimeInterval = 10 // Use a shorter timeout for testing - return LokiAPI.invoke(.getSwarm, on: candidate, associatedWith: hexEncodedPublicKey, parameters: parameters, timeout: timeout).map { _ in } + return LokiAPI.invoke(.getSwarm, on: candidate, associatedWith: hexEncodedPublicKey, parameters: parameters, timeout: timeout).map(on: workQueue) { _ in } } /// Finds `guardSnodeCount` reliable guard snodes to use for path building. The returned promise may error out with `Error.insufficientSnodes` @@ -43,18 +43,18 @@ internal enum OnionRequestAPI { return Promise> { $0.fulfill(guardSnodes) } } else { print("[Loki] Populating guard snode cache.") - return LokiAPI.getRandomSnode().then { _ -> Promise> in // Just used to populate the snode pool + return LokiAPI.getRandomSnode().then(on: workQueue) { _ -> Promise> in // Just used to populate the snode pool let snodePool = LokiAPI.randomSnodePool guard !snodePool.isEmpty else { throw Error.insufficientSnodes } - var result: Set = [] // TODO: Sync on DispatchQueue.global() + var result: Set = [] // Sync on DispatchQueue.global() // Loops until a valid guard snode is found func getGuardSnode() -> Promise { // randomElement() uses the system's default random generator, which is cryptographically secure guard let candidate = snodePool.randomElement() else { return Promise { $0.reject(Error.insufficientSnodes) } } - return testGuardSnodeCandidate(candidate).map { candidate }.recover { _ in getGuardSnode() } + return testGuardSnodeCandidate(candidate).map(on: workQueue) { candidate }.recover(on: workQueue) { _ in getGuardSnode() } } func getAndStoreGuardSnode() -> Promise { - return getGuardSnode().then { guardSnode -> Promise in + return getGuardSnode().then(on: workQueue) { guardSnode -> Promise in if !result.contains(guardSnode) { result.insert(guardSnode) return Promise { $0.fulfill(guardSnode) } @@ -64,7 +64,7 @@ internal enum OnionRequestAPI { } } let promises = (0.. Promise> { print("[Loki] Building onion request paths.") - return LokiAPI.getRandomSnode().then { _ -> Promise> in // Just used to populate the snode pool + return LokiAPI.getRandomSnode().then(on: workQueue) { _ -> Promise> in // Just used to populate the snode pool let snodePool = LokiAPI.randomSnodePool - return getGuardSnodes().map { guardSnodes in + return getGuardSnodes().map(on: workQueue) { guardSnodes in var unusedSnodes = snodePool.subtracting(guardSnodes) let minSnodeCount = guardSnodeCount * pathSize - guardSnodeCount guard unusedSnodes.count >= minSnodeCount else { throw Error.insufficientSnodes } @@ -102,7 +102,7 @@ internal enum OnionRequestAPI { if paths.count >= pathCount { return Promise { $0.fulfill(paths.randomElement()!) } } else { - return buildPaths().map { paths in + return buildPaths().map(on: workQueue) { paths in let path = paths.randomElement()! OnionRequestAPI.paths = paths return path