use of com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker in project polaris-java by polarismesh.
the class InstancesCircuitBreakTask method run.
@Override
public void run() {
Map<String, CircuitBreakResult> allResults = new HashMap<>();
Set<ResultKey> statusChangedInstances = new HashSet<>();
Collection<Instance> targetInstances = instances;
if (StringUtils.isNotEmpty(instId)) {
Instance instance = getInstance();
if (null != instance) {
targetInstances = new ArrayList<>();
targetInstances.add(instance);
}
}
if (CollectionUtils.isEmpty(targetInstances)) {
return;
}
for (CircuitBreaker circuitBreaker : extensions.getCircuitBreakers()) {
if (StringUtils.isNotBlank(cbName) && !cbName.equals(circuitBreaker.getName())) {
continue;
}
CircuitBreakResult circuitBreakResult = circuitBreaker.checkInstance(targetInstances);
if (null == circuitBreakResult || circuitBreakResult.isEmptyResult()) {
continue;
}
cleanInstanceSet(circuitBreakResult.getInstancesToOpen(), statusChangedInstances);
cleanInstanceSet(circuitBreakResult.getInstancesToHalfOpen(), statusChangedInstances);
cleanInstanceSet(circuitBreakResult.getInstancesToClose(), statusChangedInstances);
allResults.put(circuitBreaker.getName(), circuitBreakResult);
}
ServiceUpdateRequest updateRequest = buildServiceUpdateRequest(serviceKey, allResults);
if (CollectionUtils.isEmpty(updateRequest.getProperties())) {
return;
}
LOG.info("update cache for circuitbreaker, value is {}", updateRequest);
extensions.getLocalRegistry().updateInstances(updateRequest);
}
use of com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker in project polaris-java by polarismesh.
the class ServiceCallResultChecker method onServiceCallResult.
@Override
public void onServiceCallResult(InstanceGauge result) {
if (null == priorityTaskScheduler) {
return;
}
if (CollectionUtils.isEmpty(extensions.getCircuitBreakers())) {
return;
}
InstancesCircuitBreakTask rtTask = null;
for (CircuitBreaker circuitBreaker : extensions.getCircuitBreakers()) {
String cbName = circuitBreaker.getName();
boolean rtLimit = circuitBreaker.stat(result);
String instId = result.getInstanceId();
if (rtLimit && StringUtils.isNotEmpty(instId)) {
ServiceKey svcKey = new ServiceKey(result.getNamespace(), result.getService());
rtTask = new InstancesCircuitBreakTask(svcKey, cbName, null, instId, extensions, InstancesCircuitBreakTask.TaskPriority.HIGH);
break;
}
}
if (null == rtTask) {
return;
}
priorityTaskScheduler.addCircuitBreakTask(rtTask);
}
Aggregations