UNPKG

@dash0/sdk-web

Version:

Dash0's Web SDK to collect telemetry from end-users' web browsers

17 lines (16 loc) 657 B
import { setInterval } from "./timers"; export function createRateLimiter(opts) { const maxCallsPerTenMinutes = opts.maxCallsPerTenMinutes || 128; const maxCallsPerTenSeconds = opts.maxCallsPerTenSeconds || 32; let totalCallsInLastTenMinutes = 0; let totalCallsInLastTenSeconds = 0; setInterval(function () { totalCallsInLastTenMinutes = 0; }, 1000 * 60 * 10); setInterval(function () { totalCallsInLastTenSeconds = 0; }, 1000 * 10); return function isExcessiveUsage() { return ++totalCallsInLastTenMinutes > maxCallsPerTenMinutes || ++totalCallsInLastTenSeconds > maxCallsPerTenSeconds; }; }