Skip to content

Commit

Permalink
Update publish-alpha (#3157)
Browse files Browse the repository at this point in the history
Co-authored-by: Kaustubh Kumar <[email protected]>
Co-authored-by: Eswar Prasad Clinton. A <[email protected]>
Co-authored-by: Ravi theja <[email protected]>
Co-authored-by: Amar Bathwal <[email protected]>
  • Loading branch information
5 people authored Aug 6, 2024
1 parent 7d0349d commit d13d261
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 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
19 changes: 14 additions & 5 deletions packages/hms-video-store/src/media/tracks/HMSLocalVideoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,19 +521,28 @@ export class HMSLocalVideoTrack extends HMSVideoTrack {
private handleVisibilityChange = async () => {
if (document.visibilityState === 'hidden') {
this.enabledStateBeforeBackground = this.enabled;
this.nativeTrack.enabled = false;
HMSLogger.d(this.TAG, 'visibility hidden muting track');
this.replaceSenderTrack(this.nativeTrack);
if (this.enabled) {
const track = await this.replaceTrackWithBlank();
await this.replaceSender(track, this.enabled);
this.nativeTrack?.stop();
this.nativeTrack = track;
} else {
await this.replaceSender(this.nativeTrack, false);
}
// started interruption event
this.eventBus.analytics.publish(
this.sendInterruptionEvent({
started: true,
}),
);
} else {
this.nativeTrack.enabled = this.enabledStateBeforeBackground;
HMSLogger.d(this.TAG, 'visibility visibile, restoring track state', this.enabledStateBeforeBackground);
this.replaceSender(this.nativeTrack, this.enabledStateBeforeBackground);
if (this.enabledStateBeforeBackground) {
await this.setEnabled(true);
} else {
this.nativeTrack.enabled = this.enabledStateBeforeBackground;
await this.replaceSender(this.nativeTrack, this.enabledStateBeforeBackground);
}
// started interruption event
this.eventBus.analytics.publish(
this.sendInterruptionEvent({
Expand Down

0 comments on commit d13d261

Please sign in to comment.