@@ -13,7 +13,7 @@ public class Reporter {
1313 private let dispatchQueue = DispatchQueue ( label: " com.instana.ios.agent.reporter " , qos: . utility)
1414 internal var sendFirstBeacon = true // first beacon is sent all by itself, not in a batch
1515 private var slowSendStartTime : Date ?
16- private var inSlowModeBeforeFlush = false
16+ private var allowOneBackgroundFlush = false
1717 internal var lastFlushStartTime : Double ?
1818 internal var flusher : BeaconFlusher ?
1919 internal var send : BeaconFlusher . Sender ?
@@ -53,9 +53,11 @@ public class Reporter {
5353 self . updateNetworkConnection ( connectionType)
5454 }
5555 InstanaApplicationStateHandler . shared. listen { [ weak self] state, _ in
56- guard let self = self else { return }
5756 if state == . background, !ProcessInfo. isRunningTests {
58- // self.runBackgroundFlush()
57+ self ? . runBackgroundFlush ( )
58+ } else if state == . active {
59+ self ? . allowOneBackgroundFlush = false
60+ self ? . runActiveFlush ( )
5961 }
6062 }
6163 dispatchQueue. asyncAfter ( deadline: . now( ) + session. configuration. preQueueUsageTime, execute: emptyPreQueueIfNeeded)
@@ -141,23 +143,6 @@ public class Reporter {
141143 scheduleFlush ( )
142144 }
143145
144- internal var isInSlowSendMode : Bool {
145- session. configuration. slowSendInterval > 0 && ( sendFirstBeacon || slowSendStartTime != nil )
146- }
147-
148- internal func setSlowSendStartTime( _ time: Date ? ) {
149- if time == nil {
150- if slowSendStartTime != nil {
151- session. logger. add ( " Slow send ended at \( String ( describing: Date ( ) ) ) " )
152- slowSendStartTime = nil
153- }
154- } else if slowSendStartTime == nil {
155- // if slow send started, do not update so as to keep the earliest time
156- slowSendStartTime = time
157- session. logger. add ( " Slow send started at \( String ( describing: time!) ) " )
158- }
159- }
160-
161146 func checkDeviceCondition( ) -> InstanaError ? {
162147 // Check network and battery condition
163148 var error : InstanaError ?
@@ -202,29 +187,27 @@ public class Reporter {
202187 }
203188
204189 func scheduleFlush( ) {
205- guard !queue. items. isEmpty else { return }
190+ let flushableItemsAvailable = queue. flushableItemsAvailable ( )
191+
192+ let isBackground = InstanaApplicationStateHandler . shared. state == . background
193+ if isBackground, !allowOneBackgroundFlush {
194+ session. logger. add ( " Skip flush - app has been in background " , level: . debug)
195+ return
196+ }
197+ if isBackground {
198+ session. logger. add ( " App entered background, allowOneBackgroundFlush " , level: . debug)
199+ allowOneBackgroundFlush = false
200+ }
201+
202+ guard flushableItemsAvailable else { return }
206203
207204 if !canScheduleFlush( ) { return }
208205
209206 let start = Date ( )
210- var debounce : TimeInterval
207+ let debounce : TimeInterval = isBackground ? 0.0 : calcDebounceTime ( )
211208 var beaconsToSend : Set < CoreBeacon > = Set ( [ ] )
212- inSlowModeBeforeFlush = isInSlowSendMode
213- if inSlowModeBeforeFlush {
214- if sendFirstBeacon {
215- debounce = flushDebounce
216- sendFirstBeacon = false
217- } else {
218- debounce = session. configuration. slowSendInterval
219- }
220- var beacon = queue. items. first!
221- beacon. updateMetaDataWithSlowSendStartTime ( slowSendStartTime)
222- beaconsToSend. insert ( beacon)
223- } else {
224- debounce = calcDebounceTime ( )
225- // Filter out beacons already in-flight to prevent duplicate sends
226- beaconsToSend = queue. items. filter { $0. ifl != true }
227- }
209+ // Filter out beacons already in-flight to prevent duplicate sends
210+ beaconsToSend = queue. items. filter { $0. ifl != true }
228211
229212 guard !beaconsToSend. isEmpty else {
230213 session. logger. add ( " All beacons are in-flight, skipping flush " )
@@ -271,15 +254,17 @@ public class Reporter {
271254 }
272255
273256 func runBackgroundFlush( ) {
274- #if os(tvOS) || os(watchOS) || os(iOS)
275- ProcessInfo . processInfo. performExpiringActivity ( withReason: " BackgroundFlush " ) { expired in
276- guard !expired else { return }
277- self . dispatchQueue. async { [ weak self] in
278- guard let self = self else { return }
279- self . scheduleFlush ( )
280- }
281- }
282- #endif
257+ dispatchQueue. async { [ weak self] in
258+ self ? . allowOneBackgroundFlush = true
259+ self ? . scheduleFlush ( )
260+ }
261+ }
262+
263+ func runActiveFlush( ) {
264+ dispatchQueue. async { [ weak self] in
265+ self ? . session. logger. add ( " App entered foreground, resume regular flush " , level: . debug)
266+ self ? . scheduleFlush ( )
267+ }
283268 }
284269
285270 private func handle( flushResult: BeaconFlusher . Result ,
@@ -346,17 +331,6 @@ public class Reporter {
346331
347332 flusher = nil // mark this round flush done
348333 lastFlushStartTime = nil
349- if inSlowModeBeforeFlush {
350- // Another flush either resend 1 beacon (still in slow mode currently)
351- // or flush remaining beacons (got out of slow send mode already)
352- var msg : String
353- if isInSlowSendMode {
354- msg = " schedule flush to send 1 beacon in slow send mode "
355- } else {
356- msg = " flush all beacons after out of slow send mode "
357- }
358- session. logger. add ( msg)
359- }
360334 // schedule next round flush
361335 scheduleFlush ( )
362336 }
0 commit comments