Skip to content

Commit e390f8d

Browse files
committed
adjust endpoint health check
1 parent 1679770 commit e390f8d

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import java.util.Map;
2727
import java.util.Objects;
2828
import java.util.concurrent.ConcurrentHashMap;
29+
import java.util.concurrent.Executors;
30+
import java.util.concurrent.ScheduledExecutorService;
31+
import java.util.concurrent.TimeUnit;
2932

3033
import org.apache.servicecomb.http.client.task.AbstractTask;
3134
import org.apache.servicecomb.http.client.task.Task;
@@ -126,6 +129,7 @@ public void startDiscovery() {
126129
if (!started) {
127130
started = true;
128131
startTask(new PullInstanceTask());
132+
startCheckInstancesHealth();
129133
}
130134
}
131135

@@ -228,16 +232,33 @@ public void execute() {
228232
}
229233

230234
private synchronized void pullAllInstance() {
231-
List<SubscriptionKey> failedInstances = new ArrayList<>();
232235
instancesCache.forEach((k, v) -> {
233236
pullInstance(k, v, true);
234-
v.instancesCache.removeIf(instance -> isInstanceUnavailable(instance.getServiceName(), instance.getEndpoints()));
235-
if (v.instancesCache.isEmpty()) {
236-
failedInstances.add(k);
237-
}
238237
});
239-
failedInstances.forEach(instancesCache::remove);
240-
failedInstances.clear();
238+
}
239+
240+
private void startCheckInstancesHealth() {
241+
ScheduledExecutorService executor =
242+
Executors.newScheduledThreadPool(1, (t) -> new Thread(t, "instance-health-check"));
243+
executor.scheduleWithFixedDelay(new CheckInstancesHealthTask(), 0, pollInterval, TimeUnit.MILLISECONDS);
244+
}
245+
246+
class CheckInstancesHealthTask implements Runnable {
247+
@Override
248+
public void run() {
249+
if (instancesCache.isEmpty()) {
250+
return;
251+
}
252+
List<SubscriptionKey> failedInstances = new ArrayList<>();
253+
instancesCache.forEach((k, v) -> {
254+
v.instancesCache.removeIf(item -> isInstanceUnavailable(item.getServiceName(), item.getEndpoints()));
255+
if (v.instancesCache.isEmpty()) {
256+
failedInstances.add(k);
257+
}
258+
});
259+
failedInstances.forEach(instancesCache::remove);
260+
failedInstances.clear();
261+
}
241262
}
242263

243264
private static String instanceToString(List<MicroserviceInstance> instances) {

0 commit comments

Comments
 (0)