diff --git a/scarlet/src/main/java/com/tinder/scarlet/internal/connection/Connection.kt b/scarlet/src/main/java/com/tinder/scarlet/internal/connection/Connection.kt index fea50ef6..39ca7346 100644 --- a/scarlet/src/main/java/com/tinder/scarlet/internal/connection/Connection.kt +++ b/scarlet/src/main/java/com/tinder/scarlet/internal/connection/Connection.kt @@ -106,15 +106,19 @@ internal class Connection( transitionTo(Connected(session = session)) } on() { - val backoffDuration = backoffStrategy.backoffDurationMillisAt(retryCount) - val timerDisposable = scheduleRetry(backoffDuration) - transitionTo( - WaitingToRetry( - timerDisposable = timerDisposable, - retryCount = retryCount, - retryInMillis = backoffDuration + if (backoffStrategy.shouldBackoff) { + val backoffDuration = backoffStrategy.backoffDurationMillisAt(retryCount) + val timerDisposable = scheduleRetry(backoffDuration) + transitionTo( + WaitingToRetry( + timerDisposable = timerDisposable, + retryCount = retryCount, + retryInMillis = backoffDuration + ) ) - ) + } else { + transitionTo(Disconnected) + } } } state { @@ -135,15 +139,19 @@ internal class Connection( transitionTo(Destroyed) } on() { - val backoffDuration = backoffStrategy.backoffDurationMillisAt(0) - val timerDisposable = scheduleRetry(backoffDuration) - transitionTo( - WaitingToRetry( - timerDisposable = timerDisposable, - retryCount = 0, - retryInMillis = backoffDuration + if (backoffStrategy.shouldBackoff) { + val backoffDuration = backoffStrategy.backoffDurationMillisAt(0) + val timerDisposable = scheduleRetry(backoffDuration) + transitionTo( + WaitingToRetry( + timerDisposable = timerDisposable, + retryCount = 0, + retryInMillis = backoffDuration + ) ) - ) + } else { + transitionTo(Disconnected) + } } } state { diff --git a/scarlet/src/main/java/com/tinder/scarlet/retry/BackoffStrategy.kt b/scarlet/src/main/java/com/tinder/scarlet/retry/BackoffStrategy.kt index 065089ac..c4933287 100644 --- a/scarlet/src/main/java/com/tinder/scarlet/retry/BackoffStrategy.kt +++ b/scarlet/src/main/java/com/tinder/scarlet/retry/BackoffStrategy.kt @@ -12,4 +12,6 @@ interface BackoffStrategy { * Returns a duration in milliseconds. */ fun backoffDurationMillisAt(retryCount: Int): Long + + var shouldBackoff: Boolean } diff --git a/scarlet/src/main/java/com/tinder/scarlet/retry/ExponentialBackoffStrategy.kt b/scarlet/src/main/java/com/tinder/scarlet/retry/ExponentialBackoffStrategy.kt index 41e8c0d4..c145486e 100644 --- a/scarlet/src/main/java/com/tinder/scarlet/retry/ExponentialBackoffStrategy.kt +++ b/scarlet/src/main/java/com/tinder/scarlet/retry/ExponentialBackoffStrategy.kt @@ -21,4 +21,6 @@ class ExponentialBackoffStrategy( initialDurationMillis.toDouble() * Math.pow(2.0, retryCount.toDouble()) ) .toLong() + + override var shouldBackoff: Boolean = true } diff --git a/scarlet/src/main/java/com/tinder/scarlet/retry/ExponentialWithJitterBackoffStrategy.kt b/scarlet/src/main/java/com/tinder/scarlet/retry/ExponentialWithJitterBackoffStrategy.kt index bdb0a7b0..66149e71 100644 --- a/scarlet/src/main/java/com/tinder/scarlet/retry/ExponentialWithJitterBackoffStrategy.kt +++ b/scarlet/src/main/java/com/tinder/scarlet/retry/ExponentialWithJitterBackoffStrategy.kt @@ -22,6 +22,8 @@ class ExponentialWithJitterBackoffStrategy( return duration.withJitter() } + override var shouldBackoff: Boolean = true + private fun Long.withJitter(): Long = (0..this).random() private fun ClosedRange.random() = random.nextInt((endInclusive - start).toInt()) + start diff --git a/scarlet/src/main/java/com/tinder/scarlet/retry/LinearBackoffStrategy.kt b/scarlet/src/main/java/com/tinder/scarlet/retry/LinearBackoffStrategy.kt index 2f4bfb76..872f8d64 100644 --- a/scarlet/src/main/java/com/tinder/scarlet/retry/LinearBackoffStrategy.kt +++ b/scarlet/src/main/java/com/tinder/scarlet/retry/LinearBackoffStrategy.kt @@ -13,4 +13,6 @@ class LinearBackoffStrategy( } override fun backoffDurationMillisAt(retryCount: Int): Long = durationMillis + + override var shouldBackoff: Boolean = true }