Don't allow minicap to fail more than 3 times in 10s.

This commit is contained in:
Simo Kinnunen
2015-04-27 16:10:41 +09:00
parent 688ddda713
commit 2123a475e3
2 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
var util = require('util')
var EventEmitter = require('eventemitter3').EventEmitter
function FailCounter(threshold, time) {
this.threshold = threshold
this.time = time
this.values = []
}
util.inherits(FailCounter, EventEmitter)
FailCounter.prototype.inc = function() {
var now = Date.now()
while (this.values.length) {
if (now - this.values[0] >= this.time) {
this.values.shift()
}
else {
break
}
}
this.values.push(now)
if (this.values.length > this.threshold) {
this.emit('exceedLimit', this.threshold, this.time)
}
}
module.exports = FailCounter