xys-monitor
Version:
A project named xys-monitor
48 lines (41 loc) • 1.08 kB
JavaScript
import config from './config'
class Limit {
constructor () {
this.keyMap = new Map()
this.configCount = config.reportLimitConfig.count
this.configTime = config.reportLimitConfig.time
}
check (key) {
if (!key) {
return false
}
let detail = this.keyMap.get(key)
if (!detail) {
detail = {
count: 0,
timestamp: (new Date()).getTime()
}
}
detail.count++
this.keyMap.set(key, detail)
let now = (new Date()).getTime()
if (now - detail.timestamp < this.configTime) {
if (detail.count > this.configCount) {
return false
}
} else {
this.reset(key)
}
return true
}
reset (key) {
this.keyMap.set(key, {
count: 1,
timestamp: (new Date()).getTime()
})
}
clear (key) {
this.keyMap.delete(key)
}
}
export default new Limit()