From 0783a9c421f53714ea3baeca25302f03894c6501 Mon Sep 17 00:00:00 2001 From: Juan Manuel Pereira Date: Mon, 6 May 2024 16:45:26 -0300 Subject: [PATCH] Run more parallel operations on manual scans --- .../DataBrokerOperationsCollection.swift | 13 +++--- .../DataBrokerProtectionProcessor.swift | 10 ++--- ...kerProtectionProcessorConfiguration.swift} | 24 ++++++---- .../DataBrokerProtectionScheduler.swift | 1 - ...rotectionProcessorConfigurationTests.swift | 44 +++++++++++++++++++ 5 files changed, 70 insertions(+), 22 deletions(-) rename LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/{DataBrokerProtectionSchedulerConfig.swift => DataBrokerProtectionProcessorConfiguration.swift} (52%) create mode 100644 LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionProcessorConfigurationTests.swift diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/DataBrokerOperationsCollection.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/DataBrokerOperationsCollection.swift index eeae49e168..d585da0cd7 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/DataBrokerOperationsCollection.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/DataBrokerOperationsCollection.swift @@ -28,14 +28,13 @@ protocol DataBrokerOperationsCollectionErrorDelegate: AnyObject { didErrorBeforeStartingBrokerOperations error: Error) } -final class DataBrokerOperationsCollection: Operation { - - enum OperationType { - case manualScan - case optOut - case all - } +enum OperationType { + case manualScan + case optOut + case all +} +final class DataBrokerOperationsCollection: Operation { public var error: Error? public weak var errorDelegate: DataBrokerOperationsCollectionErrorDelegate? diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessor.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessor.swift index dee4d346e2..c96a3c31d8 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessor.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessor.swift @@ -26,7 +26,7 @@ protocol OperationRunnerProvider { final class DataBrokerProtectionProcessor { private let database: DataBrokerProtectionRepository - private let config: SchedulerConfig + private let config: DataBrokerProtectionProcessorConfiguration private let operationRunnerProvider: OperationRunnerProvider private let notificationCenter: NotificationCenter private let operationQueue: OperationQueue @@ -36,7 +36,7 @@ final class DataBrokerProtectionProcessor { private let eventPixels: DataBrokerProtectionEventPixels init(database: DataBrokerProtectionRepository, - config: SchedulerConfig, + config: DataBrokerProtectionProcessorConfiguration = DataBrokerProtectionProcessorConfiguration(), operationRunnerProvider: OperationRunnerProvider, notificationCenter: NotificationCenter = NotificationCenter.default, pixelHandler: EventMapping, @@ -48,7 +48,6 @@ final class DataBrokerProtectionProcessor { self.notificationCenter = notificationCenter self.operationQueue = OperationQueue() self.pixelHandler = pixelHandler - self.operationQueue.maxConcurrentOperationCount = config.concurrentOperationsDifferentBrokers self.userNotificationService = userNotificationService self.engagementPixels = DataBrokerProtectionEngagementPixels(database: database, handler: pixelHandler) self.eventPixels = DataBrokerProtectionEventPixels(database: database, handler: pixelHandler) @@ -109,11 +108,12 @@ final class DataBrokerProtectionProcessor { } // MARK: - Private functions - private func runOperations(operationType: DataBrokerOperationsCollection.OperationType, + private func runOperations(operationType: OperationType, priorityDate: Date?, showWebView: Bool, completion: @escaping ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)) { + self.operationQueue.maxConcurrentOperationCount = config.concurrentOperationsFor(operationType) // Before running new operations we check if there is any updates to the broker files. if let vault = try? DataBrokerProtectionSecureVaultFactory.makeVault(reporter: DataBrokerProtectionSecureVaultErrorReporter.shared) { let brokerUpdater = DataBrokerProtectionBrokerUpdater(vault: vault, pixelHandler: pixelHandler) @@ -153,7 +153,7 @@ final class DataBrokerProtectionProcessor { } private func createDataBrokerOperationCollections(from brokerProfileQueriesData: [BrokerProfileQueryData], - operationType: DataBrokerOperationsCollection.OperationType, + operationType: OperationType, priorityDate: Date?, showWebView: Bool) -> [DataBrokerOperationsCollection] { diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionSchedulerConfig.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessorConfiguration.swift similarity index 52% rename from LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionSchedulerConfig.swift rename to LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessorConfiguration.swift index e76c97156f..3281b66c37 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionSchedulerConfig.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessorConfiguration.swift @@ -1,5 +1,5 @@ // -// DataBrokerProtectionSchedulerConfig.swift +// DataBrokerProtectionProcessorConfiguration.swift // // Copyright © 2023 DuckDuckGo. All rights reserved. // @@ -18,13 +18,19 @@ import Foundation -protocol SchedulerConfig { - var concurrentOperationsDifferentBrokers: Int { get } - var intervalBetweenSameBrokerOperations: TimeInterval { get } -} - -struct DataBrokerProtectionSchedulerConfig: SchedulerConfig { +struct DataBrokerProtectionProcessorConfiguration { // Arbitrary numbers for now - var concurrentOperationsDifferentBrokers: Int = 2 - var intervalBetweenSameBrokerOperations: TimeInterval = 2 + let intervalBetweenSameBrokerOperations: TimeInterval = 2 + private let concurrentOperationsDifferentBrokers: Int = 2 + // https://app.asana.com/0/481882893211075/1206981742767469/f + private let concurrentOperationsOnManualScans: Int = 6 + + func concurrentOperationsFor(_ operation: OperationType) -> Int { + switch operation { + case .all, .optOut: + return concurrentOperationsDifferentBrokers + case .manualScan: + return concurrentOperationsOnManualScans + } + } } diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionScheduler.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionScheduler.swift index 4e7d9a9846..509b13272c 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionScheduler.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionScheduler.swift @@ -150,7 +150,6 @@ public final class DefaultDataBrokerProtectionScheduler: DataBrokerProtectionSch captchaService: captchaService) return DataBrokerProtectionProcessor(database: dataManager.database, - config: DataBrokerProtectionSchedulerConfig(), operationRunnerProvider: runnerProvider, notificationCenter: notificationCenter, pixelHandler: pixelHandler, diff --git a/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionProcessorConfigurationTests.swift b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionProcessorConfigurationTests.swift new file mode 100644 index 0000000000..fe8eec11c1 --- /dev/null +++ b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionProcessorConfigurationTests.swift @@ -0,0 +1,44 @@ +// +// DataBrokerProtectionProcessorConfigurationTests.swift +// +// Copyright © 2023 DuckDuckGo. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import XCTest +import Foundation +@testable import DataBrokerProtection + +final class DataBrokerProtectionProcessorConfigurationTests: XCTestCase { + + private let sut = DataBrokerProtectionProcessorConfiguration() + + func testWhenOperationIsManualScans_thenConcurrentOperationsBetweenBrokersIsSix() { + let value = sut.concurrentOperationsFor(.manualScan) + let expectedValue = 6 + XCTAssertEqual(value, expectedValue) + } + + func testWhenOperationIsAll_thenConcurrentOperationsBetweenBrokersIsTwo() { + let value = sut.concurrentOperationsFor(.all) + let expectedValue = 2 + XCTAssertEqual(value, expectedValue) + } + + func testWhenOperationIsOptOut_thenConcurrentOperationsBetweenBrokersIsTwo() { + let value = sut.concurrentOperationsFor(.optOut) + let expectedValue = 2 + XCTAssertEqual(value, expectedValue) + } +}