Skip to content

Commit

Permalink
Run more parallel operations on manual scans
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaemepereira committed May 6, 2024
1 parent 773cffa commit 0783a9c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<DataBrokerProtectionPixels>,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -153,7 +153,7 @@ final class DataBrokerProtectionProcessor {
}

private func createDataBrokerOperationCollections(from brokerProfileQueriesData: [BrokerProfileQueryData],
operationType: DataBrokerOperationsCollection.OperationType,
operationType: OperationType,
priorityDate: Date?,
showWebView: Bool) -> [DataBrokerOperationsCollection] {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// DataBrokerProtectionSchedulerConfig.swift
// DataBrokerProtectionProcessorConfiguration.swift
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
//
Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ public final class DefaultDataBrokerProtectionScheduler: DataBrokerProtectionSch
captchaService: captchaService)

return DataBrokerProtectionProcessor(database: dataManager.database,
config: DataBrokerProtectionSchedulerConfig(),
operationRunnerProvider: runnerProvider,
notificationCenter: notificationCenter,
pixelHandler: pixelHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 0783a9c

Please sign in to comment.