Skip to content

Commit

Permalink
调整Retry机制为:仅重试GET请求和paths中包含以get开头部分的请求。
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazunyang committed Jan 5, 2024
1 parent ee49e15 commit 21b141b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion http/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "cn.numeron"
version = "1.0.8"
version = "1.0.9"

tasks.withType<KotlinCompile> {
kotlinOptions {
Expand Down
16 changes: 15 additions & 1 deletion http/src/main/kotlin/cn/numeron/okhttp/RetryInterceptor.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.numeron.okhttp

import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

Expand All @@ -10,10 +11,23 @@ class RetryInterceptor(private val retryCount: Int = 2) : Interceptor {
return try {
chain.proceed(chain.request())
} catch (exception: IOException) {
retry(0, exception, chain)
if (isAllowRetry(chain.request())) {
retry(0, exception, chain)
} else throw exception
}
}

/** 是否是GET请求 */
private fun isAllowRetry(request: Request): Boolean {
if (request.method == "GET") {
return true
}
return request.url.pathSegments.any(::isGetUrl)
}

/** 是否是获取数据的url */
private fun isGetUrl(segment: String) = segment.startsWith("get")

private fun retry(count: Int, exception: IOException, chain: Interceptor.Chain): Response {
return if (count < retryCount) {
try {
Expand Down

0 comments on commit 21b141b

Please sign in to comment.