near-protocol-rewards
Version:
A transparent, metric-based rewards system for NEAR projects
35 lines (34 loc) • 1.01 kB
JavaScript
;
/**
* Rate limiter configuration and implementation
* for controlling API request rates
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimiter = void 0;
class RateLimiter {
constructor(config) {
this.requests = 0;
this.lastReset = Date.now();
this.maxRequests = config.maxRequestsPerSecond;
this.timeWindow = config.timeWindowMs || 1000;
this.retryAfter = config.retryAfterMs || 1000;
}
async acquire() {
const now = Date.now();
if (now - this.lastReset >= this.timeWindow) {
this.requests = 0;
this.lastReset = now;
}
if (this.requests >= this.maxRequests) {
await new Promise((resolve) => setTimeout(resolve, this.retryAfter));
return this.acquire();
}
this.requests++;
}
async release() {
if (this.requests > 0) {
this.requests--;
}
}
}
exports.RateLimiter = RateLimiter;