mirror of
https://github.com/openstf/stf
synced 2025-10-04 02:09:32 +02:00
Move some screen utilities to the global util directory so that they can be used for other things too.
This commit is contained in:
parent
ae9f2b5ab0
commit
5bb05f52f6
4 changed files with 3 additions and 3 deletions
32
lib/util/failcounter.js
Normal file
32
lib/util/failcounter.js
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue