From 41fe33525c6116fa08815a283900d764fea0067c Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 22 Jul 2021 13:07:51 +1000 Subject: [PATCH] Don't unnecessarily open sync transactions --- SessionSnodeKit/SnodeAPI.swift | 8 ++------ SessionSnodeKit/Storage+SnodeAPI.swift | 6 ++++-- SessionSnodeKit/Storage.swift | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/SessionSnodeKit/SnodeAPI.swift b/SessionSnodeKit/SnodeAPI.swift index a6d45db7d..214bef8f9 100644 --- a/SessionSnodeKit/SnodeAPI.swift +++ b/SessionSnodeKit/SnodeAPI.swift @@ -387,9 +387,7 @@ public final class SnodeAPI : NSObject { let (promise, seal) = RawResponsePromise.pending() let storage = SNSnodeKitConfiguration.shared.storage Threading.workQueue.async { - storage.writeSync { transaction in - storage.pruneLastMessageHashInfoIfExpired(for: snode, associatedWith: publicKey, using: transaction) - } + storage.pruneLastMessageHashInfoIfExpired(for: snode, associatedWith: publicKey) let lastHash = storage.getLastMessageHash(for: snode, associatedWith: publicKey) ?? "" let parameters = [ "pubKey" : Features.useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ] invoke(.getMessages, on: snode, associatedWith: publicKey, parameters: parameters).done2 { seal.fulfill($0) }.catch2 { seal.reject($0) } @@ -403,9 +401,7 @@ public final class SnodeAPI : NSObject { Threading.workQueue.async { attempt(maxRetryCount: maxRetryCount, recoveringOn: Threading.workQueue) { getTargetSnodes(for: publicKey).mapValues2 { targetSnode in - storage.writeSync { transaction in - storage.pruneLastMessageHashInfoIfExpired(for: targetSnode, associatedWith: publicKey, using: transaction) - } + storage.pruneLastMessageHashInfoIfExpired(for: targetSnode, associatedWith: publicKey) let lastHash = storage.getLastMessageHash(for: targetSnode, associatedWith: publicKey) ?? "" let parameters = [ "pubKey" : Features.useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ] return invoke(.getMessages, on: targetSnode, associatedWith: publicKey, parameters: parameters).map2 { rawResponse in diff --git a/SessionSnodeKit/Storage+SnodeAPI.swift b/SessionSnodeKit/Storage+SnodeAPI.swift index c7378fd76..24b36365f 100644 --- a/SessionSnodeKit/Storage+SnodeAPI.swift +++ b/SessionSnodeKit/Storage+SnodeAPI.swift @@ -103,12 +103,14 @@ extension Storage { (transaction as! YapDatabaseReadWriteTransaction).setObject(lastMessageHashInfo, forKey: key, inCollection: Storage.lastMessageHashCollection) } - public func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String, using transaction: Any) { + public func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String) { guard let lastMessageHashInfo = getLastMessageHashInfo(for: snode, associatedWith: publicKey), (lastMessageHashInfo["hash"] as? String) != nil, let expirationDate = (lastMessageHashInfo["expirationDate"] as? NSNumber)?.uint64Value else { return } let now = NSDate.millisecondTimestamp() if now >= expirationDate { - removeLastMessageHashInfo(for: snode, associatedWith: publicKey, using: transaction) + Storage.writeSync { transaction in + self.removeLastMessageHashInfo(for: snode, associatedWith: publicKey, using: transaction) + } } } diff --git a/SessionSnodeKit/Storage.swift b/SessionSnodeKit/Storage.swift index ee887efdd..ce0a90f81 100644 --- a/SessionSnodeKit/Storage.swift +++ b/SessionSnodeKit/Storage.swift @@ -22,7 +22,7 @@ public protocol SessionSnodeKitStorageProtocol { func setSwarm(to swarm: Set, for publicKey: String, using transaction: Any) func getLastMessageHash(for snode: Snode, associatedWith publicKey: String) -> String? func setLastMessageHashInfo(for snode: Snode, associatedWith publicKey: String, to lastMessageHashInfo: JSON, using transaction: Any) - func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String, using transaction: Any) + func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String) func getReceivedMessages(for publicKey: String) -> Set func setReceivedMessages(to receivedMessages: Set, for publicKey: String, using transaction: Any) }