1
1
mirror of https://github.com/Pygmalion69/OpenTopoMapViewer.git synced 2025-10-06 00:02:42 +02:00

Retry on 429 Too Many Requests

This commit is contained in:
Pygmalion69
2025-08-29 17:25:11 +02:00
parent d56e1b1797
commit 669b933ef6

View File

@@ -36,6 +36,32 @@ object OpenRouteServiceRestClient {
chain.proceed(newRequest)
}
// Retry on 429 Too Many Requests with simple backoff honoring Retry-After when present
.addInterceptor { chain ->
val maxAttempts = 3
var waitMs = 500L
var req = chain.request()
repeat(maxAttempts) { attempt ->
val response = chain.proceed(req)
if (response.code != 429 || attempt == maxAttempts - 1) {
return@addInterceptor response
}
val retryAfter = response.header("Retry-After")
response.close()
val delayMs = retryAfter?.toLongOrNull()?.times(1000) ?: waitMs
try {
Thread.sleep(delayMs)
} catch (_: InterruptedException) {
return@addInterceptor chain.proceed(req)
}
waitMs *= 2
}
// compiler sees this as unreachable
error("Unexpected state: retry loop exited without returning")
}
// Increase timeouts to accommodate heavier endpoints like POIs
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)