com.baidu.haotianjing.core.endpoint.EndpointRunningContext Maven / Gradle / Ivy
package com.baidu.haotianjing.core.endpoint;
import com.baidu.haotianjing.core.exception.RiskSdkException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
public class EndpointRunningContext {
private String serviceCode;
private String regionCodes;
private String primaryRegionCode;
private Boolean failOverOn;
private AtomicReference primaryDomain;
/**
* 是否需要新一轮探测,默认不探测
* 1、正常请求中,发现primary domain请求失败,则需要进入新一轮探测,根据用户配置的regionCode选出最优domain
* 2、如果当前primary domain和用户配置的第一优先domain不一致,则需要进入新一轮探测,
* 3、最理想状态:用户配置第一优先domain和选出的primary domain一致,则停止探测
*/
private AtomicBoolean shouldDetect;
private Date lastDetectTime;
private AtomicLong totalDetectCount;
public EndpointRunningContext(String serviceCode, String regionCodes, Boolean failOverOn) {
this.serviceCode = serviceCode;
this.regionCodes = regionCodes;
initPrimaryDomain();
this.failOverOn = failOverOn;
this.shouldDetect = new AtomicBoolean(false);
this.lastDetectTime = new Date();
this.totalDetectCount = new AtomicLong(0);
}
private void initPrimaryDomain() {
Map> regionMap = EndpointConfigLoader.getEndpointMap().get(serviceCode);
if (null == regionMap || regionMap.size() < 1) {
throw new RiskSdkException(String.format("no service while init primary domain. " +
"serviceCode: %s, regionCodes: %s", serviceCode, regionCodes));
}
String regionCode = regionCodes.split(",")[0];
List endpointList = regionMap.get(regionCode);
if (null == endpointList || endpointList.size() < 1) {
throw new RiskSdkException(String.format("no region while init primary domain. " +
"serviceCode: %s, regionCode: %s", serviceCode, regionCode));
}
this.primaryDomain = new AtomicReference<>(endpointList.get(0).getDomain());
this.primaryRegionCode = endpointList.get(0).getRegionCode();
}
public void mustDetect(String mode) {
if (failOverOn && "1".equals(mode)) {
shouldDetect.compareAndSet(false, true);
}
}
public void noDetect() {
if (failOverOn) {
shouldDetect.compareAndSet(true, false);
}
}
public boolean detectStatus() {
if (failOverOn) {
return shouldDetect.get();
}
return false;
}
public String primaryDomain() {
return primaryDomain.get();
}
public void freshPrimaryDomain(String primaryDomain) {
this.primaryDomain.set(primaryDomain);
}
public Boolean getFailOverOn() {
return failOverOn;
}
public String getPrimaryRegionCode() {
return primaryRegionCode;
}
public void setPrimaryRegionCode(String primaryRegionCode) {
this.primaryRegionCode = primaryRegionCode;
}
public void setLastDetectTime(Date lastDetectTime) {
this.lastDetectTime = lastDetectTime;
}
public AtomicLong getTotalDetectCount() {
return totalDetectCount;
}
public String showCurrentRunningContext() {
StringBuilder sb = new StringBuilder();
sb.append("cur running context: ");
sb.append("serviceCode: [").append(this.serviceCode).append("], ");
sb.append("regionCodes: [").append(this.regionCodes).append("], ");
sb.append("primaryReginCode: [").append(this.primaryRegionCode).append("], ");
sb.append("detectStatus: [").append(this.shouldDetect).append("], ");
sb.append("primaryDomain: [").append(this.primaryDomain).append("], ");
sb.append("lastDetectTime: [").append(this.lastDetectTime).append("], ");
sb.append("totalDetectCount: [").append(this.totalDetectCount).append("]");
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy