From 747beb762071ae6fde9b3e388c3bbd6da0013201 Mon Sep 17 00:00:00 2001 From: sim Date: Thu, 31 Aug 2023 08:10:30 +0200 Subject: [PATCH] Restart only if the fail comes from current source --- .../distributor/nextpush/api/SSEListener.kt | 39 ++++++++++--------- .../nextpush/services/FailureHandler.kt | 18 ++++++--- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt index dab71be..4883777 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt @@ -101,9 +101,10 @@ class SSEListener(val context: Context) : EventSourceListener() { Log.d(TAG, "onClosed: $eventSource") eventSource.cancel() if (!shouldRestart()) return - FailureHandler.newFail(context, eventSource) - clearVars() - RestartWorker.run(context, delay = 0) + if (FailureHandler.newFail(context, eventSource)) { + clearVars() + RestartWorker.run(context, delay = 0) + } } override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) { @@ -118,23 +119,25 @@ class SSEListener(val context: Context) : EventSourceListener() { } if (!AppCompanion.hasInternet.get()) { Log.d(TAG, "No Internet: do not restart") - FailureHandler.once(eventSource) - clearVars() + if (FailureHandler.once(eventSource)) { + clearVars() + } return } - FailureHandler.newFail(context, eventSource) - clearVars() - val delay = when (FailureHandler.nFails()) { - 1 -> 2 // 2sec - 2 -> 5 // 5sec - 3 -> 20 // 20sec - 4 -> 60 // 1min - 5 -> 300 // 5min - 6 -> 600 // 10min - else -> return // else keep the worker with its 16min - }.toLong() - Log.d(TAG, "Retrying in $delay s") - RestartWorker.run(context, delay = delay) + if (FailureHandler.newFail(context, eventSource)) { + clearVars() + val delay = when (FailureHandler.nFails()) { + 1 -> 2 // 2sec + 2 -> 5 // 5sec + 3 -> 20 // 20sec + 4 -> 60 // 1min + 5 -> 300 // 5min + 6 -> 600 // 10min + else -> return // else keep the worker with its 16min + }.toLong() + Log.d(TAG, "Retrying in $delay s") + RestartWorker.run(context, delay = delay) + } } private fun shouldRestart(): Boolean { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt index b51cbdc..5302181 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt @@ -18,7 +18,7 @@ object FailureHandler { // This is the last eventSource opened private val eventSource: AtomicReference = AtomicReference(null) - private fun isRightEventSource(eventSource: EventSource?): Boolean { + private fun isCurrentEventSource(eventSource: EventSource?): Boolean { return this.eventSource.get()?.let { it == eventSource } ?: true @@ -39,11 +39,12 @@ object FailureHandler { return nFails.get() } - fun newFail(context: Context, eventSource: EventSource?) { + // Returns true if the fail is from the current eventSource + fun newFail(context: Context, eventSource: EventSource?): Boolean { Log.d(TAG, "newFail/Eventsource: $eventSource") // ignore fails from a possible old eventSource // if we are already reconnected - if (isRightEventSource(eventSource)) { + return if (isCurrentEventSource(eventSource)) { Log.d(TAG, "EventSource is known or null") ttlFails.getAndIncrement() if (nFails.incrementAndGet() == 2) { @@ -56,21 +57,28 @@ object FailureHandler { } } this.eventSource.getAndSet(null)?.cancel() + true } else { + Log.d(TAG, "This is an old EventSource.") eventSource?.cancel() + false } } - fun once(eventSource: EventSource?) { + // Returns true if the fail is from the current eventSource + fun once(eventSource: EventSource?): Boolean { Log.d(TAG, "once/Eventsource: $eventSource") // ignore fails from a possible old eventSource // if we are already reconnected - if (isRightEventSource(eventSource)) { + return if (isCurrentEventSource(eventSource)) { Log.d(TAG, "EventSource is known or null") nFails.set(1) this.eventSource.getAndSet(null)?.cancel() + true } else { + Log.d(TAG, "This is an old EventSource.") eventSource?.cancel() + false } }