everything
This commit is contained in:
36
src/RateLimiter.ts
Normal file
36
src/RateLimiter.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { EventEmitter } from "stream";
|
||||
|
||||
// Class to ratelimit a resource (chatting, logging in, etc)
|
||||
export default class RateLimiter extends EventEmitter {
|
||||
private limit : number;
|
||||
private interval : number;
|
||||
private requestCount : number;
|
||||
private limiter? : NodeJS.Timer;
|
||||
private limiterSet : boolean;
|
||||
constructor(limit : number, interval : number) {
|
||||
super();
|
||||
this.limit = limit;
|
||||
this.interval = interval;
|
||||
this.requestCount = 0;
|
||||
this.limiterSet = false;
|
||||
}
|
||||
// Return value is whether or not the action should be continued
|
||||
request() : boolean {
|
||||
this.requestCount++;
|
||||
if (this.requestCount === this.limit) {
|
||||
this.emit('limit');
|
||||
clearTimeout(this.limiter);
|
||||
this.limiterSet = false;
|
||||
this.requestCount = 0;
|
||||
return false;
|
||||
}
|
||||
if (!this.limiterSet) {
|
||||
this.limiter = setTimeout(() => {
|
||||
this.limiterSet = false;
|
||||
this.requestCount = 0;
|
||||
}, this.interval * 1000);
|
||||
this.limiterSet = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user