feat: DoS prevention, #250

This commit is contained in:
Yang Jun
2024-07-09 22:51:11 +08:00
committed by Jun Yang
parent fad00aa14e
commit e443068cb9
31 changed files with 476 additions and 140 deletions
+18
View File
@@ -0,0 +1,18 @@
import { assert } from './assert'
export class Limiter {
private message: string
private base = 0
private limit: number
constructor (resource: string, limit: number) {
this.message = `${resource} limit exceeded`
this.limit = limit
}
use (count: number) {
assert(this.base + count <= this.limit, this.message)
this.base += count
}
check (count: number) {
assert(count <= this.limit, this.message)
}
}