File tree Expand file tree Collapse file tree 3 files changed +5
-12
lines changed
data/src/main/java/com/idle/data/repository
network/src/main/java/com/idle/network Expand file tree Collapse file tree 3 files changed +5
-12
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import com.idle.network.model.chat.ReadMessageRequest
1212import com.idle.network.model.chat.SendMessageRequest
1313import com.idle.network.source.ChatDataSource
1414import com.idle.network.util.MAX_RETRY_ATTEMPTS
15- import com.idle.network.util.calculateEqualJitter
15+ import com.idle.network.util.calculateBackoffTime
1616import kotlinx.coroutines.delay
1717import kotlinx.coroutines.flow.Flow
1818import kotlinx.coroutines.flow.map
@@ -207,7 +207,7 @@ class ChatRepositoryImpl @Inject constructor(
207207 }.retryWhen { cause, attempt ->
208208 if (cause is IOException && attempt < MAX_RETRY_ATTEMPTS ) {
209209 connectWebSocket()
210- delay(calculateEqualJitter (attempt.toInt()))
210+ delay(calculateBackoffTime (attempt.toInt()))
211211 true
212212 } else {
213213 false
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ import com.idle.network.model.chat.SendMessageRequest
1414import com.idle.network.serializer.ChatResponseSerializer
1515import com.idle.network.util.MAX_RETRY_ATTEMPTS
1616import com.idle.network.util.MAX_WAIT_TIME
17- import com.idle.network.util.calculateEqualJitter
17+ import com.idle.network.util.calculateBackoffTime
1818import com.idle.network.util.safeApiCall
1919import kotlinx.coroutines.delay
2020import kotlinx.coroutines.flow.Flow
@@ -87,7 +87,7 @@ class ChatDataSource @Inject constructor(
8787 connectionAttempts = 0
8888 }.recoverCatching { throwable ->
8989 if (connectionAttempts < MAX_RETRY_ATTEMPTS ) {
90- val waitTime = minOf(calculateEqualJitter (connectionAttempts), MAX_WAIT_TIME )
90+ val waitTime = minOf(calculateBackoffTime (connectionAttempts), MAX_WAIT_TIME )
9191 delay(waitTime)
9292 connectionAttempts++
9393 connectWebSocket().getOrThrow()
Original file line number Diff line number Diff line change @@ -2,15 +2,8 @@ package com.idle.network.util
22
33import com.idle.domain.model.CountDownTimer.Companion.TICK_INTERVAL
44import kotlin.math.pow
5- import kotlin.random.Random
65
76const val MAX_RETRY_ATTEMPTS = 5
87const val MAX_WAIT_TIME = 10_000L
98
10- fun calculateEqualJitter (attempt : Int ): Long {
11- val baseDelay = calculateBackoffTime(attempt)
12- val randomJitter = Random .nextLong(0 , baseDelay / 2 + 1 )
13- return baseDelay / 2 + randomJitter
14- }
15-
16- private fun calculateBackoffTime (attempt : Int ): Long = (2.0 .pow(attempt) * TICK_INTERVAL ).toLong()
9+ fun calculateBackoffTime (attempt : Int ): Long = (2.0 .pow(attempt) * TICK_INTERVAL ).toLong()
You can’t perform that action at this time.
0 commit comments