Restart only if the fail comes from current source
This commit is contained in:
parent
5484ff6d59
commit
747beb7620
|
@ -101,10 +101,11 @@ class SSEListener(val context: Context) : EventSourceListener() {
|
|||
Log.d(TAG, "onClosed: $eventSource")
|
||||
eventSource.cancel()
|
||||
if (!shouldRestart()) return
|
||||
FailureHandler.newFail(context, eventSource)
|
||||
if (FailureHandler.newFail(context, eventSource)) {
|
||||
clearVars()
|
||||
RestartWorker.run(context, delay = 0)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) {
|
||||
Log.d(TAG, "onFailure")
|
||||
|
@ -118,11 +119,12 @@ class SSEListener(val context: Context) : EventSourceListener() {
|
|||
}
|
||||
if (!AppCompanion.hasInternet.get()) {
|
||||
Log.d(TAG, "No Internet: do not restart")
|
||||
FailureHandler.once(eventSource)
|
||||
if (FailureHandler.once(eventSource)) {
|
||||
clearVars()
|
||||
}
|
||||
return
|
||||
}
|
||||
FailureHandler.newFail(context, eventSource)
|
||||
if (FailureHandler.newFail(context, eventSource)) {
|
||||
clearVars()
|
||||
val delay = when (FailureHandler.nFails()) {
|
||||
1 -> 2 // 2sec
|
||||
|
@ -136,6 +138,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
|
|||
Log.d(TAG, "Retrying in $delay s")
|
||||
RestartWorker.run(context, delay = delay)
|
||||
}
|
||||
}
|
||||
|
||||
private fun shouldRestart(): Boolean {
|
||||
if (!StartService.isServiceStarted) {
|
||||
|
|
|
@ -18,7 +18,7 @@ object FailureHandler {
|
|||
// This is the last eventSource opened
|
||||
private val eventSource: AtomicReference<EventSource?> = 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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue