Hopefully fix threading issue

This commit is contained in:
Niels Andriesse 2020-01-20 15:04:22 +11:00
parent de2d0ea15e
commit 7020f2a73e
3 changed files with 3 additions and 4 deletions

View File

@ -121,7 +121,7 @@ public extension LokiAPI {
internal extension Promise {
internal func handlingSwarmSpecificErrorsIfNeeded(for target: LokiAPITarget, associatedWith hexEncodedPublicKey: String) -> Promise<T> {
return recover(on: DispatchQueue.global()) { error -> Promise<T> in
return recover(on: LokiAPI.errorHandlingQueue) { error -> Promise<T> in
if let error = error as? NetworkManagerError {
switch error.statusCode {
case 0, 400, 500, 503:
@ -130,7 +130,6 @@ internal extension Promise {
let newFailureCount = oldFailureCount + 1
LokiAPI.failureCount[target] = newFailureCount
print("[Loki] Couldn't reach snode at: \(target); setting failure count to \(newFailureCount).")
Analytics.shared.track("Unreachable Snode")
if newFailureCount >= LokiAPI.failureThreshold {
print("[Loki] Failure threshold reached for: \(target); dropping it.")
LokiAPI.dropIfNeeded(target, hexEncodedPublicKey: hexEncodedPublicKey) // Remove it from the swarm cache associated with the given public key
@ -140,7 +139,6 @@ internal extension Promise {
case 421:
// The snode isn't associated with the given public key anymore
print("[Loki] Invalidating swarm for: \(hexEncodedPublicKey).")
Analytics.shared.track("Migrated Snode")
LokiAPI.dropIfNeeded(target, hexEncodedPublicKey: hexEncodedPublicKey)
case 432:
// The PoW difficulty is too low

View File

@ -5,6 +5,7 @@ public final class LokiAPI : NSObject {
private static var syncMessageTimestamps: [String:Set<UInt64>] = [:]
public static var lastDeviceLinkUpdate: [String:Date] = [:] // Hex encoded public key to date
@objc public static var userHexEncodedPublicKeyCache: [String:Set<String>] = [:] // Thread ID to set of user hex encoded public keys
public static let errorHandlingQueue = DispatchQueue(label: "errorHandlingQueue")
// MARK: Convenience
internal static let storage = OWSPrimaryStorage.shared()

View File

@ -69,7 +69,7 @@ public final class LokiLongPoller : NSObject {
let nextSnode = unusedSnodes.randomElement()!
usedSnodes.insert(nextSnode)
print("[Loki] Opening long polling connection to \(nextSnode).")
longPoll(nextSnode, seal: seal).catch(on: DispatchQueue.global()) { [weak self] error in
longPoll(nextSnode, seal: seal).catch(on: LokiAPI.errorHandlingQueue) { [weak self] error in
print("[Loki] Long polling connection to \(nextSnode) failed; dropping it and switching to next snode.")
LokiAPI.dropIfNeeded(nextSnode, hexEncodedPublicKey: userHexEncodedPublicKey)
self?.openConnectionToNextSnode(seal: seal)