Coverage Summary for Class: DeliveryPauseManager (cloud.mindbox.mobile_sdk.managers)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| DeliveryPauseManager |
100%
(8/8)
|
100%
(14/14)
|
100%
(25/25)
|
100%
(144/144)
|
| DeliveryPauseManager$Companion |
0%
(0/2)
|
|
0%
(0/2)
|
0%
(0/2)
|
| DeliveryPauseManager$OperationState |
100%
(1/1)
|
|
100%
(3/3)
|
100%
(12/12)
|
| DeliveryPauseManager$WhenMappings |
|
| Total |
81.8%
(9/11)
|
100%
(14/14)
|
93.3%
(28/30)
|
98.7%
(156/158)
|
package cloud.mindbox.mobile_sdk.managers
import cloud.mindbox.mobile_sdk.models.AsyncEventSendResult
import cloud.mindbox.mobile_sdk.models.EventType
import cloud.mindbox.mobile_sdk.models.Milliseconds
internal class DeliveryPauseManager(
private val monotonicNow: () -> Milliseconds,
) {
private companion object {
val FIRST_PAUSE = Milliseconds(180_000L)
val MAX_PAUSE = Milliseconds(3_600_000L)
}
private class OperationState(
val pausedUntilTick: Milliseconds,
val nextPause: Milliseconds,
)
private val states = mutableMapOf<String, OperationState>()
@Volatile
var generation: Int = 0
private set
@Synchronized
fun remainingPause(eventType: EventType): Milliseconds? = remainingPause(eventType.deliveryKey)
@Synchronized
fun wasThrottled(eventType: EventType): Boolean = states.containsKey(eventType.deliveryKey)
@Synchronized
fun register(eventType: EventType, result: AsyncEventSendResult, sentInGeneration: Int): Milliseconds? {
if (sentInGeneration != generation) return null
val key = eventType.deliveryKey
if (remainingPause(key) != null) return null
return when (result) {
AsyncEventSendResult.DELIVERED, AsyncEventSendResult.DISCARDED -> {
states.remove(key)
null
}
AsyncEventSendResult.THROTTLED -> {
val pause = states[key]?.nextPause ?: FIRST_PAUSE
states[key] = OperationState(
pausedUntilTick = Milliseconds(monotonicNow().interval + pause.interval),
nextPause = Milliseconds(minOf(pause.interval * 2, MAX_PAUSE.interval)),
)
pause
}
AsyncEventSendResult.RETRY_LATER -> null
}
}
@Synchronized
fun reset() {
states.clear()
generation++
}
private fun remainingPause(key: String): Milliseconds? {
val pausedUntilTick = states[key]?.pausedUntilTick ?: return null
val remaining = Milliseconds(pausedUntilTick.interval - monotonicNow().interval)
return remaining.takeIf { it.interval > 0 }
}
}
internal val EventType.deliveryKey: String
get() = "$endpoint $operation"