Skip to content

Commit

Permalink
Merge pull request #683 from justforstudy-A/flow-cir-fix
Browse files Browse the repository at this point in the history
【fix】修复熔断时间窗口时间单位问题
  • Loading branch information
robotLJW authored Aug 27, 2022
2 parents e42c7ef + d82efe1 commit a26e277
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +40,7 @@
public class CircuitBreakerHandler extends AbstractRequestHandler<CircuitBreaker, CircuitBreakerRule> {
@Override
protected final Optional<CircuitBreaker> createProcessor(String businessName, CircuitBreakerRule rule) {
final SlidingWindowType slidingWindowType = getSlidingWindowType(rule.getSlidingWindowType());
return Optional.of(new CircuitBreakerAdaptor(CircuitBreakerRegistry.of(CircuitBreakerConfig
.custom()
.failureRateThreshold(rule.getFailureRateThreshold())
Expand All @@ -47,12 +49,21 @@ protected final Optional<CircuitBreaker> 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;
Expand Down

0 comments on commit a26e277

Please sign in to comment.