inflow group 추가
This commit is contained in:
@@ -40,6 +40,9 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
|
||||
private Map<String, CustomBucket> adapterBucketList = new HashMap<>();
|
||||
private Map<String, CustomBucket> interfaceBucketList = new HashMap<>();
|
||||
private Map<String, CustomBucket> groupBucketList = new HashMap<>();
|
||||
private Map<String, String> interfaceToGroupMap = new HashMap<>();
|
||||
private Map<String, InflowGroupVO> groupVoMap = new HashMap<>();
|
||||
private boolean started;
|
||||
private LifecycleSupport lifecycle = new LifecycleSupport(this);
|
||||
|
||||
@@ -91,6 +94,7 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
private void init() throws Exception {
|
||||
initAdapter();
|
||||
initInterface();
|
||||
initGroup();
|
||||
}
|
||||
|
||||
private void initAdapter() throws Exception {
|
||||
@@ -121,6 +125,33 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
}
|
||||
}
|
||||
|
||||
private void initGroup() throws Exception {
|
||||
groupBucketList = new HashMap<>();
|
||||
interfaceToGroupMap = new HashMap<>();
|
||||
groupVoMap = new HashMap<>();
|
||||
|
||||
List<InflowGroupVO> inflowGroupVoList = inflowControlDAO.getInflowGroupList();
|
||||
|
||||
for (InflowGroupVO groupVo : inflowGroupVoList) {
|
||||
if (isInflowGroupTarget(groupVo)) {
|
||||
CustomBucket bucket = makeBucketUsingGroupVO(groupVo);
|
||||
if (bucket != null) {
|
||||
groupBucketList.put(groupVo.getGroupId(), bucket);
|
||||
groupVoMap.put(groupVo.getGroupId(), groupVo);
|
||||
// 인터페이스 → 그룹 매핑 등록
|
||||
for (String interfaceId : groupVo.getInterfaceList()) {
|
||||
interfaceToGroupMap.put(interfaceId, groupVo.getGroupId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isInfo()) {
|
||||
logger.info("InflowControlManager] initGroup completed. groups=" + groupBucketList.size()
|
||||
+ ", mappings=" + interfaceToGroupMap.size());
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void reloadAdapter() throws Exception {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reload all Started ...");
|
||||
@@ -200,6 +231,61 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void reloadGroup() throws Exception {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reloadGroup all Started ...");
|
||||
}
|
||||
initGroup();
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reloadGroup all finished ...");
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void reloadGroup(String groupId) throws Exception {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reloadGroup Started ... groupId=" + groupId);
|
||||
}
|
||||
Map<String, InflowGroupVO> map = inflowControlDAO.getInflowTargetByGroup(groupId);
|
||||
if (map != null) {
|
||||
// 기존 그룹에 속한 인터페이스 매핑 제거
|
||||
Iterator<Map.Entry<String, String>> mappingIt = interfaceToGroupMap.entrySet().iterator();
|
||||
while (mappingIt.hasNext()) {
|
||||
Map.Entry<String, String> entry = mappingIt.next();
|
||||
if (groupId.equals(entry.getValue())) {
|
||||
mappingIt.remove();
|
||||
}
|
||||
}
|
||||
|
||||
Iterator<String> it = map.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
String key = it.next();
|
||||
InflowGroupVO groupVo = map.get(key);
|
||||
|
||||
if (isInflowGroupTarget(groupVo)) {
|
||||
CustomBucket bucket = makeBucketUsingGroupVO(groupVo);
|
||||
if (bucket != null) {
|
||||
groupBucketList.put(groupVo.getGroupId(), bucket);
|
||||
groupVoMap.put(groupVo.getGroupId(), groupVo);
|
||||
// 인터페이스 → 그룹 매핑 재등록
|
||||
for (String interfaceId : groupVo.getInterfaceList()) {
|
||||
interfaceToGroupMap.put(interfaceId, groupVo.getGroupId());
|
||||
}
|
||||
} else {
|
||||
groupBucketList.remove(groupVo.getGroupId());
|
||||
groupVoMap.remove(groupVo.getGroupId());
|
||||
}
|
||||
} else {
|
||||
groupBucketList.remove(groupVo.getGroupId());
|
||||
groupVoMap.remove(groupVo.getGroupId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reloadGroup finished ...");
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() throws LifecycleException {
|
||||
// Validate and update our current component state
|
||||
if (!started)
|
||||
@@ -210,6 +296,9 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
|
||||
adapterBucketList.clear();
|
||||
interfaceBucketList.clear();
|
||||
groupBucketList.clear();
|
||||
interfaceToGroupMap.clear();
|
||||
groupVoMap.clear();
|
||||
|
||||
started = false;
|
||||
// Notify our interested LifecycleListeners
|
||||
@@ -260,6 +349,29 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
interfaceBucketList.remove(inter);
|
||||
}
|
||||
|
||||
public void removeGroup(String groupId) {
|
||||
groupBucketList.remove(groupId);
|
||||
groupVoMap.remove(groupId);
|
||||
// 해당 그룹에 속한 인터페이스 매핑도 제거
|
||||
Iterator<Map.Entry<String, String>> it = interfaceToGroupMap.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<String, String> entry = it.next();
|
||||
if (groupId.equals(entry.getValue())) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getGroupAllKeys() {
|
||||
Iterator<String> it = this.groupBucketList.keySet().iterator();
|
||||
String[] groupIds = new String[this.groupBucketList.size()];
|
||||
for (int i = 0; it.hasNext(); i++) {
|
||||
groupIds[i] = it.next();
|
||||
}
|
||||
Arrays.sort(groupIds);
|
||||
return groupIds;
|
||||
}
|
||||
|
||||
private CustomBucket makeBucketUsingInflowVO(InflowTargetVO inflowTarget) {
|
||||
if (InflowControlManager.isInflowTarget(inflowTarget)) {
|
||||
LocalBucketBuilder builder = Bucket4j.builder();
|
||||
@@ -278,6 +390,32 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
return null;
|
||||
}
|
||||
|
||||
private CustomBucket makeBucketUsingGroupVO(InflowGroupVO groupVo) {
|
||||
if (isInflowGroupTarget(groupVo)) {
|
||||
LocalBucketBuilder builder = Bucket4j.builder();
|
||||
if (groupVo.getThresholdPerSecond() > 0) {
|
||||
builder.addLimit(Bandwidth.simple(groupVo.getThresholdPerSecond(), Duration.ofSeconds(1)));
|
||||
}
|
||||
|
||||
if (groupVo.getThreshold() > 0 && StringUtils.isNotBlank(groupVo.getThresholdTimeUnit())) {
|
||||
builder.addLimit(Bandwidth.simple(groupVo.getThreshold(),
|
||||
InflowControlManager.getPeriod(groupVo.getThresholdTimeUnit())));
|
||||
}
|
||||
|
||||
// InflowTargetVO로 변환하여 CustomBucket 생성
|
||||
InflowTargetVO targetVo = new InflowTargetVO();
|
||||
targetVo.setName(groupVo.getGroupId());
|
||||
targetVo.setThreshold(groupVo.getThreshold());
|
||||
targetVo.setThresholdPerSecond(groupVo.getThresholdPerSecond());
|
||||
targetVo.setThresholdTimeUnit(groupVo.getThresholdTimeUnit());
|
||||
targetVo.setActivate(groupVo.isActivate());
|
||||
|
||||
return new CustomBucket(builder.build(), targetVo);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdapterPass(String adapter) {
|
||||
CustomBucket b = adapterBucketList.get(adapter);
|
||||
@@ -327,6 +465,30 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
return getInterfaceInflowThreashold(inter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGroupPass(String groupId) {
|
||||
CustomBucket b = groupBucketList.get(groupId);
|
||||
|
||||
if (b == null)
|
||||
return true;
|
||||
|
||||
return b.getLocalBucket().tryConsume(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InflowGroupVO getGroupInflowThreshold(String groupId) {
|
||||
return groupVoMap.get(groupId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGroupIdByInterface(String interfaceId) {
|
||||
return interfaceToGroupMap.get(interfaceId);
|
||||
}
|
||||
|
||||
public InflowGroupVO getGroupInflow(String groupId) {
|
||||
return getGroupInflowThreshold(groupId);
|
||||
}
|
||||
|
||||
public static boolean isInflowTarget(InflowTargetVO inflowTarget) {
|
||||
if (inflowTarget == null || !inflowTarget.isActivate()) {
|
||||
return false;
|
||||
@@ -338,6 +500,16 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
|
||||
}
|
||||
|
||||
public static boolean isInflowGroupTarget(InflowGroupVO groupVo) {
|
||||
if (groupVo == null || !groupVo.isActivate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return groupVo.getThresholdPerSecond() > 0 || (groupVo.getThreshold() > 0 && StringUtils
|
||||
.equalsAnyIgnoreCase(groupVo.getThresholdTimeUnit(), QUOTA_TIMEUNIT_DAY, QUOTA_TIMEUNIT_SECOND,
|
||||
QUOTA_TIMEUNIT_MINUTE, QUOTA_TIMEUNIT_HOUR, QUOTA_TIMEUNIT_DAY, QUOTA_TIMEUNIT_MONTH));
|
||||
}
|
||||
|
||||
public static Duration getPeriod(String timeUnit) {
|
||||
if (StringUtils.equalsIgnoreCase(timeUnit, QUOTA_TIMEUNIT_SECOND)) {
|
||||
return Duration.ofSeconds(1);
|
||||
|
||||
Reference in New Issue
Block a user