Merge branch 'feature/inflow-control-improvement' into master

This commit is contained in:
curry772
2026-05-08 10:51:48 +09:00
22 changed files with 1284 additions and 439 deletions
@@ -0,0 +1,81 @@
package com.eactive.eai.custom.inbound.processor;
import org.apache.commons.lang3.StringUtils;
import com.eactive.eai.common.inflow.Bucket;
import com.eactive.eai.common.inflow.InflowTargetVO;
import com.eactive.eai.common.inflow.dual.ClientDualBucket;
import com.eactive.eai.common.inflow.dual.DualBucket;
import com.eactive.eai.common.inflow.dual.DualCustomBucket;
import com.eactive.eai.inbound.processor.RequestProcessor;
/**
* 어댑터/인터페이스/클라이언트 유량제어 차단 시 어느 버킷(PER_SECOND/THRESHOLD)에서
* 차단됐는지 에러 메시지에 포함하는 RequestProcessor 확장.
*
* <p>ClientDualInflowControlManager와 함께 사용:
* <pre>
* inflow.control.bucket.className=com.eactive.eai.common.inflow.dual.ClientDualInflowControlManager
* </pre>
*
* <p>Spring Bean 등록 후 기존 RequestProcessor 대신 이 클래스를 어댑터에 설정한다.
*/
public class DJBRequestProcessor extends RequestProcessor {
@Override
protected String checkClientInflow(Bucket bucket, String clientId) {
if (StringUtils.isBlank(clientId)) return null;
if (bucket instanceof ClientDualBucket) {
return ((ClientDualBucket) bucket).isClientPassDetail(clientId);
}
return null;
}
@Override
protected InflowTargetVO resolveClientInflowThreshold(Bucket bucket, String clientId) {
if (bucket instanceof ClientDualBucket) {
return ((ClientDualBucket) bucket).getClientInflowThreshold(clientId);
}
return null;
}
@Override
protected String checkAdapterInflow(Bucket bucket, String adapterGroupName) {
if (bucket instanceof DualBucket) {
return ((DualBucket) bucket).isAdapterPassDetail(adapterGroupName);
}
return super.checkAdapterInflow(bucket, adapterGroupName);
}
@Override
protected String checkInterfaceInflow(Bucket bucket, String eaiSvcCd) {
if (bucket instanceof DualBucket) {
return ((DualBucket) bucket).isInterfacePassDetail(eaiSvcCd);
}
return super.checkInterfaceInflow(bucket, eaiSvcCd);
}
/**
* 차단 원인에 따라 에러 메시지에 한도 정보를 포함.
* <ul>
* <li>PER_SECOND: "API 호출 한도 초과 [name: Nreq/sec]"</li>
* <li>THRESHOLD: "API 호출 한도 초과 [name: Nreq/UNIT]"</li>
* <li>그 외: "API 호출 한도 초과 [name]" (기존 동작)</li>
* </ul>
*/
@Override
protected String buildInflowTargetErrorMsg(InflowTargetVO inflowTargetVO, String blockedType) {
if (DualCustomBucket.RESULT_BLOCKED_PER_SECOND.equals(blockedType)) {
return String.format("API 호출 한도 초과 [%s: %dreq/sec]",
inflowTargetVO.getName(),
inflowTargetVO.getThresholdPerSecond());
}
if (DualCustomBucket.RESULT_BLOCKED_THRESHOLD.equals(blockedType)) {
return String.format("API 호출 한도 초과 [%s: %dreq/%s]",
inflowTargetVO.getName(),
inflowTargetVO.getThreshold(),
inflowTargetVO.getThresholdTimeUnit());
}
return super.buildInflowTargetErrorMsg(inflowTargetVO, blockedType);
}
}
@@ -0,0 +1,63 @@
package com.eactive.eai.custom.inflow;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.eactive.eai.manage.inflow.TargetBucketStatusDTO;
/**
* 클라이언트 버킷 상태 모니터링 API.
*
* ClientDualInflowControlManager 가 활성화된 환경에서만 정상 응답한다.
*
* GET /manage/inflow/client/bucket-status → 전체 클라이언트 버킷 상태
* GET /manage/inflow/client/{clientId}/bucket-status → 특정 클라이언트 버킷 상태
*/
@RestController
@RequestMapping("/manage/inflow")
public class ClientInflowTargetBucketController {
@Autowired
private ClientInflowTargetBucketService bucketService;
@GetMapping("/client/bucket-status")
public ResponseEntity<?> getAllClientBucketStatus() {
List<TargetBucketStatusDTO> list = bucketService.getAllClientBucketStatus();
return ok(list);
}
@GetMapping("/client/{clientId}/bucket-status")
public ResponseEntity<?> getClientBucketStatus(@PathVariable String clientId) {
TargetBucketStatusDTO dto = bucketService.getClientBucketStatus(clientId);
return single(dto, "클라이언트를 찾을 수 없습니다: " + clientId);
}
private ResponseEntity<?> ok(List<TargetBucketStatusDTO> list) {
Map<String, Object> result = new HashMap<>();
result.put("success", true);
result.put("data", list);
result.put("count", list.size());
return ResponseEntity.ok(result);
}
private ResponseEntity<?> single(TargetBucketStatusDTO dto, String notFoundMsg) {
if (dto == null) {
Map<String, Object> error = new HashMap<>();
error.put("success", false);
error.put("message", notFoundMsg);
return ResponseEntity.ok(error);
}
Map<String, Object> result = new HashMap<>();
result.put("success", true);
result.put("data", dto);
return ResponseEntity.ok(result);
}
}
@@ -0,0 +1,79 @@
package com.eactive.eai.custom.inflow;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.eactive.eai.common.inflow.AbstractInflowControlManager;
import com.eactive.eai.common.inflow.InflowControlUtil;
import com.eactive.eai.common.inflow.InflowTargetVO;
import com.eactive.eai.common.inflow.dual.ClientDualInflowControlManager;
import com.eactive.eai.common.inflow.dual.DualCustomBucket;
import com.eactive.eai.manage.inflow.GroupBucketStatusDTO;
import com.eactive.eai.manage.inflow.TargetBucketStatusDTO;
/**
* 클라이언트 버킷 상태 조회 서비스.
*
* ClientDualInflowControlManager 가 활성화된 경우에만 동작한다.
*/
@Service
public class ClientInflowTargetBucketService {
public TargetBucketStatusDTO getClientBucketStatus(String clientId) {
ClientDualInflowControlManager manager = getClientDualManager();
if (manager == null) return null;
DualCustomBucket bucket = manager.getClientBucket(clientId);
if (bucket == null) return null;
return toDto(clientId, "CLIENT", bucket);
}
public List<TargetBucketStatusDTO> getAllClientBucketStatus() {
ClientDualInflowControlManager manager = getClientDualManager();
if (manager == null) return new ArrayList<>();
return toDtoList("CLIENT", manager.getClientBucketMap());
}
protected ClientDualInflowControlManager getClientDualManager() {
AbstractInflowControlManager base = InflowControlUtil.getInflowControlManager();
return (base instanceof ClientDualInflowControlManager) ? (ClientDualInflowControlManager) base : null;
}
private TargetBucketStatusDTO toDto(String targetId, String targetType, DualCustomBucket bucket) {
InflowTargetVO vo = bucket.getInflowTargetVo();
List<GroupBucketStatusDTO.BucketInfo> buckets = new ArrayList<>();
if (vo.getThresholdPerSecond() > 0) {
long capacity = vo.getThresholdPerSecond();
long available = bucket.getPerSecondAvailableTokens();
if (available < 0) available = capacity;
buckets.add(new GroupBucketStatusDTO.BucketInfo(
"perSecond", capacity, Math.min(available, capacity), "SEC"));
}
if (vo.getThreshold() > 0) {
long capacity = vo.getThreshold();
long available = bucket.getThresholdAvailableTokens();
if (available < 0) available = capacity;
buckets.add(new GroupBucketStatusDTO.BucketInfo(
"threshold", capacity, Math.min(available, capacity), vo.getThresholdTimeUnit()));
}
TargetBucketStatusDTO dto = new TargetBucketStatusDTO();
dto.setTargetId(targetId);
dto.setTargetType(targetType);
dto.setActivate(vo.isActivate());
dto.setBuckets(buckets);
return dto;
}
private List<TargetBucketStatusDTO> toDtoList(String targetType, Map<String, DualCustomBucket> bucketMap) {
List<TargetBucketStatusDTO> result = new ArrayList<>();
for (Map.Entry<String, DualCustomBucket> entry : bucketMap.entrySet()) {
result.add(toDto(entry.getKey(), targetType, entry.getValue()));
}
return result;
}
}
@@ -0,0 +1,94 @@
package com.eactive.eai.custom.inflow;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.eactive.eai.manage.inflow.TargetMetricDTO;
/**
* 클라이언트 유량제어 메트릭 API.
*
* ClientDualInflowControlManager 가 활성화된 환경에서만 정상 응답한다.
*
* GET /manage/inflow/client/metric → 전체 클라이언트 메트릭
* GET /manage/inflow/client/{clientId}/metric → 특정 클라이언트 메트릭
* POST /manage/inflow/client/metric/reset → 전체 클라이언트 메트릭 초기화
* POST /manage/inflow/client/{clientId}/metric/reset → 특정 클라이언트 메트릭 초기화
*/
@RestController
@RequestMapping("/manage/inflow")
public class ClientInflowTargetMetricController {
@Autowired
private ClientInflowTargetMetricService metricService;
@GetMapping("/client/metric")
public ResponseEntity<?> getAllClientMetrics() {
List<TargetMetricDTO> list = metricService.getAllClientMetrics();
return okList(list);
}
@GetMapping("/client/{clientId}/metric")
public ResponseEntity<?> getClientMetric(@PathVariable String clientId) {
TargetMetricDTO dto = metricService.getClientMetric(clientId);
return okSingle(dto, "클라이언트를 찾을 수 없습니다: " + clientId);
}
@PostMapping("/client/metric/reset")
public ResponseEntity<?> resetAllClientMetrics() {
metricService.resetAllClientMetrics();
return okReset("전체 클라이언트 메트릭 초기화 완료");
}
@PostMapping("/client/{clientId}/metric/reset")
public ResponseEntity<?> resetClientMetric(@PathVariable String clientId) {
boolean ok = metricService.resetClientMetric(clientId);
return okResetSingle(ok, clientId, "클라이언트");
}
private ResponseEntity<?> okList(List<TargetMetricDTO> list) {
Map<String, Object> result = new HashMap<>();
result.put("success", true);
result.put("data", list);
result.put("count", list.size());
return ResponseEntity.ok(result);
}
private ResponseEntity<?> okSingle(TargetMetricDTO dto, String notFoundMsg) {
if (dto == null) {
Map<String, Object> error = new HashMap<>();
error.put("success", false);
error.put("message", notFoundMsg);
return ResponseEntity.ok(error);
}
Map<String, Object> result = new HashMap<>();
result.put("success", true);
result.put("data", dto);
return ResponseEntity.ok(result);
}
private ResponseEntity<?> okReset(String message) {
Map<String, Object> result = new HashMap<>();
result.put("success", true);
result.put("message", message);
return ResponseEntity.ok(result);
}
private ResponseEntity<?> okResetSingle(boolean ok, String targetId, String targetLabel) {
Map<String, Object> result = new HashMap<>();
result.put("success", ok);
result.put("message", ok
? "초기화 완료: " + targetId
: targetLabel + "를 찾을 수 없습니다: " + targetId);
return ResponseEntity.ok(result);
}
}
@@ -0,0 +1,79 @@
package com.eactive.eai.custom.inflow;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.eactive.eai.common.inflow.AbstractInflowControlManager;
import com.eactive.eai.common.inflow.InflowControlUtil;
import com.eactive.eai.common.inflow.InflowTargetVO;
import com.eactive.eai.common.inflow.dual.ClientDualInflowControlManager;
import com.eactive.eai.common.inflow.dual.DualInflowControlManager;
import com.eactive.eai.manage.inflow.TargetMetricDTO;
@Service
public class ClientInflowTargetMetricService {
public TargetMetricDTO getClientMetric(String clientId) {
ClientDualInflowControlManager manager = getClientDualManager();
if (manager == null) return null;
InflowTargetVO vo = manager.getClientInflowThreshold(clientId);
if (vo == null) return null;
return buildDTO(clientId, "CLIENT", vo, manager.getClientMetrics(clientId));
}
public List<TargetMetricDTO> getAllClientMetrics() {
ClientDualInflowControlManager manager = getClientDualManager();
if (manager == null) return new ArrayList<>();
List<TargetMetricDTO> result = new ArrayList<>();
for (String id : manager.getClientBucketMap().keySet()) {
TargetMetricDTO dto = getClientMetric(id);
if (dto != null) result.add(dto);
}
return result;
}
public boolean resetClientMetric(String clientId) {
ClientDualInflowControlManager manager = getClientDualManager();
if (manager == null) return false;
if (manager.getClientInflowThreshold(clientId) == null) return false;
manager.resetClientMetrics(clientId);
return true;
}
public void resetAllClientMetrics() {
ClientDualInflowControlManager manager = getClientDualManager();
if (manager != null) manager.resetAllClientMetrics();
}
protected ClientDualInflowControlManager getClientDualManager() {
AbstractInflowControlManager base = InflowControlUtil.getInflowControlManager();
return (base instanceof ClientDualInflowControlManager) ? (ClientDualInflowControlManager) base : null;
}
private TargetMetricDTO buildDTO(String targetId, String targetType,
InflowTargetVO vo,
DualInflowControlManager.TargetMetrics m) {
TargetMetricDTO dto = new TargetMetricDTO();
dto.setTargetId(targetId);
dto.setTargetType(targetType);
dto.setActivate(vo.isActivate());
if (m != null) {
long allowed = m.allowed.get();
long rejPs = m.rejectedPerSecond.get();
long rejTh = m.rejectedThreshold.get();
long total = allowed + rejPs + rejTh;
dto.setAllowed(allowed);
dto.setRejectedPerSecond(rejPs);
dto.setRejectedThreshold(rejTh);
dto.setTotalRequests(total);
dto.setRejectRatio(total > 0
? Math.round((double)(rejPs + rejTh) / total * 10000) / 100.0
: 0.0);
dto.setLastResetTime(m.lastResetTime);
}
return dto;
}
}
@@ -21,8 +21,6 @@ import org.springframework.web.bind.annotation.RestController;
* GET /manage/inflow/adapter/{adapterId}/bucket-status → 특정 어댑터 버킷 상태
* GET /manage/inflow/interface/bucket-status → 전체 인터페이스 버킷 상태
* GET /manage/inflow/interface/{interfaceId}/bucket-status → 특정 인터페이스 버킷 상태
* GET /manage/inflow/client/bucket-status → 전체 클라이언트 버킷 상태
* GET /manage/inflow/client/{clientId}/bucket-status → 특정 클라이언트 버킷 상태
*/
@RestController
@RequestMapping("/manage/inflow")
@@ -63,22 +61,6 @@ public class InflowTargetBucketController {
return single(dto, "인터페이스를 찾을 수 없습니다: " + interfaceId);
}
// =========================================================================
// 클라이언트
// =========================================================================
@GetMapping("/client/bucket-status")
public ResponseEntity<?> getAllClientBucketStatus() {
List<TargetBucketStatusDTO> list = bucketService.getAllClientBucketStatus();
return ok(list);
}
@GetMapping("/client/{clientId}/bucket-status")
public ResponseEntity<?> getClientBucketStatus(@PathVariable String clientId) {
TargetBucketStatusDTO dto = bucketService.getClientBucketStatus(clientId);
return single(dto, "클라이언트를 찾을 수 없습니다: " + clientId);
}
// =========================================================================
// Helpers
// =========================================================================
@@ -57,24 +57,6 @@ public class InflowTargetBucketService {
return toDtoList("INTERFACE", manager.getInterfaceBucketMap());
}
// -------------------------------------------------------------------------
// 클라이언트
// -------------------------------------------------------------------------
public TargetBucketStatusDTO getClientBucketStatus(String clientId) {
DualInflowControlManager manager = getDualManager();
if (manager == null) return null;
DualCustomBucket bucket = manager.getClientBucket(clientId);
if (bucket == null) return null;
return toDto(clientId, "CLIENT", bucket);
}
public List<TargetBucketStatusDTO> getAllClientBucketStatus() {
DualInflowControlManager manager = getDualManager();
if (manager == null) return new ArrayList<>();
return toDtoList("CLIENT", manager.getClientBucketMap());
}
// -------------------------------------------------------------------------
// Private helpers
// -------------------------------------------------------------------------
@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 어댑터/인터페이스/클라이언트 유량제어 메트릭 API.
* 어댑터/인터페이스 유량제어 메트릭 API.
*
* DualInflowControlManager 가 활성화된 환경에서만 정상 응답한다.
*
@@ -26,11 +26,6 @@ import org.springframework.web.bind.annotation.RestController;
* GET /manage/inflow/interface/{interfaceId}/metric → 특정 인터페이스 메트릭
* POST /manage/inflow/interface/metric/reset → 전체 인터페이스 메트릭 초기화
* POST /manage/inflow/interface/{interfaceId}/metric/reset → 특정 인터페이스 메트릭 초기화
*
* GET /manage/inflow/client/metric → 전체 클라이언트 메트릭
* GET /manage/inflow/client/{clientId}/metric → 특정 클라이언트 메트릭
* POST /manage/inflow/client/metric/reset → 전체 클라이언트 메트릭 초기화
* POST /manage/inflow/client/{clientId}/metric/reset → 특정 클라이언트 메트릭 초기화
*/
@RestController
@RequestMapping("/manage/inflow")
@@ -95,34 +90,6 @@ public class InflowTargetMetricController {
return okResetSingle(ok, interfaceId, "인터페이스");
}
// =========================================================================
// 클라이언트
// =========================================================================
@GetMapping("/client/metric")
public ResponseEntity<?> getAllClientMetrics() {
List<TargetMetricDTO> list = metricService.getAllClientMetrics();
return okList(list);
}
@GetMapping("/client/{clientId}/metric")
public ResponseEntity<?> getClientMetric(@PathVariable String clientId) {
TargetMetricDTO dto = metricService.getClientMetric(clientId);
return okSingle(dto, "클라이언트를 찾을 수 없습니다: " + clientId);
}
@PostMapping("/client/metric/reset")
public ResponseEntity<?> resetAllClientMetrics() {
metricService.resetAllClientMetrics();
return okReset("전체 클라이언트 메트릭 초기화 완료");
}
@PostMapping("/client/{clientId}/metric/reset")
public ResponseEntity<?> resetClientMetric(@PathVariable String clientId) {
boolean ok = metricService.resetClientMetric(clientId);
return okResetSingle(ok, clientId, "클라이언트");
}
// =========================================================================
// Helpers
// =========================================================================
@@ -85,42 +85,6 @@ public class InflowTargetMetricService {
if (manager != null) manager.resetAllInterfaceMetrics();
}
// -------------------------------------------------------------------------
// 클라이언트
// -------------------------------------------------------------------------
public TargetMetricDTO getClientMetric(String clientId) {
DualInflowControlManager manager = getDualManager();
if (manager == null) return null;
InflowTargetVO vo = manager.getClientInflowThreshold(clientId);
if (vo == null) return null;
return buildDTO(clientId, "CLIENT", vo, manager.getClientMetrics(clientId));
}
public List<TargetMetricDTO> getAllClientMetrics() {
DualInflowControlManager manager = getDualManager();
if (manager == null) return new ArrayList<>();
List<TargetMetricDTO> result = new ArrayList<>();
for (String id : manager.getClientBucketMap().keySet()) {
TargetMetricDTO dto = getClientMetric(id);
if (dto != null) result.add(dto);
}
return result;
}
public boolean resetClientMetric(String clientId) {
DualInflowControlManager manager = getDualManager();
if (manager == null) return false;
if (manager.getClientInflowThreshold(clientId) == null) return false;
manager.resetClientMetrics(clientId);
return true;
}
public void resetAllClientMetrics() {
DualInflowControlManager manager = getDualManager();
if (manager != null) manager.resetAllClientMetrics();
}
// -------------------------------------------------------------------------
// Private helpers
// -------------------------------------------------------------------------