Skip to content

Commit 6c85f1b

Browse files
authored
[#4942] Add random time to the scheduled task for pulling instances from serviceCenter (#4977)
1 parent c545324 commit 6c85f1b

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
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Objects;
24+
import java.util.Random;
2425
import java.util.concurrent.ConcurrentHashMap;
2526

2627
import org.apache.servicecomb.http.client.task.AbstractTask;
@@ -99,6 +100,8 @@ public static class SubscriptionValue {
99100

100101
private final Object lock = new Object();
101102

103+
private final Random random = new Random();
104+
102105
public ServiceCenterDiscovery(ServiceCenterClient serviceCenterClient, EventBus eventBus) {
103106
super("service-center-discovery-task");
104107
this.serviceCenterClient = serviceCenterClient;
@@ -208,10 +211,17 @@ class PullInstanceTask implements Task {
208211
public void execute() {
209212
pullAllInstance();
210213

211-
startTask(new BackOffSleepTask(pollInterval, new PullInstanceTask()));
214+
startTask(new BackOffSleepTask(buildPollIntervalWithSalt(), new PullInstanceTask()));
212215
}
213216
}
214217

218+
private long buildPollIntervalWithSalt() {
219+
int positive = random.nextInt(5);
220+
int sign = random.nextBoolean() ? 1 : -1;
221+
long currentPollInterval = pollInterval + sign * positive * 1000;
222+
return currentPollInterval > 0 ? currentPollInterval : pollInterval;
223+
}
224+
215225
class PullInstanceOnceTask implements Task {
216226
@Override
217227
public void execute() {

0 commit comments

Comments
 (0)