Skip to content

Commit ac61999

Browse files
authored
[#4942] Add random time to the scheduled task for pulling instances from serviceCenter (#4978)
1 parent 1e95045 commit ac61999

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterDiscovery.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Objects;
26+
import java.util.Random;
2627
import java.util.concurrent.ConcurrentHashMap;
2728

2829
import org.apache.servicecomb.http.client.task.AbstractTask;
@@ -101,6 +102,8 @@ public static class SubscriptionValue {
101102

102103
private final Object lock = new Object();
103104

105+
private final Random random = new Random();
106+
104107
public ServiceCenterDiscovery(ServiceCenterClient serviceCenterClient, EventBus eventBus) {
105108
super("service-center-discovery-task");
106109
this.serviceCenterClient = serviceCenterClient;
@@ -219,10 +222,17 @@ class PullInstanceTask implements Task {
219222
public void execute() {
220223
pullAllInstance();
221224

222-
startTask(new BackOffSleepTask(pollInterval, new PullInstanceTask()));
225+
startTask(new BackOffSleepTask(buildPollIntervalWithSalt(), new PullInstanceTask()));
223226
}
224227
}
225228

229+
private long buildPollIntervalWithSalt() {
230+
int positive = random.nextInt(5);
231+
int sign = random.nextBoolean() ? 1 : -1;
232+
long currentPollInterval = pollInterval + sign * positive * 1000;
233+
return currentPollInterval > 0 ? currentPollInterval : pollInterval;
234+
}
235+
226236
class PullInstanceOnceTask implements Task {
227237
@Override
228238
public void execute() {

0 commit comments

Comments
 (0)