mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-05 17:53:12 +02:00
feat: initial counter
This commit is contained in:
parent
d7b2874afd
commit
aa3e49f8c5
47
src/lib/routes/client-api/request-counter.ts
Normal file
47
src/lib/routes/client-api/request-counter.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
datefns;
|
||||||
|
|
||||||
|
export default class RequestCounter {
|
||||||
|
private requestCache: Object;
|
||||||
|
private longTermCache: Object;
|
||||||
|
private interval: number;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.requestCache = {};
|
||||||
|
this.longTermCache = [];
|
||||||
|
|
||||||
|
this.interval = setInterval(() => {
|
||||||
|
this.requestCache.endTime = new Date();
|
||||||
|
|
||||||
|
const longTermCacheObject = {
|
||||||
|
...this.requestCache,
|
||||||
|
rps: this.calculateRPS(),
|
||||||
|
};
|
||||||
|
|
||||||
|
this.longTermCache.push(longTermCacheObject);
|
||||||
|
|
||||||
|
this.requestCache = this.createBucket();
|
||||||
|
}, 300000).unref();
|
||||||
|
}
|
||||||
|
|
||||||
|
createBucket = () => {
|
||||||
|
const bucket = {
|
||||||
|
count: 0,
|
||||||
|
startTime: new Date(),
|
||||||
|
endTime: null,
|
||||||
|
};
|
||||||
|
return bucket;
|
||||||
|
};
|
||||||
|
|
||||||
|
recordRequest = (appName: string): Promise<void> => {
|
||||||
|
if (this.requestCache[appName]) {
|
||||||
|
this.requestCache[count] += 1;
|
||||||
|
} else {
|
||||||
|
this.requestCache[appName] = { count: 1 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
calculateRPS = () => {
|
||||||
|
const { count } = this.requestCache;
|
||||||
|
return count / 300;
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user