Circuit Breaker Plugin
Plugin implementing Circuit Breaker
design pattern.
Request maker will have 3 states:
Closed
- all requests are passing to the next plugin allowing to create requestsOpen
- no requests are passing to next step, every request throws error from last 'real' requestHalf-Open
- only limited number of requests are allowed to actually executes, if these requests were successful state changes to Closed, otherwise goes back to Open
Api
Create options
getKey
: function [= () => ''] - allows to divide requests to different instances of Circuit Breaker, by default only one Circuit Breaker instance is createdisSystemError
: function [=() => true] specifies that error should be treated as a system error and therefore will lead to an increasing counter of failed requests in Circuit Breaker, if the error is not a system error then error is treated as normal behavior and therefore will lead to a decreasing counter of failed requestsfailureTimeout
: number [=120000] - time interval in which failed requests will considered to get statefailureThreshold
: number [=50] - percentage of failed requests insidefailureTimeout
interval, if that number is exceeded state changes to OpenminimumFailureCount
: number [=5] - number of minimum request which should be failed to consider stats from current time intervalopenTimeout
: number [=60000] - time interval in which all requests will forcedly fail, after that timeouthalfOpenThreshold
number of requests will be executed as usualhalfOpenThreshold
: number [=5] - percentage of requests allowed to execute while state is Half-Open
External meta
CIRCUIT_BREAKER.open
: boolean - flag indicating that current request was blocked by Circuit Breaker
Internal meta
CIRCUIT_BREAKER.breaker
- Circuit Breaker instance
How to
Base example
import request from '@tinkoff/request-core';
import circuitBreaker from '@tinkoff/request-plugin-circuit-breaker';
const req = request([
// ...plugins for any request transforms and cache
// should be set after transforming plugins and cache plugins
circuitBreaker(),
// should be set before protocol plugins
// ...plugins for making actual request
]);
Validators
import request from '@tinkoff/request-core';
import http, { isNetworkFail } from '@tinkoff/request-plugin-protocol-http';
import circuitBreaker from '@tinkoff/request-plugin-circuit-breaker';
const req = request([
// ...plugins for any request transforms and cache
// should be set after transforming plugins and cache plugins
circuitBreaker({
isSystemError: isNetworkFail,
}),
// should be set before protocol plugins
// ...plugins for making actual request
http(),
]);