提交 2be29405 编写于 作者: 如梦技术's avatar 如梦技术 🐛

mica-http retry 添加对结果集校验.

上级 c305587a
......@@ -36,6 +36,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
/**
* ok http 封装,请求结构体
......@@ -410,11 +411,21 @@ public class HttpRequest {
return this;
}
public HttpRequest retryOn(Predicate<ResponseSpec> respPredicate) {
this.retryPolicy = new RetryPolicy(respPredicate);
return this;
}
public HttpRequest retry(int maxAttempts, long sleepMillis) {
this.retryPolicy = new RetryPolicy(maxAttempts, sleepMillis);
return this;
}
public HttpRequest retry(int maxAttempts, long sleepMillis, Predicate<ResponseSpec> respPredicate) {
this.retryPolicy = new RetryPolicy(maxAttempts, sleepMillis);
return this;
}
public HttpRequest hostnameVerifier(HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
return this;
......
......@@ -20,11 +20,13 @@ import lombok.RequiredArgsConstructor;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import java.io.IOException;
import java.util.function.Predicate;
/**
* 重试拦截器,应对代理问题
......@@ -39,7 +41,21 @@ public class RetryInterceptor implements Interceptor {
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
RetryTemplate template = createRetryTemplate(retryPolicy);
return template.execute(context -> chain.proceed(request));
return template.execute(context -> {
Response response = chain.proceed(request);
// 结果集校验
Predicate<ResponseSpec> respPredicate = retryPolicy.getRespPredicate();
if (respPredicate == null) {
return response;
}
// copy 一份 body
ResponseBody body = response.peekBody(Long.MAX_VALUE);
boolean tested = respPredicate.test(new HttpResponse(response));
if (tested) {
throw new IOException("Http Retry ResponsePredicate test Failure.");
}
return response.newBuilder().body(body).build();
});
}
private static RetryTemplate createRetryTemplate(RetryPolicy policy) {
......
......@@ -20,6 +20,9 @@ import lombok.Getter;
import lombok.ToString;
import org.springframework.retry.policy.SimpleRetryPolicy;
import javax.annotation.Nullable;
import java.util.function.Predicate;
/**
* 重试策略
*
......@@ -32,13 +35,24 @@ public class RetryPolicy {
private final int maxAttempts;
private final long sleepMillis;
@Nullable
private final Predicate<ResponseSpec> respPredicate;
public RetryPolicy() {
this(SimpleRetryPolicy.DEFAULT_MAX_ATTEMPTS, 0L);
this(null);
}
public RetryPolicy(int maxAttempts, long sleepMillis) {
this(maxAttempts, sleepMillis, null);
}
public RetryPolicy(@Nullable Predicate<ResponseSpec> respPredicate) {
this(SimpleRetryPolicy.DEFAULT_MAX_ATTEMPTS, 0L, respPredicate);
}
public RetryPolicy(int maxAttempts, long sleepMillis, @Nullable Predicate<ResponseSpec> respPredicate) {
this.maxAttempts = maxAttempts;
this.sleepMillis = sleepMillis;
this.respPredicate = respPredicate;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册