Skip to content

Commit a5b9e74

Browse files
author
hexiaochun
committed
feat: optimize retry and exception
1 parent 49ea4a6 commit a5b9e74

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/exception/ArkHttpException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.gson.Gson;
44

55
public class ArkHttpException extends RuntimeException {
6+
public static Integer INTERNAL_SERVICE_CODE = 500;
67

78
public final int statusCode;
89

@@ -31,6 +32,7 @@ public String getMessage() {
3132
public String toString() {
3233
return "ArkHttpException{" +
3334
"statusCode=" + statusCode +
35+
", message='" + super.getMessage() + '\'' +
3436
", code='" + code + '\'' +
3537
", param='" + param + '\'' +
3638
", type='" + type + '\'' +

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/interceptor/RequestIdInterceptor.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.volcengine.ark.runtime.interceptor;
22

33
import com.volcengine.ark.runtime.Const;
4+
import com.volcengine.ark.runtime.exception.ArkAPIError;
5+
import com.volcengine.ark.runtime.exception.ArkException;
6+
import com.volcengine.ark.runtime.exception.ArkHttpException;
47
import com.volcengine.version.Version;
58
import okhttp3.Interceptor;
69
import okhttp3.Request;
@@ -26,7 +29,14 @@ public Response intercept(Chain chain) throws IOException {
2629
requestBuilder.header("User-Agent", getUserAgent());
2730

2831
Request request = requestBuilder.build();
29-
return chain.proceed(request);
32+
33+
try {
34+
return chain.proceed(request);
35+
} catch (Exception e) {
36+
String requestId = request.header(Const.CLIENT_REQUEST_HEADER);
37+
ArkAPIError arkAPIError = new ArkAPIError(new ArkAPIError.ArkErrorDetails(e.getMessage(), "", "", ""));
38+
throw new ArkHttpException(arkAPIError, e, ArkHttpException.INTERNAL_SERVICE_CODE, requestId);
39+
}
3040
}
3141

3242
private String genRequestId() {

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/interceptor/RetryInterceptor.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,31 @@ public RetryInterceptor(int retryTimes) {
2121
}
2222

2323
@Override
24-
public Response intercept(Chain chain) throws IOException {
24+
public Response intercept(Chain chain) throws RuntimeException, InterruptedIOException {
2525
Request request = chain.request();
2626

27-
// try the request
28-
Response response = chain.proceed(request);
29-
27+
Response response = null;
3028
int tryCount = 0;
31-
while ((response.code() >= 500 || response.code() == 429) && tryCount < retryTimes) {
32-
tryCount++;
29+
boolean shouldRetry;
30+
Exception exception;
31+
do {
32+
if (response != null) {
33+
response.close();
34+
}
35+
exception = null;
36+
37+
try {
38+
response = chain.proceed(request);
39+
shouldRetry = response.code() >= 500 || response.code() == 429;
40+
} catch (Exception e) {
41+
shouldRetry = true;
42+
exception = e;
43+
}
3344

34-
// retry the request
35-
response.close();
45+
tryCount++;
46+
if (!(shouldRetry && tryCount <= retryTimes)) {
47+
break;
48+
}
3649

3750
try {
3851
double interval = retryInterval(retryTimes, retryTimes - tryCount) * 1000;
@@ -41,10 +54,12 @@ public Response intercept(Chain chain) throws IOException {
4154
Thread.currentThread().interrupt();
4255
throw new InterruptedIOException();
4356
}
44-
response = chain.proceed(request);
45-
}
57+
} while (true);
4658

47-
return response;
59+
if (response != null) {
60+
return response;
61+
}
62+
throw new RuntimeException(exception);
4863
}
4964

5065
public double retryInterval(int max, int remain) {

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/service/ArkBaseService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ public abstract class ArkBaseService {
1717
public static final String BASE_REGION = "cn-beijing";
1818
public static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(10);
1919
public static final Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofMinutes(1);
20+
public static final int DEFAULT_RETRY_TIMES = 2;
2021
}

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/service/ArkService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public static OkHttpClient defaultApiKeyClient(String apiKey, Duration timeout)
100100
return new OkHttpClient.Builder()
101101
.addInterceptor(new AuthenticationInterceptor(apiKey))
102102
.addInterceptor(new RequestIdInterceptor())
103+
.addInterceptor(new RetryInterceptor(DEFAULT_RETRY_TIMES))
103104
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
104105
.readTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS)
105106
.build();
@@ -109,6 +110,7 @@ public static OkHttpClient defaultResourceStsClient(String ak, String sk, Durati
109110
return new OkHttpClient.Builder()
110111
.addInterceptor(new ArkResourceStsAuthenticationInterceptor(ak, sk, region))
111112
.addInterceptor(new RequestIdInterceptor())
113+
.addInterceptor(new RetryInterceptor(DEFAULT_RETRY_TIMES))
112114
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
113115
.readTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS)
114116
.build();
@@ -132,9 +134,7 @@ public static <T> T execute(Single<T> apiCall) {
132134
try {
133135
Headers headers = e.response().raw().request().headers();
134136
requestId = headers.get(Const.CLIENT_REQUEST_HEADER);
135-
} catch (Exception ignored) {
136-
137-
}
137+
} catch (Exception ignored) {}
138138

139139
try {
140140
if (e.response() == null || e.response().errorBody() == null) {
@@ -265,7 +265,7 @@ public static class Builder {
265265
private String baseUrl = BASE_URL;
266266
private Duration timeout = DEFAULT_TIMEOUT;
267267
private Duration connectTimeout = DEFAULT_CONNECT_TIMEOUT;
268-
private int retryTimes = 0;
268+
private int retryTimes = DEFAULT_RETRY_TIMES;
269269
private Proxy proxy;
270270
private ConnectionPool connectionPool;
271271
private Dispatcher dispatcher;

0 commit comments

Comments
 (0)