mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 08:03:30 +02:00
Don't allow minicap to fail more than 3 times in 10s.
This commit is contained in:
32
lib/units/device/plugins/screen/util/failcounter.js
Normal file
32
lib/units/device/plugins/screen/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
|
||||
Reference in New Issue
Block a user