Skip to content

Commit 73f9b19

Browse files
committed
Fix timer not canceling
It seems to operate in its own little world and I have no idea how to make it stop when the job running it has stopped.
1 parent 4bacfc4 commit 73f9b19

File tree

1 file changed

+6
-14
lines changed
  • src/main/kotlin/com/coder/gateway/sdk

1 file changed

+6
-14
lines changed

src/main/kotlin/com/coder/gateway/sdk/Retry.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.coder.gateway.sdk
22

33
import kotlinx.coroutines.delay
4-
import kotlinx.datetime.Clock
54
import java.util.Random
65
import java.util.concurrent.TimeUnit
7-
import kotlin.concurrent.timer
8-
import kotlin.math.max
96
import 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
}

0 commit comments

Comments
 (0)