From 256cd631db6b87db21e52e919a77bc533ba3cabd Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 12 Jun 2020 10:08:07 +1000 Subject: [PATCH] Further tweak threading --- SignalServiceKit/src/Loki/API/LokiAPI.swift | 2 +- .../src/Loki/API/LokiDotNetAPI.swift | 6 ++-- .../src/Loki/API/LokiFileServerAPI.swift | 2 +- .../src/Loki/API/LokiHTTPClient.swift | 2 +- .../Open Groups/LokiPublicChatPoller.swift | 2 +- .../src/Loki/Utilities/Promise+Retrying.swift | 2 +- .../Loki/Utilities/Promise+Threading.swift | 30 +++++++++---------- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index 5dd1a9c8a..aae83e834 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -44,7 +44,7 @@ public final class LokiAPI : NSObject { let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ]) if let headers = headers { request.allHTTPHeaderFields = headers } request.timeoutInterval = timeout ?? defaultTimeout - return TSNetworkManager.shared().perform(request, withCompletionQueue: DispatchQueue.global(qos: .userInitiated)) + return TSNetworkManager.shared().perform(request, withCompletionQueue: DispatchQueue.global(qos: .default)) .map2 { $0.responseObject } .handlingSnodeErrorsIfNeeded(for: target, associatedWith: hexEncodedPublicKey) .recoveringNetworkErrorsIfNeeded() diff --git a/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift b/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift index 05e4ff3bc..203bc07fb 100644 --- a/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift @@ -65,7 +65,7 @@ public class LokiDotNetAPI : NSObject { let queryParameters = "pubKey=\(getUserHexEncodedPublicKey())" let url = URL(string: "\(server)/loki/v1/get_challenge?\(queryParameters)")! let request = TSRequest(url: url) - return LokiFileServerProxy(for: server).perform(request, withCompletionQueue: DispatchQueue.global(qos: .userInitiated)).map2 { rawResponse in + return LokiFileServerProxy(for: server).perform(request, withCompletionQueue: DispatchQueue.global(qos: .default)).map2 { rawResponse in guard let json = rawResponse as? JSON, let base64EncodedChallenge = json["cipherText64"] as? String, let base64EncodedServerPublicKey = json["serverPubKey64"] as? String, let challenge = Data(base64Encoded: base64EncodedChallenge), var serverPublicKey = Data(base64Encoded: base64EncodedServerPublicKey) else { throw LokiDotNetAPIError.parsingFailed @@ -89,7 +89,7 @@ public class LokiDotNetAPI : NSObject { let url = URL(string: "\(server)/loki/v1/submit_challenge")! let parameters = [ "pubKey" : getUserHexEncodedPublicKey(), "token" : token ] let request = TSRequest(url: url, method: "POST", parameters: parameters) - return LokiFileServerProxy(for: server).perform(request, withCompletionQueue: DispatchQueue.global(qos: .userInitiated)).map2 { _ in token } + return LokiFileServerProxy(for: server).perform(request, withCompletionQueue: DispatchQueue.global(qos: .default)).map2 { _ in token } } // MARK: Public API @@ -191,7 +191,7 @@ public class LokiDotNetAPI : NSObject { proceed(with: "loki") // Uploads to the Loki File Server shouldn't include any personally identifiable information so use a dummy auth token } } else { - getAuthToken(for: server).done2 { token in + getAuthToken(for: server).done(on: DispatchQueue.global(qos: .userInitiated)) { token in proceed(with: token) }.catch2 { error in print("[Loki] Couldn't upload attachment due to error: \(error).") diff --git a/SignalServiceKit/src/Loki/API/LokiFileServerAPI.swift b/SignalServiceKit/src/Loki/API/LokiFileServerAPI.swift index 948896d6b..b78a05eab 100644 --- a/SignalServiceKit/src/Loki/API/LokiFileServerAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiFileServerAPI.swift @@ -38,7 +38,7 @@ public final class LokiFileServerAPI : LokiDotNetAPI { let queryParameters = "ids=\(hexEncodedPublicKeys.map { "@\($0)" }.joined(separator: ","))&include_user_annotations=1" let url = URL(string: "\(server)/users?\(queryParameters)")! let request = TSRequest(url: url) - return LokiFileServerProxy(for: server).perform(request, withCompletionQueue: DispatchQueue.global(qos: .userInitiated)).map2 { rawResponse -> Set in + return LokiFileServerProxy(for: server).perform(request, withCompletionQueue: DispatchQueue.global(qos: .default)).map2 { rawResponse -> Set in guard let json = rawResponse as? JSON, let data = json["data"] as? [JSON] else { print("[Loki] Couldn't parse device links for users: \(hexEncodedPublicKeys) from: \(rawResponse).") throw LokiDotNetAPIError.parsingFailed diff --git a/SignalServiceKit/src/Loki/API/LokiHTTPClient.swift b/SignalServiceKit/src/Loki/API/LokiHTTPClient.swift index 516a903b4..8f3989eff 100644 --- a/SignalServiceKit/src/Loki/API/LokiHTTPClient.swift +++ b/SignalServiceKit/src/Loki/API/LokiHTTPClient.swift @@ -10,7 +10,7 @@ public class LokiHTTPClient { securityPolicy.validatesDomainName = false result.securityPolicy = securityPolicy result.responseSerializer = AFHTTPResponseSerializer() - result.completionQueue = DispatchQueue.global(qos: .userInitiated) + result.completionQueue = DispatchQueue.global(qos: .default) return result }() diff --git a/SignalServiceKit/src/Loki/API/Open Groups/LokiPublicChatPoller.swift b/SignalServiceKit/src/Loki/API/Open Groups/LokiPublicChatPoller.swift index 6d9fc5906..0b98a325a 100644 --- a/SignalServiceKit/src/Loki/API/Open Groups/LokiPublicChatPoller.swift +++ b/SignalServiceKit/src/Loki/API/Open Groups/LokiPublicChatPoller.swift @@ -203,7 +203,7 @@ public final class LokiPublicChatPoller : NSObject { proceed() } } else { - DispatchQueue.global(qos: .userInitiated).async { + DispatchQueue.global(qos: .default).async { proceed() } } diff --git a/SignalServiceKit/src/Loki/Utilities/Promise+Retrying.swift b/SignalServiceKit/src/Loki/Utilities/Promise+Retrying.swift index 558620895..675c25782 100644 --- a/SignalServiceKit/src/Loki/Utilities/Promise+Retrying.swift +++ b/SignalServiceKit/src/Loki/Utilities/Promise+Retrying.swift @@ -1,7 +1,7 @@ import PromiseKit /// Retry the promise constructed in `body` up to `maxRetryCount` times. -internal func attempt(maxRetryCount: UInt, recoveringOn queue: DispatchQueue = .global(qos: .userInitiated), body: @escaping () -> Promise) -> Promise { +internal func attempt(maxRetryCount: UInt, recoveringOn queue: DispatchQueue = .global(qos: .default), body: @escaping () -> Promise) -> Promise { var retryCount = 0 func attempt() -> Promise { return body().recover(on: queue) { error -> Promise in diff --git a/SignalServiceKit/src/Loki/Utilities/Promise+Threading.swift b/SignalServiceKit/src/Loki/Utilities/Promise+Threading.swift index 6daed1706..b6ff6b76e 100644 --- a/SignalServiceKit/src/Loki/Utilities/Promise+Threading.swift +++ b/SignalServiceKit/src/Loki/Utilities/Promise+Threading.swift @@ -3,74 +3,74 @@ import PromiseKit public extension Thenable { func then2(_ body: @escaping (T) throws -> U) -> Promise where U : Thenable { - return then(on: DispatchQueue.global(qos: .userInitiated), body) + return then(on: DispatchQueue.global(qos: .default), body) } func map2(_ transform: @escaping (T) throws -> U) -> Promise { - return map(on: DispatchQueue.global(qos: .userInitiated), transform) + return map(on: DispatchQueue.global(qos: .default), transform) } func done2(_ body: @escaping (T) throws -> Void) -> Promise { - return done(on: DispatchQueue.global(qos: .userInitiated), body) + return done(on: DispatchQueue.global(qos: .default), body) } func get2(_ body: @escaping (T) throws -> Void) -> Promise { - return get(on: DispatchQueue.global(qos: .userInitiated), body) + return get(on: DispatchQueue.global(qos: .default), body) } } public extension Thenable where T: Sequence { func mapValues2(_ transform: @escaping (T.Iterator.Element) throws -> U) -> Promise<[U]> { - return mapValues(on: DispatchQueue.global(qos: .userInitiated), transform) + return mapValues(on: DispatchQueue.global(qos: .default), transform) } } public extension Guarantee { func then2(_ body: @escaping (T) -> Guarantee) -> Guarantee { - return then(on: DispatchQueue.global(qos: .userInitiated), body) + return then(on: DispatchQueue.global(qos: .default), body) } func map2(_ body: @escaping (T) -> U) -> Guarantee { - return map(on: DispatchQueue.global(qos: .userInitiated), body) + return map(on: DispatchQueue.global(qos: .default), body) } func done2(_ body: @escaping (T) -> Void) -> Guarantee { - return done(on: DispatchQueue.global(qos: .userInitiated), body) + return done(on: DispatchQueue.global(qos: .default), body) } func get2(_ body: @escaping (T) -> Void) -> Guarantee { - return get(on: DispatchQueue.global(qos: .userInitiated), body) + return get(on: DispatchQueue.global(qos: .default), body) } } public extension CatchMixin { func catch2(_ body: @escaping (Error) -> Void) -> PMKFinalizer { - return self.catch(on: DispatchQueue.global(qos: .userInitiated), body) + return self.catch(on: DispatchQueue.global(qos: .default), body) } func recover2(_ body: @escaping(Error) throws -> U) -> Promise where U.T == T { - return recover(on: DispatchQueue.global(qos: .userInitiated), body) + return recover(on: DispatchQueue.global(qos: .default), body) } func recover2(_ body: @escaping(Error) -> Guarantee) -> Guarantee { - return recover(on: DispatchQueue.global(qos: .userInitiated), body) + return recover(on: DispatchQueue.global(qos: .default), body) } func ensure2(_ body: @escaping () -> Void) -> Promise { - return ensure(on: DispatchQueue.global(qos: .userInitiated), body) + return ensure(on: DispatchQueue.global(qos: .default), body) } } public extension CatchMixin where T == Void { func recover2(_ body: @escaping(Error) -> Void) -> Guarantee { - return recover(on: DispatchQueue.global(qos: .userInitiated), body) + return recover(on: DispatchQueue.global(qos: .default), body) } func recover2(_ body: @escaping(Error) throws -> Void) -> Promise { - return recover(on: DispatchQueue.global(qos: .userInitiated), body) + return recover(on: DispatchQueue.global(qos: .default), body) } }