Skip to content

Commit 9b7716e

Browse files
Hongyan JiangGitHub Enterprise
authored andcommitted
fix duplicated beacon issue for slow network (#93)
1 parent d3724d8 commit 9b7716e

5 files changed

Lines changed: 36 additions & 6 deletions

File tree

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## 1.9.3
4+
- address dulpicated beacon issue for slow network
45
- crash beacon improvement
56

67
## 1.9.2

Sources/InstanaAgent/Beacons/Reporter.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public class Reporter {
151151
return true
152152
}
153153

154-
let maxFlushingTimeAllowed = 10.0 // in seconds
154+
let maxFlushingTimeAllowed = 100.0 // in seconds
155155

156156
let diff = Date().timeIntervalSince1970 - lastFlushStartTime!
157157
if diff > maxFlushingTimeAllowed {
@@ -193,7 +193,7 @@ public class Reporter {
193193
beacon.updateMetaDataWithSlowSendStartTime(slowSendStartTime)
194194
beacons.insert(beacon)
195195
} else {
196-
debounce = queue.isFull ? 0.0 : flushDebounce
196+
debounce = calcDebounceTime()
197197
beacons = queue.items
198198
}
199199
let flusher = BeaconFlusher(reporter: self, items: beacons, debounce: debounce,
@@ -207,7 +207,20 @@ public class Reporter {
207207
}
208208
flusher.schedule()
209209
self.flusher = flusher
210-
lastFlushStartTime = Date().timeIntervalSince1970
210+
lastFlushStartTime = Date().timeIntervalSince1970 + debounce
211+
}
212+
213+
internal func calcDebounceTime() -> TimeInterval {
214+
var debounce: TimeInterval = flushDebounce
215+
let itemsCount = queue.items.count
216+
if itemsCount >= (queue.maxItems * 4 / 5) {
217+
// if queue 80% full then flush immediately
218+
debounce = 0
219+
} else if itemsCount >= (queue.maxItems / 2) {
220+
// if queue 50% full then flush quicker than default waiting time
221+
debounce /= 2.0
222+
}
223+
return debounce
211224
}
212225

213226
func runBackgroundFlush() {

Sources/InstanaAgent/Utils/Extensions/UI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ extension UIViewController {
111111
let duration = (CACurrentMediaTime() - startTime) * 1000.0
112112
let classType = type(of: self)
113113
#if DEBUG
114-
print("DEBUG: \(Bundle(for: classType).bundleIdentifier!) classType=\(classType) viewDidAppear() \(duration) milliseconds")
114+
// print("DEBUG: \(Bundle(for: classType).bundleIdentifier!) classType=\(classType) viewDidAppear() \(duration) milliseconds")
115115
#endif
116116
objc_setAssociatedObject(self, &AssociatedKeys.viewLoadStartTime, nil, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
117117

Tests/InstanaAgentTests/Beacons/Beacon Types/DiagnosticBeaconTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class DiagnosticBeaconTests: InstanaTestCase {
4141
// Then
4242
AssertEqualAndNotNil(beacon.cti, "\(crashBeacon.crashTime)")
4343
AssertEqualAndNotNil(beacon.d, "\(crashBeacon.duration)")
44-
AssertEqualAndNotNil(beacon.ast, crashBeacon.crashPayload)
44+
// AssertEqualAndNotNil(beacon.ast, crashBeacon.crashPayload) // not sent since version 1.9.3
4545
AssertEqualAndNotNil(beacon.st, crashBeacon.formatted)
4646
AssertEqualAndNotNil(beacon.et, crashBeacon.errorType)
4747
AssertEqualAndNotNil(beacon.em, crashBeacon.errorMessage)
@@ -72,7 +72,7 @@ class DiagnosticBeaconTests: InstanaTestCase {
7272
let sut = beacon.asString
7373
// When
7474
let conn = InstanaSystemUtils.networkUtility.connectionType
75-
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nast\tmockCrashPayload\nav\t\(beacon.av)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncn\tmockCarrier\nct\tmockConnectionType\ncti\t\(crashTime)\nd\t0\ndma\tApple\ndmo\t\(beacon.dmo)\nem\tmockErrorType - crash terminationReason\net\tmockErrorType\nk\t\(key)\nm_cn\t\(conn.cellular.carrierName)\nm_ct\t\(conn.description)\nm_id\t\(mockSession.id)\nm_mg\t\(crashGroupID.uuidString)\nm_mt\tcrash\nm_sym\ttrue\nm_ver\t\(currentInstanaCrashPayloadVersion)\nosn\tiOS\nosv\t\(beacon.osv)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(crashSession.id)\nst\tmockSymbolicated\nt\tcrash\nti\t\(crashBeacon.timestamp)\nue\tmockEmail\nuf\tc,lm\nui\tmockUserID\nul\ten\nun\tmockUserName\nusi\t\(mockSession.usi!.uuidString)\nv\tmockViewName\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
75+
let expected = "ab\t\(beacon.ab)\nagv\t\(beacon.agv)\nav\t\(beacon.av)\nbi\t\(beacon.bi)\nbid\t\(beacon.bid)\ncn\tmockCarrier\nct\tmockConnectionType\ncti\t\(crashTime)\nd\t0\ndma\tApple\ndmo\t\(beacon.dmo)\nem\tmockErrorType - crash terminationReason\net\tmockErrorType\nk\t\(key)\nm_cn\t\(conn.cellular.carrierName)\nm_ct\t\(conn.description)\nm_id\t\(mockSession.id)\nm_mg\t\(crashGroupID.uuidString)\nm_mt\tcrash\nm_sym\ttrue\nm_ver\t\(currentInstanaCrashPayloadVersion)\nosn\tiOS\nosv\t\(beacon.osv)\np\tiOS\nro\t\(String(InstanaSystemUtils.isDeviceJailbroken))\nsid\t\(crashSession.id)\nst\tmockSymbolicated\nt\tcrash\nti\t\(crashBeacon.timestamp)\nue\tmockEmail\nuf\tc,lm\nui\tmockUserID\nul\ten\nun\tmockUserName\nusi\t\(mockSession.usi!.uuidString)\nv\tmockViewName\nvh\t\(Int(UIScreen.main.bounds.height))\nvw\t\(Int(UIScreen.main.bounds.width))"
7676
XCTAssertEqual(sut, expected)
7777
}
7878
}

Tests/InstanaAgentTests/Beacons/ReporterTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,22 @@ class ReporterTests: InstanaTestCase {
10771077
XCTAssertTrue(reporter.canScheduleFlush())
10781078
}
10791079

1080+
func test_calcDebounceTime_moreThanHalf() {
1081+
// Given
1082+
session.configuration.reporterSendDebounce = 2.0
1083+
let reporter = Reporter(session, batterySafeForNetworking: { true }, networkUtility: .wifi)
1084+
1085+
let beacons: [HTTPBeacon] = (0..<reporter.queue.maxItems/2 + 1).map { _ in HTTPBeacon.createMock() }
1086+
let corebeacons = try! CoreBeaconFactory(session).map(beacons)
1087+
reporter.queue.add(corebeacons)
1088+
1089+
// When
1090+
let debounce = reporter.calcDebounceTime()
1091+
1092+
// Then
1093+
XCTAssertTrue(debounce == 1.0)
1094+
}
1095+
10801096
func test_submit_and_flush_shouldNotCause_RetainCycle() {
10811097
// Given
10821098
let waitForCompletion = expectation(description: "waitForSend")

0 commit comments

Comments
 (0)