| 1 | export const RESTART_BUDGET_MAX = 3; |
| 2 | export const RESTART_BUDGET_WINDOW_MS = 5 * 60 * 1000; |
| 3 | |
| 4 | export class RestartBudget { |
| 5 | private stamps: number[] = []; |
| 6 | |
| 7 | constructor(private readonly max = RESTART_BUDGET_MAX, private readonly windowMs = RESTART_BUDGET_WINDOW_MS) {} |
| 8 | |
| 9 | allow(now = Date.now()): boolean { |
| 10 | this.stamps = this.stamps.filter((stamp) => now - stamp < this.windowMs); |
| 11 | if (this.stamps.length >= this.max) return false; |
| 12 | this.stamps.push(now); |
| 13 | return true; |
| 14 | } |
| 15 | } |
| 16 |