Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public class IpPortManager {

private boolean autoDiscoveryInited = false;

private int maxRetryTimes;

public int getMaxRetryTimes() {
return maxRetryTimes;
}

public IpPortManager(ServiceRegistryConfig serviceRegistryConfig, InstanceCacheManager instanceCacheManager) {
this.serviceRegistryConfig = serviceRegistryConfig;
this.instanceCacheManager = instanceCacheManager;
Expand All @@ -61,6 +67,7 @@ public IpPortManager(ServiceRegistryConfig serviceRegistryConfig, InstanceCacheM
}
int initialIndex = new Random().nextInt(defaultIpPort.size());
currentAvailableIndex = new AtomicInteger(initialIndex);
maxRetryTimes = defaultIpPort.size() + (getDiscoveredIpPort() == null ? 0 : getDiscoveredIpPort().size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not correct. maxRetryTimes is dynamically change and you can not getDiscoveryIpPort when initialize IpPortManager

}

// we have to do this operation after the first time setup has already done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public class RequestContext {

private RequestParam params;

// we can set max retry policies, now only try it twice
private boolean retry;
private int retryTimes = 0;

public IpPort getIpPort() {
return ipPort;
Expand Down Expand Up @@ -68,11 +67,12 @@ public void setParams(RequestParam params) {
this.params = params;
}

public boolean isRetry() {
return retry;

public int getRetryTimes() {
return retryTimes;
}

public void setRetry(boolean retry) {
this.retry = retry;
public void incrementRetryTimes() {
++this.retryTimes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void init() {
private void retry(RequestContext requestContext, Handler<RestResponse> responseHandler) {
LOGGER.warn("invoke service [{}] failed, retry.", requestContext.getUri());
requestContext.setIpPort(ipPortManager.getNextAvailableAddress(requestContext.getIpPort()));
requestContext.setRetry(true);
requestContext.incrementRetryTimes();
RestUtils.httpDo(requestContext, responseHandler);
}

Expand All @@ -98,7 +98,7 @@ protected <T> Handler<RestResponse> syncHandler(CountDownLatch countDownLatch, C
HttpClientResponse response = restResponse.getResponse();
if (response == null) {
// 请求失败,触发请求SC的其他实例
if (!requestContext.isRetry()) {
if (requestContext.getRetryTimes() <= ipPortManager.getMaxRetryTimes()) {
retry(requestContext, syncHandler(countDownLatch, cls, holder));
} else {
countDownLatch.countDown();
Expand Down Expand Up @@ -150,7 +150,7 @@ private Handler<RestResponse> syncHandlerEx(CountDownLatch countDownLatch, Holde
HttpClientResponse response = restResponse.getResponse();
if (response == null) {
// 请求失败,触发请求SC的其他实例
if (!requestContext.isRetry()) {
if (requestContext.getRetryTimes() <= ipPortManager.getMaxRetryTimes()) {
retry(requestContext, syncHandlerEx(countDownLatch, holder));
} else {
countDownLatch.countDown();
Expand All @@ -176,7 +176,7 @@ private <T> Handler<RestResponse> syncHandlerForInstances(CountDownLatch countDo
HttpClientResponse response = restResponse.getResponse();
if (response == null) {
// 请求失败,触发请求SC的其他实例
if (!requestContext.isRetry()) {
if (requestContext.getRetryTimes() <= ipPortManager.getMaxRetryTimes()) {
retry(requestContext, syncHandlerForInstances(countDownLatch, mInstances));
} else {
countDownLatch.countDown();
Expand Down