Skip to content

Commit

Permalink
Prepare release 1.6.3
Browse files Browse the repository at this point in the history
Fix duplicated beacons issue
  • Loading branch information
Hongyan Jiang authored and GitHub Enterprise committed Jul 10, 2023
1 parent 5c5cd75 commit 72c9c1e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.6.3
- Fix duplicated beacons issue

## 1.6.2
- [Tech Preview] Add sample code in InstanaAgentExample app on how to enable crash reporting to Instana backend
- Once in slow send mode, periodically resend 1 beacon. Once out of slow send mode, flush all beacons immediately.
Expand Down
2 changes: 1 addition & 1 deletion InstanaAgent.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "InstanaAgent"
s.version = "1.6.2"
s.version = "1.6.3"
s.summary = "Instana iOS agent."

# This description is used to generate tags and improve search results.
Expand Down
59 changes: 48 additions & 11 deletions Sources/InstanaAgent/Beacons/Reporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Reporter {
private var sendFirstBeacon = true // first beacon is sent all by itself, not in a batch
private var slowSendStartTime: Date?
private var inSlowModeBeforeFlush = false
private var lastFlushStartTime: Double?
private var flusher: BeaconFlusher?
internal var send: BeaconFlusher.Sender?
private let rateLimiter: ReporterRateLimiter
Expand Down Expand Up @@ -128,8 +129,34 @@ public class Reporter {
}
}

func canScheduleFlush() -> Bool {
if flusher == nil {
return true
}
if lastFlushStartTime == nil {
return true
}

var maxFlushingTimeAllowed = 10.0 // in seconds
if isInSlowSendMode {
maxFlushingTimeAllowed += session.configuration.slowSendInterval
}

let diff = Date().timeIntervalSince1970 - lastFlushStartTime!
if diff > maxFlushingTimeAllowed {
// Previous flushing takes too long, force a new flush to prevent
// too many beacons accumulated locally thus lead to beacon loss.
session.logger.add("Previous flushing takes more than \(diff) seconds. Force another flushing now")
return true
}
return false
}

func scheduleFlush() {
guard !queue.items.isEmpty else { return }

if !canScheduleFlush() { return }

let start = Date()
var debounce: TimeInterval
let connectionType = networkUtility.connectionType
Expand Down Expand Up @@ -162,10 +189,14 @@ public class Reporter {
config: session.configuration, queue: dispatchQueue,
send: send) { [weak self] result in
guard let self = self else { return }
self.handle(flushResult: result, start)
self.dispatchQueue.async { [weak self] in
guard let self = self else { return }
self.handle(flushResult: result, start, fromBeaconFlusherCompletion: true)
}
}
flusher.schedule()
self.flusher = flusher
lastFlushStartTime = Date().timeIntervalSince1970
}

func runBackgroundFlush() {
Expand All @@ -180,7 +211,9 @@ public class Reporter {
#endif
}

private func handle(flushResult: BeaconFlusher.Result, _ start: Date = Date()) {
private func handle(flushResult: BeaconFlusher.Result,
_ start: Date = Date(),
fromBeaconFlusherCompletion: Bool = false) {
let result: BeaconResult
let errors = flushResult.errors
let sent = flushResult.sentBeacons
Expand All @@ -201,16 +234,20 @@ public class Reporter {
self.completionHandler.forEach { $0(result) }
}

if inSlowModeBeforeFlush {
// Another flush either resend 1 beacon (still in slow mode currently)
// or flush remaing beacons (got out of slow send mode already)
var msg: String
if isInSlowSendMode {
msg = "schedule flush to send 1 beacon in slow send mode"
} else {
msg = "flush all beacons after out of slow send mode"
if fromBeaconFlusherCompletion {
flusher = nil // mark this round flush done
if inSlowModeBeforeFlush {
// Another flush either resend 1 beacon (still in slow mode currently)
// or flush remaining beacons (got out of slow send mode already)
var msg: String
if isInSlowSendMode {
msg = "schedule flush to send 1 beacon in slow send mode"
} else {
msg = "flush all beacons after out of slow send mode"
}
session.logger.add(msg)
}
session.logger.add(msg)
// schedule next round flush
scheduleFlush()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/InstanaAgent/Configuration/VersionConfig.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
struct VersionConfig {
static let agentVersion = "1.6.2"
static let agentVersion = "1.6.3"
}
2 changes: 1 addition & 1 deletion Tests/InstanaAgentTests/Beacons/ReporterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class ReporterTests: InstanaTestCase {

// Then - Should only flush once when getting more beacons before flushing occured
AssertEqualAndNotZero(sendCount, 1)
AssertTrue(reporter.queue.items.isEmpty)
AssertEqualAndNotNil(reporter.queue.items.count, 5)
}

func test_schedule_and_flush_twice() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class InstanaSystemUtilsTests: InstanaTestCase {

func test_AgentVersion() {
// Then
AssertTrue(InstanaSystemUtils.agentVersion == "1.6.2")
AssertTrue(InstanaSystemUtils.agentVersion == "1.6.3")
}

func test_systemVersion() {
Expand Down

0 comments on commit 72c9c1e

Please sign in to comment.