Skip to content

Commit

Permalink
Use timestamp of last fetch instead of counting them
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g authored and valldrac committed Nov 8, 2023
1 parent 44d4a30 commit eeba13d
Showing 1 changed file with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object FcmFetchManager {
val WEBSOCKET_DRAIN_TIMEOUT = 5.minutes.inWholeMilliseconds

@Volatile
private var activeCount = 0
private var last = 0L

@Volatile
private var highPriority = false
Expand Down Expand Up @@ -92,7 +92,7 @@ object FcmFetchManager {
NotificationManagerCompat.from(context).cancel(NotificationIds.MAY_HAVE_MESSAGES_NOTIFICATION_ID)
}

private fun fetch(context: Context) {
private fun fetch(context: Context, now: Long) {
val hasHighPriorityContext = highPriority

val metricId = SignalLocalMetrics.PushWebsocketFetch.startFetch()
Expand All @@ -108,9 +108,8 @@ object FcmFetchManager {
}

synchronized(this) {
activeCount--

if (activeCount <= 0) {
if (last <= now) {
Log.i(TAG, "No more active. Stopping.")
context.stopService(Intent(context, FcmFetchBackgroundService::class.java))
FcmFetchForegroundService.stopServiceIfNecessary(context)
Expand All @@ -130,13 +129,9 @@ object FcmFetchManager {
if (highPriority) {
this.highPriority = true
}
val performedReplace = EXECUTOR.enqueue { fetch(context) }
if (performedReplace) {
Log.i(TAG, "Already have one running and one enqueued. Ignoring.")
} else {
activeCount++
Log.i(TAG, "Incrementing active count to $activeCount")
}
last = System.nanoTime()
Log.i(TAG, "Updating last event to $last")
EXECUTOR.enqueue { fetch(context, last) }
}
}

Expand Down

0 comments on commit eeba13d

Please sign in to comment.