Store dummy eventSource when setting one failure

This commit is contained in:
sim 2024-12-02 09:10:10 +00:00
parent 2e1e309ebb
commit b8012ae2af
1 changed files with 18 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicReference
import okhttp3.Request
import okhttp3.sse.EventSource
import org.unifiedpush.distributor.nextpush.AppCompanion
import org.unifiedpush.distributor.nextpush.utils.DisconnectedNotification
@ -97,12 +98,27 @@ object FailureCounter {
*
* Used when there is no Internet access, or we want to restart the sync request to ack messages.
*
* The eventSource isn't closed, else it would call _onFailure_ and try to restart, which will
* be redundant
* We replace the eventSource with a dummy eventSource because when closing, this source will call _onFailure_
* and we don't want to add a new failure when it does.
*/
fun setFailOnce() {
Log.d(TAG, "setFailOnce")
Atomics.nFails.set(1)
val dummyEventSource = getDummyEventSource()
Atomics.eventSource.getAndSet(dummyEventSource)?.cancel()
}
/**
* Return dummy event source, used when we don't want the next fail to be
* counted
*/
private fun getDummyEventSource(): EventSource {
return object : EventSource {
override fun cancel() {}
override fun request(): Request {
return Request.Builder().build()
}
}
}
fun clearFails() {