Skip to content

Commit

Permalink
fix: rate limit events
Browse files Browse the repository at this point in the history
  • Loading branch information
KaustubhKumar05 authored Aug 5, 2024
1 parent 3f036fd commit be0dfd7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/hms-video-store/src/analytics/AnalyticsTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,30 @@ export abstract class AnalyticsTransport {
abstract failedEvents: Queue<AnalyticsEvent>;
private readonly TAG = '[AnalyticsTransport]';

private eventCount = 0;
private lastResetTime: number = Date.now();
private readonly MAX_EVENTS_PER_MINUTE: number = 200;
private readonly RESET_INTERVAL_MS: number = 60000;

private checkRateLimit() {
const now = Date.now();
if (now - this.lastResetTime >= this.RESET_INTERVAL_MS) {
this.eventCount = 0;
this.lastResetTime = now;
}
if (this.eventCount >= this.MAX_EVENTS_PER_MINUTE) {
throw new Error('Too many events being sent, please check the implementation.');
}
this.eventCount++;
}

sendEvent(event: AnalyticsEvent) {
try {
this.checkRateLimit();
} catch (e) {
HMSLogger.w(this.TAG, 'Rate limit exceeded', e);
throw e;
}
try {
this.sendSingleEvent(event);
this.flushFailedEvents();
Expand Down

0 comments on commit be0dfd7

Please sign in to comment.