滑动window大小和最小调用次数的区别

Difference between sliding window size and minimum number of calls

我是断路器的新手,最近在我的一项服务中实现了它们。我正在浏览文档 Resilience 4J official documentation 并找到了我们可以为断路器配置的两个属性。

  1. 滑动窗口大小
  2. minimumNumberOfCalls

以上两个属性都指定了必须对服务进行的调用次数,以确定断路器是应该保持打开还是应该关闭。 我需要了解这两个属性之间的细微差别以及它们之间可能存在的任何关系?应该相互独立配置还是两者之间应该有关系?

另外,以上两者和permittedNumberOfCallsInHalfOpenState有关系吗?例如,如果我将 permittedNumberOfCallsInHalfOpenState 配置为 5,但我的 slidingWindowSize/minimumNumberOfCalls 配置为 10,那么如何重新验证断路器状态?因为它至少需要 10 个请求才能重新评估断路器的新状态,但是当它处于打开状态时我们只允许 5 个请求?

This answer 来自 Resilience4j 的 之父 帮助了我:

In a production system you should not set minimumNumberOfCalls to 1. For testing it is okay, but 3 is better.

Let's assume you have minimumNumberOfCalls=3, slidingWindowSize = 10 and slidingWindowType = COUNT_BASED: That means the CircuitBreaker is calculating the failure rate and slow call rate based on the last 10 calls, as soon as 3 calls have been recorded.

Let's assume 2 calls are slow and 1 call is fast: That means the slow call rate is above 50% and the CircuitBreaker will transition to OPEN.

The minimumNumberOfCalls setting makes even more sense, if slidingWindowType = TIME_BASED and the failure rate is calculated based on the calls from the last N seconds.

至于permittedNumberOfCallsInHalfOpenState题,在wait-duration-in-open-state期结束后进入max-wait-duration-in-half-open-state期,最多可调用5次。只要阈值不是met/exceeded,电路就会关闭。它不依赖于 slidingWindowSize/minimumNumberOfCalls 根据 here.

The CircuitBreaker rejects calls with a CallNotPermittedException when it is OPEN. After a wait time duration has elapsed, the CircuitBreaker state changes from OPEN to HALF_OPEN and permits a configurable number of calls to see if the backend is still unavailable or has become available again. Further calls are rejected with a CallNotPermittedException, until all permitted calls have completed. If the failure rate or slow call rate is then equal or greater than the configured threshold, the state changes back to OPEN. If the failure rate and slow call rate is below the threshold, the state changes back to CLOSED.

P.S 如果不配置任何属性,则使用默认值here