Improve performance
This commit is contained in:
parent
055c238baf
commit
728a148980
|
@ -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<Message> { 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
|
||||
|
|
|
@ -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<Set<Promise<RawResponse>>> {
|
||||
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<Set<Promise<RawResponse>>> {
|
||||
func sendLokiMessage(_ lokiMessage: Message, to target: Target) -> Promise<RawResponse> {
|
||||
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<Set<Promise<RawResponse>>> {
|
||||
return lokiMessage.calculatePoW().then { updatedLokiMessage -> Promise<Set<Promise<RawResponse>>> 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<Set<Promise<RawResponse>>> 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue