@rexxars/p-ratelimit
Version:
Promise-based utility to make sure you don’t call rate-limited APIs too quickly.
37 lines • 1.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getQuotaManager = void 0;
var dequeue_1 = require("../dequeue");
function getQuotaManager(quota) {
var activeCount = 0;
var history = dequeue_1.dequeue();
function start() {
if (activeCount >= quota.concurrency) {
return false;
}
if (quota.interval !== undefined && quota.rate !== undefined) {
removeExpiredHistory();
if (history.length() >= quota.rate) {
return false;
}
history.push(Date.now());
}
activeCount++;
return true;
}
function end() {
activeCount--;
}
function maxDelay() {
return quota.maxDelay || 0;
}
function removeExpiredHistory() {
var expired = Date.now() - quota.interval;
while (history.length && history.peekFront() < expired) {
history.shift();
}
}
return { activeCount: function () { return activeCount; }, quota: quota, maxDelay: maxDelay, start: start, end: end };
}
exports.getQuotaManager = getQuotaManager;
//# sourceMappingURL=quotaManager.js.map