Limit the times of registry retry. (#2946)

The default value is 3.
This commit is contained in:
时无两丶 2018-12-13 17:07:17 +08:00 committed by Ian Luo
parent b8c3e84455
commit afcb9f2b8d
2 changed files with 30 additions and 3 deletions

View File

@ -487,11 +487,21 @@ public class Constants {
*/
public static final String REGISTRY_RETRY_PERIOD_KEY = "retry.period";
/**
* Most retry times
*/
public static final String REGISTRY_RETRY_TIMES_KEY = "retry.times";
/**
* Default value for the period of retry interval in milliseconds: 5000
*/
public static final int DEFAULT_REGISTRY_RETRY_PERIOD = 5 * 1000;
/**
* Default value for the times of retry: 3
*/
public static final int DEFAULT_REGISTRY_RETRY_TIMES = 3;
/**
* Reconnection period in milliseconds for register center
*/

View File

@ -49,12 +49,23 @@ public abstract class AbstractRetryTask implements TimerTask {
/**
* retry period
*/
protected final long retryPeriod;
final long retryPeriod;
/**
* define the most retry times
*/
private final int retryTimes;
/**
* task name for this task
*/
protected final String taskName;
private final String taskName;
/**
* times of retry.
* retry task is execute in single thread so that the times is not need volatile.
*/
private int times = 1;
private volatile boolean cancel;
@ -67,6 +78,7 @@ public abstract class AbstractRetryTask implements TimerTask {
this.taskName = taskName;
cancel = false;
this.retryPeriod = url.getParameter(Constants.REGISTRY_RETRY_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RETRY_PERIOD);
this.retryTimes = url.getParameter(Constants.REGISTRY_RETRY_TIMES_KEY, Constants.DEFAULT_REGISTRY_RETRY_TIMES);
}
public void cancel() {
@ -86,7 +98,7 @@ public abstract class AbstractRetryTask implements TimerTask {
if (timer.isStop() || timeout.isCancelled() || isCancel()) {
return;
}
times++;
timer.newTimeout(timeout.task(), tick, TimeUnit.MILLISECONDS);
}
@ -96,6 +108,11 @@ public abstract class AbstractRetryTask implements TimerTask {
// other thread cancel this timeout or stop the timer.
return;
}
if (times > retryTimes) {
// reach the most times of retry.
logger.warn("Final failed to execute task " + taskName + ", url: " + url + ", retry " + retryTimes + " times.");
return;
}
if (logger.isInfoEnabled()) {
logger.info(taskName + " : " + url);
}