@applitools/execution-grid-tunnel
Version:
Allows user to run tests with exection-grid and navigate to private hosts and ips
52 lines (42 loc) • 1.3 kB
JavaScript
class ErrorCircuitBreaker{
constructor({threshold = 3, timeout = 10000, logger} = {}) {
this._threshold = threshold
this._timeout = timeout
this._failureCount = 0
this._logger = logger
this.status = 'CLOSED'
this.waitForClosedState = Promise.resolve()
this._onOpenCallbacks = []
}
incErrorCounter(){
if (this.status === 'OPEN') return
this._failureCount++
if(this._failureCount < this._threshold) return
if (this._logger){
this._logger.warn({
action: 'circuit-breaker-was-opened',
threshold: this._threshold
})
}
this.status = 'OPEN'
this.waitForClosedState = new Promise(r => setTimeout(() => {
this.status = 'CLOSED'
this._logger.info({
action: 'circuit-breaker-was-closed',
threshold: this._threshold
})
r()
},this._timeout))
this._failureCount = 0
for(const cb of this._onOpenCallbacks){
cb()
}
}
resetErrorCounter(){
this._failureCount = 0
}
onOpen(cb){
this._onOpenCallbacks.push(cb)
}
}
module.exports = {ErrorCircuitBreaker}