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 1
1
package com.coder.gateway.sdk
2
2
3
3
import kotlinx.coroutines.delay
4
- import kotlinx.datetime.Clock
5
4
import java.util.Random
6
5
import java.util.concurrent.TimeUnit
7
- import kotlin.concurrent.timer
8
- import kotlin.math.max
9
6
import kotlin.math.min
10
7
11
8
/* *
@@ -27,18 +24,13 @@ suspend fun <T> suspendingRetryWithExponentialBackOff(
27
24
return action(attempt)
28
25
}
29
26
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)
39
33
}
40
- delay(delayMs)
41
- timer.cancel()
42
34
delayMs = min(delayMs * backOffFactor, backOffLimitMs) + (random.nextGaussian() * delayMs * backOffJitter).toLong()
43
35
}
44
36
}
You can’t perform that action at this time.
0 commit comments