Skip to content

Commit

Permalink
fix: add repeat interval on EventPipeline to prevent high-frequency r…
Browse files Browse the repository at this point in the history
…equests on continuing failing
  • Loading branch information
sojingle committed Dec 16, 2024
1 parent 2f16421 commit 7985e4d
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions Sources/Amplitude/Utilities/EventPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import Foundation

public class EventPipeline {
static let MAX_CONTINOUS_FAILURE_COUNT: Int = 6
static let MAX_RETRY_INTERVAL: TimeInterval = 60

var httpClient: HttpClient
let storage: Storage?
let logger: (any Logger)?
let configuration: Configuration
var continuousFailure: Int = 0

@Atomic internal var eventCount: Int = 0
internal var flushTimer: QueueTimer?
Expand Down Expand Up @@ -101,13 +105,35 @@ public class EventPipeline {
eventsString: eventsString
)
responseHandler.handle(result: result)
// Don't send the next event file if we're being deallocated
self.uploadsQueue.async { [weak self] in
guard let self = self else {
return
}

switch result {
case .success:
self.continuousFailure = 0
case .failure:
self.continuousFailure += 1
}

if self.continuousFailure > Self.MAX_CONTINOUS_FAILURE_COUNT {
self.currentUpload = nil
self.sendNextEventFile()
self.configuration.offline = true
self.logger?.log(message: "Request failed more than \(Self.MAX_CONTINOUS_FAILURE_COUNT) times, marking offline")
} else {
// Don't send the next event file if we're being deallocated
let nextFileBlock: () -> Void = { [weak self] in
guard let self = self else {
return
}
self.currentUpload = nil
self.sendNextEventFile()
}

if self.continuousFailure == 0 {
self.uploadsQueue.async(execute: nextFileBlock)
} else {
let sendingInterval = min(Self.MAX_RETRY_INTERVAL, pow(2, Double(self.continuousFailure)))
self.uploadsQueue.asyncAfter(deadline: .now() + sendingInterval, execute: nextFileBlock)
self.logger?.debug(message: "Request failed \(self.continuousFailure) times, send next event file in \(sendingInterval) seconds")
}
}
}
}
Expand Down

0 comments on commit 7985e4d

Please sign in to comment.