Unbatch legacy contact requests

// FREEBIE
pull/1/head
Michael Kirk 7 years ago
parent e602cf55a0
commit 2e38fa145c

@ -82,8 +82,8 @@ NS_ASSUME_NONNULL_BEGIN
failure:(void (^)(NSError *error))failure failure:(void (^)(NSError *error))failure
{ {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
OWSContactDiscoveryOperation *operation = OWSLegacyContactDiscoveryOperation *operation =
[[OWSContactDiscoveryOperation alloc] initWithRecipientIdsToLookup:recipientIdsToLookup.allObjects]; [[OWSLegacyContactDiscoveryOperation alloc] initWithRecipientIdsToLookup:recipientIdsToLookup.allObjects];
NSArray<NSOperation *> *operationAndDependencies = [operation.dependencies arrayByAddingObject:operation]; NSArray<NSOperation *> *operationAndDependencies = [operation.dependencies arrayByAddingObject:operation];
[self.contactIntersectionQueue addOperations:operationAndDependencies waitUntilFinished:YES]; [self.contactIntersectionQueue addOperations:operationAndDependencies waitUntilFinished:YES];

@ -4,10 +4,17 @@
import Foundation import Foundation
@objc(OWSContactDiscoveryOperation) @objc(OWSCDSOperation)
class ContactDiscoveryOperation: OWSOperation, LegacyContactDiscoveryBatchOperationDelegate { class CDSOperation: OWSOperation {
let batchSize = 2048 let batchSize = 2048
static let operationQueue: OperationQueue = {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 5
return queue
}()
let recipientIdsToLookup: [String] let recipientIdsToLookup: [String]
@objc @objc
@ -22,8 +29,7 @@ class ContactDiscoveryOperation: OWSOperation, LegacyContactDiscoveryBatchOperat
Logger.debug("\(logTag) in \(#function) with recipientIdsToLookup: \(recipientIdsToLookup.count)") Logger.debug("\(logTag) in \(#function) with recipientIdsToLookup: \(recipientIdsToLookup.count)")
for batchIds in recipientIdsToLookup.chunked(by: batchSize) { for batchIds in recipientIdsToLookup.chunked(by: batchSize) {
let batchOperation = LegacyContactDiscoveryBatchOperation(recipientIdsToLookup: batchIds) let batchOperation = CDSBatchOperation(recipientIdsToLookup: batchIds)
batchOperation.delegate = self
self.addDependency(batchOperation) self.addDependency(batchOperation)
} }
} }
@ -35,7 +41,7 @@ class ContactDiscoveryOperation: OWSOperation, LegacyContactDiscoveryBatchOperat
Logger.debug("\(logTag) in \(#function)") Logger.debug("\(logTag) in \(#function)")
for dependency in self.dependencies { for dependency in self.dependencies {
guard let batchOperation = dependency as? LegacyContactDiscoveryBatchOperation else { guard let batchOperation = dependency as? CDSBatchOperation else {
owsFail("\(self.logTag) in \(#function) unexpected dependency: \(dependency)") owsFail("\(self.logTag) in \(#function) unexpected dependency: \(dependency)")
continue continue
} }
@ -46,23 +52,13 @@ class ContactDiscoveryOperation: OWSOperation, LegacyContactDiscoveryBatchOperat
self.reportSuccess() self.reportSuccess()
} }
// MARK: LegacyContactDiscoveryBatchOperationDelegate
func contactDiscoverBatchOperation(_ contactDiscoverBatchOperation: LegacyContactDiscoveryBatchOperation, didFailWithError error: Error) {
Logger.debug("\(logTag) in \(#function) canceling self and all dependencies.")
self.dependencies.forEach { $0.cancel() }
self.cancel()
}
}
protocol LegacyContactDiscoveryBatchOperationDelegate: class {
func contactDiscoverBatchOperation(_ contactDiscoverBatchOperation: LegacyContactDiscoveryBatchOperation, didFailWithError error: Error)
} }
@objc(OWSLegacyContactDiscoveryOperation)
class LegacyContactDiscoveryBatchOperation: OWSOperation { class LegacyContactDiscoveryBatchOperation: OWSOperation {
@objc
var registeredRecipientIds: Set<String> var registeredRecipientIds: Set<String>
weak var delegate: LegacyContactDiscoveryBatchOperationDelegate?
private let recipientIdsToLookup: [String] private let recipientIdsToLookup: [String]
private var networkManager: TSNetworkManager { private var networkManager: TSNetworkManager {
@ -71,6 +67,7 @@ class LegacyContactDiscoveryBatchOperation: OWSOperation {
// MARK: Initializers // MARK: Initializers
@objc
required init(recipientIdsToLookup: [String]) { required init(recipientIdsToLookup: [String]) {
self.recipientIdsToLookup = recipientIdsToLookup self.recipientIdsToLookup = recipientIdsToLookup
self.registeredRecipientIds = Set() self.registeredRecipientIds = Set()
@ -134,16 +131,12 @@ class LegacyContactDiscoveryBatchOperation: OWSOperation {
// Called at most one time. // Called at most one time.
override func didSucceed() { override func didSucceed() {
// Compare against new CDS service // Compare against new CDS service
let newCDSBatchOperation = CDSBatchOperation(recipientIdsToLookup: self.recipientIdsToLookup) let modernCDSOperation = CDSOperation(recipientIdsToLookup: self.recipientIdsToLookup)
let cdsFeedbackOperation = CDSFeedbackOperation(legacyRegisteredRecipientIds: self.registeredRecipientIds) let cdsFeedbackOperation = CDSFeedbackOperation(legacyRegisteredRecipientIds: self.registeredRecipientIds)
cdsFeedbackOperation.addDependency(newCDSBatchOperation) cdsFeedbackOperation.addDependency(modernCDSOperation)
CDSBatchOperation.operationQueue.addOperations([newCDSBatchOperation, cdsFeedbackOperation], waitUntilFinished: false)
}
// Called at most one time. let operations = modernCDSOperation.dependencies + [modernCDSOperation, cdsFeedbackOperation]
override func didFail(error: Error) { CDSOperation.operationQueue.addOperations(operations, waitUntilFinished: false)
self.delegate?.contactDiscoverBatchOperation(self, didFailWithError: error)
} }
// MARK: Private Helpers // MARK: Private Helpers
@ -204,13 +197,6 @@ class CDSBatchOperation: OWSOperation {
private let recipientIdsToLookup: [String] private let recipientIdsToLookup: [String]
private(set) var registeredRecipientIds: Set<String> private(set) var registeredRecipientIds: Set<String>
static let operationQueue: OperationQueue = {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
return queue
}()
private var networkManager: TSNetworkManager { private var networkManager: TSNetworkManager {
return TSNetworkManager.shared() return TSNetworkManager.shared()
} }
@ -411,7 +397,6 @@ class CDSBatchOperation: OWSOperation {
additionalAuthenticatedData: nil, additionalAuthenticatedData: nil,
authTag: authTag, authTag: authTag,
key: remoteAttestation.keys.serverKey) else { key: remoteAttestation.keys.serverKey) else {
throw ContactDiscoveryError.parseError(description: "decryption failed") throw ContactDiscoveryError.parseError(description: "decryption failed")
} }
@ -462,7 +447,7 @@ class CDSFeedbackOperation: OWSOperation {
return return
} }
guard let cdsOperation = dependencies.first as? CDSBatchOperation else { guard let cdsOperation = dependencies.first as? CDSOperation else {
let error = OWSErrorMakeAssertionError("\(self.logTag) in \(#function) cdsOperation was unexpectedly nil") let error = OWSErrorMakeAssertionError("\(self.logTag) in \(#function) cdsOperation was unexpectedly nil")
self.reportError(error) self.reportError(error)
return return

Loading…
Cancel
Save