@@ -4,12 +4,9 @@ import com.intellij.openapi.diagnostic.Logger
4
4
import com.intellij.openapi.progress.ProcessCanceledException
5
5
import com.intellij.ssh.SshException
6
6
import kotlinx.coroutines.delay
7
- import kotlinx.datetime.Clock
8
7
import java.util.Random
9
8
import java.util.concurrent.TimeUnit
10
- import kotlin.concurrent.timer
11
9
import kotlin.coroutines.cancellation.CancellationException
12
- import kotlin.math.max
13
10
import kotlin.math.min
14
11
15
12
fun unwrap (ex : Exception ): Throwable ? {
@@ -66,19 +63,14 @@ suspend fun <T> suspendingRetryWithExponentialBackOff(
66
63
update(attempt, unwrappedEx, null )
67
64
return null
68
65
}
69
- val end = Clock .System .now().toEpochMilliseconds() + delayMs
70
- val timer = timer(period = TimeUnit .SECONDS .toMillis(1 )) {
71
- val now = Clock .System .now().toEpochMilliseconds()
72
- val next = max(end - now, 0 )
73
- if (next > 0 ) {
74
- update(attempt, unwrappedEx, next)
75
- } else {
76
- cancel()
77
- }
78
- }
79
66
logger.error(" Failed to $label (attempt $attempt ; will retry in $delayMs ms)" , originalEx)
80
- delay(delayMs)
81
- timer.cancel()
67
+ var remainingMs = delayMs
68
+ while (remainingMs > 0 ) {
69
+ update(attempt, unwrappedEx, remainingMs)
70
+ val next = min(remainingMs, TimeUnit .SECONDS .toMillis(1 ))
71
+ remainingMs - = next
72
+ delay(next)
73
+ }
82
74
delayMs = min(delayMs * backOffFactor, backOffLimitMs) + (random.nextGaussian() * delayMs * backOffJitter).toLong()
83
75
}
84
76
}
0 commit comments