File tree Expand file tree Collapse file tree 1 file changed +6
-14
lines changed
src/main/kotlin/com/coder/gateway/sdk Expand file tree Collapse file tree 1 file changed +6
-14
lines changed Original file line number Diff line number Diff line change 11package com.coder.gateway.sdk
22
33import kotlinx.coroutines.delay
4- import kotlinx.datetime.Clock
54import java.util.Random
65import java.util.concurrent.TimeUnit
7- import kotlin.concurrent.timer
8- import kotlin.math.max
96import kotlin.math.min
107
118/* *
@@ -27,18 +24,13 @@ suspend fun <T> suspendingRetryWithExponentialBackOff(
2724 return action(attempt)
2825 }
2926 catch (e: Exception ) {
30- val end = Clock .System .now().toEpochMilliseconds() + delayMs
31- val timer = timer(period = TimeUnit .SECONDS .toMillis(1 )) {
32- val now = Clock .System .now().toEpochMilliseconds()
33- val next = max(end - now, 0 )
34- if (next > 0 ) {
35- update(attempt, next, e)
36- } else {
37- this .cancel()
38- }
27+ var remainingMs = delayMs
28+ while (remainingMs > 0 ) {
29+ update(attempt, remainingMs, e)
30+ val next = min(remainingMs, TimeUnit .SECONDS .toMillis(1 ))
31+ remainingMs - = next
32+ delay(next)
3933 }
40- delay(delayMs)
41- timer.cancel()
4234 delayMs = min(delayMs * backOffFactor, backOffLimitMs) + (random.nextGaussian() * delayMs * backOffJitter).toLong()
4335 }
4436 }
You can’t perform that action at this time.
0 commit comments