Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【fix】修复熔断时间窗口时间单位问题 #683

Merged
merged 1 commit into from
Aug 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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