1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-03 09:49:17 +02:00
OpenSTF/lib/util/failcounter.js
Gunther Brunner 711ab4a8e5 Updated npm dependencies.
ProtobufJS and Bluebird require further work.
2016-08-03 20:08:30 +09:00

33 lines
620 B
JavaScript

var util = require('util')
var EventEmitter = require('eventemitter3')
function FailCounter(threshold, time) {
EventEmitter.call(this)
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