Skip to content

Commit

Permalink
Implement exponential backoff for optout retries
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaemepereira committed Jun 5, 2024
1 parent 62d2059 commit a847bab
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct OperationPreferredDateCalculator {
return currentPreferredRunDate
}
case .error:
return date.now.addingTimeInterval(schedulingConfig.retryError.hoursToSeconds)
return date.now.addingTimeInterval(calculateNextRunDate(schedulingConfig: schedulingConfig, historyEvents: historyEvents))
case .optOutStarted, .scanStarted, .noMatchFound:
return currentPreferredRunDate
case .optOutConfirmed, .optOutRequested:
Expand All @@ -97,4 +97,10 @@ struct OperationPreferredDateCalculator {
let lastRemovalEventDate = lastRemovalEvent.date.addingTimeInterval(schedulingConfig.maintenanceScan.hoursToSeconds)
return lastRemovalEventDate < Date()
}

private func calculateNextRunDate(schedulingConfig: DataBrokerScheduleConfig,
historyEvents: [HistoryEvent]) -> TimeInterval {
let pastTries = historyEvents.filter { $0.isError }.count
return min(Int(pow(2.0, Double(pastTries))), schedulingConfig.retryError).hoursToSeconds
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import XCTest
final class OperationPreferredDateCalculatorTests: XCTestCase {

private let schedulingConfig = DataBrokerScheduleConfig(
retryError: 1000,
retryError: 48,
confirmOptOutScan: 2000,
maintenanceScan: 3000
)
Expand Down Expand Up @@ -322,17 +322,89 @@ final class OperationPreferredDateCalculatorTests: XCTestCase {
XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}

func testError_thenOptOutDateIsRetry() throws {
let expectedOptOutDate = Date().addingTimeInterval(schedulingConfig.retryError.hoursToSeconds)

func testWhenOptOutFailedOnce_thenWeRetryInTwoHours() throws {
let expectedOptOutDate = Calendar.current.date(byAdding: .hour, value: 2, to: Date())!
let historyEvents = [
HistoryEvent(extractedProfileId: 1,
brokerId: 1,
profileQueryId: 1,
type: .error(error: DataBrokerProtectionError.malformedURL))]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
schedulingConfig: schedulingConfig)

XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}

func testWhenOptOutFailedTwice_thenWeRetryInFourHours() throws {
let expectedOptOutDate = Calendar.current.date(byAdding: .hour, value: 4, to: Date())!
let historyEvents: [HistoryEvent] = [
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL))
]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
schedulingConfig: schedulingConfig)

XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}

func testWhenOptOutFailedThreeTimes_thenWeRetryInEightHours() throws {
let expectedOptOutDate = Calendar.current.date(byAdding: .hour, value: 8, to: Date())!
let historyEvents: [HistoryEvent] = [
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL))
]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
schedulingConfig: schedulingConfig)

XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}

func testWhenOptOutFailedSixTimes_thenWeRetryInTwoDays() throws {
let expectedOptOutDate = Calendar.current.date(byAdding: .day, value: 2, to: Date())!
let historyEvents: [HistoryEvent] = [
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL))
]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
schedulingConfig: schedulingConfig)

XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}

func testWhenOptOutFailedMoreThanTheThreshold_thenWeRetryAtTheSchedulingRetry() throws {
let expectedOptOutDate = Date().addingTimeInterval(schedulingConfig.retryError.hoursToSeconds)
let historyEvents: [HistoryEvent] = [
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL))
]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
Expand Down

0 comments on commit a847bab

Please sign in to comment.