From d82efe10ae5d1ef27d148bdb761b6f4087efb6f2 Mon Sep 17 00:00:00 2001 From: justforstudy <2167005981@qq.com> Date: Fri, 26 Aug 2022 18:08:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=86=94=E6=96=AD=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E7=AA=97=E5=8F=A3=E6=97=B6=E9=97=B4=E5=8D=95=E4=BD=8D?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../res4j/handler/CircuitBreakerHandler.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-service/src/main/java/com/huawei/fowcontrol/res4j/handler/CircuitBreakerHandler.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-service/src/main/java/com/huawei/fowcontrol/res4j/handler/CircuitBreakerHandler.java index 5e6236d080..7289c435ef 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-service/src/main/java/com/huawei/fowcontrol/res4j/handler/CircuitBreakerHandler.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-service/src/main/java/com/huawei/fowcontrol/res4j/handler/CircuitBreakerHandler.java @@ -25,6 +25,7 @@ import io.github.resilience4j.circuitbreaker.CircuitBreaker; import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; +import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.SlidingWindowType; import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; import java.time.Duration; @@ -39,6 +40,7 @@ public class CircuitBreakerHandler extends AbstractRequestHandler { @Override protected final Optional createProcessor(String businessName, CircuitBreakerRule rule) { + final SlidingWindowType slidingWindowType = getSlidingWindowType(rule.getSlidingWindowType()); return Optional.of(new CircuitBreakerAdaptor(CircuitBreakerRegistry.of(CircuitBreakerConfig .custom() .failureRateThreshold(rule.getFailureRateThreshold()) @@ -47,12 +49,21 @@ protected final Optional createProcessor(String businessName, Ci .slowCallDurationThreshold(Duration.ofMillis(rule.getParsedSlowCallDurationThreshold())) .permittedNumberOfCallsInHalfOpenState(rule.getPermittedNumberOfCallsInHalfOpenState()) .minimumNumberOfCalls(rule.getMinimumNumberOfCalls()) - .slidingWindowType(getSlidingWindowType(rule.getSlidingWindowType())) - .slidingWindowSize((int) rule.getParsedSlidingWindowSize()) + .slidingWindowType(slidingWindowType) + .slidingWindowSize(getWindowSize(slidingWindowType, rule.getParsedSlidingWindowSize())) .build()) .circuitBreaker(businessName), rule)); } + private int getWindowSize(SlidingWindowType slidingWindowType, long parsedSlidingWindowSize) { + if (slidingWindowType == SlidingWindowType.COUNT_BASED) { + return (int) parsedSlidingWindowSize; + } + + // rest4j暂且仅支持秒作为时间窗口, 这里 parsedSlidingWindowSize为毫秒, 因此此处需转换成秒 + return (int) Duration.ofMillis(parsedSlidingWindowSize).getSeconds(); + } + private CircuitBreakerConfig.SlidingWindowType getSlidingWindowType(String type) { if (StringUtils.equalIgnoreCase(type, CircuitBreakerRule.SLIDE_WINDOW_COUNT_TYPE)) { return CircuitBreakerConfig.SlidingWindowType.COUNT_BASED;