Skip to content

Commit

Permalink
Doc: add code snippet for Sliding Count Breaker and Sliding Time Breaker
Browse files Browse the repository at this point in the history
  • Loading branch information
tichon29 committed Apr 16, 2021
1 parent 19e3f28 commit 851e9ca
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
50 changes: 48 additions & 2 deletions docs/content/api/module/breaker/sliding/count.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,60 @@ The Circuit Breaker has 3 possible states:
* OPEN
* HALF_OPEN

When the circuit is opened, all the requests are failing fast
When the circuit is opened, all the iterations are failing fast

When the circuit is half opened, a certain number of requests are authorrized. When this number is reached, the failure and slow call rate thresholds are checked to see if the circuit should be opened or closed.
When the circuit is half opened, a certain number of iterations are authorrized. When this number is reached, the failure and slow call rate thresholds are checked to see if the circuit should be opened or closed.

When the circuit is closed, a sliding window is used to store the outcome of calls.
The count-based sliding window calculates the outcome of the last N calls, and decides if the circuit should be opened (if the failure or slow call rate thresholds are exceeded)
For example, if the count window size is 10, the circular array has always 10 measurements.

``` javascript
// Imports needed components
const { Circuit, SlidingCountBreaker } = require('mollitia');
// Creates Sliding Count Breaker Module
const slidingCountBreaker = new SlidingCountBreaker({
name: 'my-sliding-count-breaker',
slidingWindowSize: 6, // Failure Rate Calculation is done on the last 6 iterations
minimumNumberOfCalls: 3, // 3 iterations are needed to start calculating the failure rate, and see if circuit should be opened or not
failureRateThreshold: 50, // If half of the iterations or more are failing, the circuit is switched to Opened state.
slowCallDurationThreshold: 1000, // An iteration is considered as being slow if the iteration lasts more than 1s
slowCallRateThreshold: 80, // If at least 80% of the iterations are considered as being slow, the circuit is switched to Opened state.
permittedNumberOfCallsInHalfOpenState: 2, // When the circuit is in Half Opened state, the circuit accepts 2 iterations in this state.
// Once these 2 iterations are received, failure rate is calculated on these iterations.
// If failure rate is lower than failureRateThreshold, the circuit is switched to Closed state.
// If the failure rate is higher or equal to failureRateThreshold, the circuit is switched to Opened state.
openStateDelay: 10000 // The circuit stays in Opened state for 10s
});
// Creates a circuit
const circuit = new Circuit({
name: 'my-circuit',
options: {
modules: [ slidingCountBreaker ]
}
});

await circuit.fn(myFunction).execute();
await circuit.fn(myFunction2).execute();
// When this 3rd iteration is received, failureRate and slowCallRate is calculated
// If the 3 iterations lasts less than 1s and are all success, then the circuit is still closed
// The circuit is opened if:
// - at least 2 iterations failed (failure rate is 66% (2 iterations failing) or 100% (3 iterations failing), which is > 50 (failureRateThreshold))
// - or all the iterations are slow (as slowCallRateThreshold is 80%)
await circuit.fn(myFunction3).execute();
// If
// - circuit is still closed, then the failure rate and slow call rate threshold will be calculated when a new iteration is received
// - circuit is opened, then the iterations are failing fast
// Here, 4th iteration received. Calculation will be done on the 4th iterations
await circuit.fn(myFunction4).execute();
// Here, 5th iteration received. Calculation will be done on the 4 iterations
await circuit.fn(myFunction5).execute();
// Here, 6th iteration received. Calculation will be done on the 6 iterations
await circuit.fn(myFunction6).execute();
// Here, number of iterations is 7. So, the 1st iteration is no longer taken into account and the calculation is done on iterations 2 to 7
await circuit.fn(myFunction7).execute();
```

<p class="flex-center-row" align="center"><pg-img src="/img/circuit-breaker-diagram.png" alt="Circuit Breaker - Diagram"></pg-img></p>

## Options
Expand Down
56 changes: 52 additions & 4 deletions docs/content/api/module/breaker/sliding/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,61 @@ The Circuit Breaker has 3 possible states:
* OPEN
* HALF_OPEN

When the circuit is opened, all the requests are failing fast
When the circuit is opened, all the iterations are failing fast

When the circuit is half opened, a certain number of requests are authorized. When this number is reached, the failure and slow call rate thresholds are checked to see if the circuit should be opened or closed.
When the circuit is half opened, a certain number of iterations are authorized. When this number is reached, the failure and slow call rate thresholds are checked to see if the circuit should be opened or closed.

When the circuit is closed, a sliding window is used to store the outcome of calls.
The time-based sliding window calculates the outcome of the last calls receiving during the last N milliseconds, and decides if the circuit should be opened (if the failure or slow call rate thresholds are exceeded)
For example, if the count window size is 10000, the circular array stores the requests that occurred during the last 10s (with a maximum of 1000 elements in the array)
The time-based sliding window calculates the outcome of the last calls received during the last N milliseconds, and decides if the circuit should be opened (if the failure or slow call rate thresholds are exceeded)
For example, if the count window size is 10000, the circular array stores the iterations that occurred during the last 10s (with a maximum of 1000 elements in the array)

``` javascript
// Imports needed components
const { Circuit, SlidingTimeBreaker } = require('mollitia');
// Creates Sliding Count Breaker Module
const slidingTimeBreaker = new SlidingTimeBreaker({
name: 'my-sliding-time-breaker',
slidingWindowSize: 30000, // Failure Rate Calculation is done on the iterations received during the last 30s.
minimumNumberOfCalls: 2, // 2 iterations are needed to start calculating the failure rate, and see if circuit should be opened or not
failureRateThreshold: 60, // If at least 60% of the iterations are failing, the circuit is switched to Opened state.
slowCallDurationThreshold: 500, // An iteration is considered as being slow if the iteration lasts more than 0.5s
slowCallRateThreshold: 40, // If at least 40% of the iterations are considered as being slow, the circuit is switched to Opened state.
permittedNumberOfCallsInHalfOpenState: 2, // When the circuit is in Half Opened state, the circuit accepts 2 iterations in this state.
// Once these 2 iterations are received, failure rate is calculated on these iterations.
// If failure rate is lower than failureRateThreshold, the circuit is switched to Closed.
// If the failure rate is higher or equal to failureRateThreshold, the circuit is switched to Opened.
openStateDelay: 10000 // The circuit stays in Opened state for 10s
});
// Creates a circuit
const circuit = new Circuit({
name: 'my-circuit',
options: {
modules: [ slidingCountBreaker ]
}
});
// Time is t0
await circuit.fn(myFunction).execute();
// 10s later (t0 + 10s)
// When this 2nd iteration is received, failureRate and slowCallRate is calculated
// If the 2 iterations lasts less than 500ms and are all success, then the circuit is still closed
// The circuit is opened if:
// - the 2 iterations failed (as failureRateThreshold is 60%)
// - at least 1 iteration is slow (as slowCallRateThreshold is 40%)
await circuit.fn(myFunction2).execute();
// 10s later (t0 + 20s)
// Elapsed time since 1st iteration is lower than 30s
// If circuit is still closed, the failureRate and slowCallRate threshold is calculated on the 3 iterations
await circuit.fn(myFunction3).execute();
// 5s later (t0 + 25s)
// Elapsed time since 1st iteration is lower than 30s
// If circuit is still closed, the failureRate and slowCallRate threshold is calculated on the 4 iterations
await circuit.fn(myFunction4).execute();
// 10s later (t0 + 35s)
// Elapsed time since 1st iteration is greater than 30s. Elapsed time since 2nd iteration is lower than 30s
// If circuit is still closed, the failureRate and slowCallRate threshold is now calculated on iterations from 2 to 5, as iteration 1 is too old
await circuit.fn(myFunction5).execute();

```

<p class="flex-center-row" align="center"><pg-img src="/img/circuit-breaker-diagram.png" alt="Circuit Breaker - Diagram"></pg-img></p>

Expand Down

0 comments on commit 851e9ca

Please sign in to comment.