【发布时间】:2021-12-23 20:55:25
【问题描述】:
我有以下设置:带有 okHttp3 客户端的 spring boot 应用程序。我有一个自定义重试机制,需要在每种情况下重试调用有限的次数。拦截器如下: 弹簧靴:2.5.5 okhttp: 4.9.3
@Slf4j
@RequiredArgsConstructor
public class RetryOnFailureInterceptor implements Interceptor {
private final int maxRetryCount;
@Override
public Response intercept(final Chain chain) throws IOException {
final Request request = chain.request();
int tryCount = 0;
Response response = null;
// first call is actual call, following are first retries
while ((response == null || !response.isSuccessful()) && tryCount < this.maxRetryCount) {
tryCount++;
try {
response = chain.proceed(request);
if (response.isSuccessful()) {
return response;
}
// close response before retry
response.close();
log.info("Intercept, request failed, retry_count={}/{} ", tryCount, this.maxRetryCount);
} catch (final IOException ioException) {
log.info("Caught exception with message={}, retry_count={}/{} ",
ioException.getMessage(), tryCount, this.maxRetryCount);
if (response != null && response.body() != null) {
response.close();
}
}
}
// last try should proceed as is
return chain.proceed(request);
}
}
我想通过以下测试来测试拦截器的功能:
private MockWebServer mockWebServer;
@BeforeEach
void setUp() throws IOException {
this.mockWebServer = new MockWebServer();
this.mockWebServer.start(8080);
}
@AfterEach
void tearDown() throws IOException {
this.mockWebServer.shutdown();
}
@Test
void withRetryInterceptorsTest() throws IOException {
final OkHttpClient httpClient = new CustomOkHttpClientBuilder(getHttpSettings())
.withRetryInterceptor(5)
.build(); //custom client builder I made, it sets timeouts and the interceptor. Also it sets okhttpclient.retryOnConnectionFailure(false) to avoid duplicate retry mechanisms. Also enabling it does not fix it.
final MockResponse timeout1 = new MockResponse().setBodyDelay(20, TimeUnit.DAYS).setHeadersDelay(20, TimeUnit.DAYS);
final MockResponse timeout2 = new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE); //another way to timeout
final MockResponse failure = new MockResponse().setResponseCode(400);
final MockResponse success = new MockResponse().setResponseCode(200);
this.mockWebServer.enqueue(timeout1);
this.mockWebServer.enqueue(timeout2);
this.mockWebServer.enqueue(failure);
this.mockWebServer.enqueue(success);
final Request request = new Request.Builder()
.url("http://localhost:8080")
.get()
.build();
final Response response = httpClient.newCall(request).execute();
assertEquals(200, response.code());
}
当我运行测试时会发生这种情况:
21:47:05.337 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Socket closed, retry_count=1/5
21:47:05.341 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Canceled, retry_count=2/5
21:47:05.341 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Canceled, retry_count=3/5
21:47:05.342 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Canceled, retry_count=4/5
21:47:05.342 [main] INFO RetryOnFailureInterceptor - Caught exception with message=Canceled, retry_count=5/5
我正在尝试找出问题所在。是因为 mockwebserver 中发生的某些事情而无法正常工作,还是当连接超时时您无法重试,因为调用被取消,这是真正的预期行为?
我怀疑原因是在 mockwebserver 中,但我不知道如何解决它,或者问题出在哪里。
【问题讨论】:
标签: java spring-boot junit okhttp mockwebserver