From 728a14898081d580db78572e17f42ade06d23e34 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 27 May 2019 12:50:30 +1000 Subject: [PATCH] Improve performance --- .../src/Loki/API/LokiAPI+Message.swift | 12 +++++------ SignalServiceKit/src/Loki/API/LokiAPI.swift | 20 ++++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+Message.swift b/SignalServiceKit/src/Loki/API/LokiAPI+Message.swift index f3e7b5539..9a5fbf748 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI+Message.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI+Message.swift @@ -16,23 +16,21 @@ public extension LokiAPI { /// When the proof of work was calculated, if applicable (P2P messages don't require proof of work). /// /// - Note: Expressed as milliseconds since 00:00:00 UTC on 1 January 1970. - private(set) var timestamp: UInt64? + private(set) var timestamp: UInt64? = nil /// The base 64 encoded proof of work, if applicable (P2P messages don't require proof of work). - private(set) var nonce: String? + private(set) var nonce: String? = nil - private init(destination: String, data: LosslessStringConvertible, ttl: UInt64, isPing: Bool = false, timestamp: UInt64? = nil, nonce: String? = nil) { + private init(destination: String, data: LosslessStringConvertible, ttl: UInt64, isPing: Bool) { self.destination = destination self.data = data self.ttl = ttl self.isPing = isPing - self.timestamp = timestamp - self.nonce = nonce } /// Construct a `LokiMessage` from a `SignalMessage`. /// /// - Note: `timestamp` is the original message timestamp (i.e. `TSOutgoingMessage.timestamp`). - public static func from(signalMessage: SignalMessage, timestamp: UInt64) -> Message? { + public static func from(signalMessage: SignalMessage, with timestamp: UInt64) -> Message? { // To match the desktop application, we have to wrap the data in an envelope and then wrap that in a websocket object do { let wrappedMessage = try LokiMessageWrapper.wrap(message: signalMessage, timestamp: timestamp) @@ -55,7 +53,7 @@ public extension LokiAPI { return Promise { seal in DispatchQueue.global(qos: .default).async { let now = NSDate.ows_millisecondTimeStamp() - let dataAsString = self.data as! String // Safe because of the way from(signalMessage:timestamp:) is implemented + let dataAsString = self.data as! String // Safe because of how from(signalMessage:with:) is implemented if let nonce = ProofOfWork.calculate(data: dataAsString, pubKey: self.destination, timestamp: now, ttl: self.ttl) { var result = self result.timestamp = now diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index bfd8f7569..fbc042ec3 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -47,23 +47,25 @@ import PromiseKit let newRawMessages = removeDuplicates(from: rawMessages) return parseProtoEnvelopes(from: newRawMessages) } - }.retryingIfNeeded(maxRetryCount: maxRetryCount).map { Set($0) } + }.map { Set($0) }.retryingIfNeeded(maxRetryCount: maxRetryCount) } public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, with timestamp: UInt64) -> Promise>> { - guard let lokiMessage = Message.from(signalMessage: signalMessage, timestamp: timestamp) else { return Promise(error: Error.messageConversionFailed) } + guard let lokiMessage = Message.from(signalMessage: signalMessage, with: timestamp) else { return Promise(error: Error.messageConversionFailed) } let destination = lokiMessage.destination - func sendLokiMessage(_ lokiMessage: Message, to targets: [Target]) -> Promise>> { + func sendLokiMessage(_ lokiMessage: Message, to target: Target) -> Promise { let parameters = lokiMessage.toJSON() - return Promise.value(targets).mapValues { invoke(.sendMessage, on: $0, associatedWith: destination, parameters: parameters) }.map { Set($0) } + return invoke(.sendMessage, on: target, associatedWith: destination, parameters: parameters) } func sendLokiMessageUsingSwarmAPI() -> Promise>> { - return lokiMessage.calculatePoW().then { updatedLokiMessage -> Promise>> in - return getTargetSnodes(for: destination).then { sendLokiMessage(updatedLokiMessage, to: $0) } + let powPromise = lokiMessage.calculatePoW() + let swarmPromise = getTargetSnodes(for: destination) + return when(fulfilled: powPromise, swarmPromise).map { lokiMessageWithPoW, swarm in + return Set(swarm.map { sendLokiMessage(lokiMessageWithPoW, to: $0) }) } } if let p2pDetails = LokiP2PManager.getDetails(forContact: destination), (lokiMessage.isPing || p2pDetails.isOnline) { - return sendLokiMessage(lokiMessage, to: [ p2pDetails.target ]).get { _ in + return Promise.value([ p2pDetails.target ]).mapValues { sendLokiMessage(lokiMessage, to: $0) }.map { Set($0) }.retryingIfNeeded(maxRetryCount: maxRetryCount).get { _ in LokiP2PManager.setOnline(true, forContact: destination) }.recover { error -> Promise>> in LokiP2PManager.setOnline(false, forContact: destination) @@ -76,10 +78,10 @@ import PromiseKit throw error } } - return sendLokiMessageUsingSwarmAPI() + return sendLokiMessageUsingSwarmAPI().retryingIfNeeded(maxRetryCount: maxRetryCount) } } else { - return sendLokiMessageUsingSwarmAPI() + return sendLokiMessageUsingSwarmAPI().retryingIfNeeded(maxRetryCount: maxRetryCount) } }