Unverified Commit 6418ee96 authored by Yang Libin's avatar Yang Libin Committed by GitHub

feat: support retry times (#49)

parent b06cbce1
......@@ -23,10 +23,10 @@ public class HttpUtil {
private static final String DEFAULT_USER_AGENT = DEFAULT_CONFIG.getUserAgent();
private static final OkHttpClient DEFAULT_CLIENT = new OkHttpClient.Builder()
.retryOnConnectionFailure(DEFAULT_CONFIG.getMaxRetries() > 0)
.connectTimeout(DEFAULT_CONFIG.getConnectTimeout(), TimeUnit.MILLISECONDS)
.readTimeout(DEFAULT_CONFIG.getReadTimeout(), TimeUnit.MILLISECONDS)
.writeTimeout(DEFAULT_CONFIG.getWriteTimeout(), TimeUnit.MILLISECONDS)
.addInterceptor(new RetryInterceptor(DEFAULT_CONFIG.getMaxRetries()))
.build();
private HttpUtil() {
......@@ -41,6 +41,7 @@ public class HttpUtil {
.readTimeout(config.getReadTimeout(), TimeUnit.MILLISECONDS)
.writeTimeout(config.getWriteTimeout(), TimeUnit.MILLISECONDS)
.retryOnConnectionFailure(config.getMaxRetries() > 0)
.addInterceptor(new RetryInterceptor(config.getMaxRetries()))
.build();
}
Map<String, String> headers = new HashMap<>(2);
......@@ -82,3 +83,29 @@ public class HttpUtil {
}
}
}
class RetryInterceptor implements Interceptor {
private int maxRetry;
RetryInterceptor(int maxRetry) {
this.maxRetry = maxRetry;
}
public int getMaxRetry() {
return maxRetry;
}
public void setMaxRetry(int maxRetry) {
this.maxRetry = maxRetry;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
for (int i = 0; i < maxRetry && !response.isSuccessful(); ++i) {
response = chain.proceed(request);
}
return response;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment