Skip to content

Commit b01e365

Browse files
Hongyan JiangGitHub Enterprise
authored andcommitted
check network/battery condition before beacon flush to avoid beacon accidentally get deleted (#98)
1 parent bd9af4d commit b01e365

6 files changed

Lines changed: 46 additions & 30 deletions

File tree

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 1.9.5
4+
- check network/battery condition before beacon flush to avoid beacon accidentally get deleted
5+
36
## 1.9.4
47
- add `deleteOldBeacons` feature to remove older beacons
58
- add `maxBeaconResendTries` to configure retry times on beacon send failure

InstanaAgent.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "InstanaAgent"
19-
s.version = "1.9.4"
19+
s.version = "1.9.5"
2020
s.summary = "Instana iOS agent."
2121

2222
# This description is used to generate tags and improve search results.

Sources/InstanaAgent/Beacons/BeaconFlusher.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class BeaconFlusher {
9595
}
9696

9797
private func flush() {
98+
if reporter?.deviceAllowFlush() == false {
99+
return
100+
}
98101
let batches = items.chunkedBeacons(size: config.maxBeaconsPerRequest)
99102
let disapatchGroup = DispatchGroup()
100103
batches.forEach { beaconBatch in

Sources/InstanaAgent/Beacons/Reporter.swift

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,30 @@ public class Reporter {
158158
}
159159
}
160160

161+
func deviceAllowFlush() -> Bool {
162+
// Check network and battery condition
163+
var error: Error?
164+
let connectionType = networkUtility.connectionType
165+
if connectionType == .none || connectionType == .undetermined {
166+
error = InstanaError.offline
167+
} else if suspendReporting.contains(.cellularConnection), connectionType == .cellular {
168+
error = InstanaError.noWifiAvailable
169+
} else if suspendReporting.contains(.lowBattery), !batterySafeForNetworking() {
170+
error = InstanaError.lowBattery
171+
}
172+
if error != nil {
173+
session.logger.add("Cannot send beacon: \(error!)", level: .warning)
174+
completionHandler.forEach { $0(BeaconResult.failure(error!)) }
175+
return false
176+
}
177+
return true
178+
}
179+
161180
func canScheduleFlush() -> Bool {
181+
if !deviceAllowFlush() {
182+
return false
183+
}
184+
162185
if flusher == nil {
163186
return true
164187
}
@@ -185,16 +208,6 @@ public class Reporter {
185208

186209
let start = Date()
187210
var debounce: TimeInterval
188-
let connectionType = networkUtility.connectionType
189-
guard connectionType != .none else {
190-
return handle(flushResult: .failure([InstanaError.offline]))
191-
}
192-
if suspendReporting.contains(.cellularConnection), connectionType == .cellular {
193-
return handle(flushResult: .failure([InstanaError.noWifiAvailable]))
194-
}
195-
if suspendReporting.contains(.lowBattery), !batterySafeForNetworking() {
196-
return handle(flushResult: .failure([InstanaError.lowBattery]))
197-
}
198211
var beacons: Set<CoreBeacon> = Set([])
199212
inSlowModeBeforeFlush = isInSlowSendMode
200213
if inSlowModeBeforeFlush {
@@ -218,7 +231,7 @@ public class Reporter {
218231
guard let self = self else { return }
219232
self.dispatchQueue.async { [weak self] in
220233
let originalBeacons: Set<CoreBeacon>? = (result.sentBeacons.count < beacons.count) ? beacons : nil
221-
self?.handle(flushResult: result, start, fromBeaconFlusherCompletion: true, originalBeacons: originalBeacons)
234+
self?.handle(flushResult: result, start, originalBeacons: originalBeacons)
222235
}
223236
}
224237
flusher.schedule()
@@ -253,7 +266,6 @@ public class Reporter {
253266

254267
private func handle(flushResult: BeaconFlusher.Result,
255268
_ start: Date = Date(),
256-
fromBeaconFlusherCompletion: Bool = false,
257269
originalBeacons: Set<CoreBeacon>? = nil) {
258270
let result: BeaconResult
259271
let errors = flushResult.errors
@@ -297,22 +309,20 @@ public class Reporter {
297309
}
298310
}
299311

300-
if fromBeaconFlusherCompletion {
301-
flusher = nil // mark this round flush done
302-
lastFlushStartTime = nil
303-
if inSlowModeBeforeFlush {
304-
// Another flush either resend 1 beacon (still in slow mode currently)
305-
// or flush remaining beacons (got out of slow send mode already)
306-
var msg: String
307-
if isInSlowSendMode {
308-
msg = "schedule flush to send 1 beacon in slow send mode"
309-
} else {
310-
msg = "flush all beacons after out of slow send mode"
311-
}
312-
session.logger.add(msg)
312+
flusher = nil // mark this round flush done
313+
lastFlushStartTime = nil
314+
if inSlowModeBeforeFlush {
315+
// Another flush either resend 1 beacon (still in slow mode currently)
316+
// or flush remaining beacons (got out of slow send mode already)
317+
var msg: String
318+
if isInSlowSendMode {
319+
msg = "schedule flush to send 1 beacon in slow send mode"
320+
} else {
321+
msg = "flush all beacons after out of slow send mode"
313322
}
314-
// schedule next round flush
315-
scheduleFlush()
323+
session.logger.add(msg)
316324
}
325+
// schedule next round flush
326+
scheduleFlush()
317327
}
318328
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
struct VersionConfig {
2-
static let agentVersion = "1.9.4"
2+
static let agentVersion = "1.9.5"
33
}

Tests/InstanaAgentTests/Configuration/InstanaSystemUtilsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class InstanaSystemUtilsTests: InstanaTestCase {
55

66
func test_AgentVersion() {
77
// Then
8-
AssertTrue(InstanaSystemUtils.agentVersion == "1.9.4")
8+
AssertTrue(InstanaSystemUtils.agentVersion == "1.9.5")
99
}
1010

1111
func test_systemVersion() {

0 commit comments

Comments
 (0)