Skip to content

Commit 6251c29

Browse files
committed
feat: reuse task scheduler to clean up parser stats
1 parent 3fecb18 commit 6251c29

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

src/NetworkScoresCalculator.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ import {
55
INetworkScoresCalculator,
66
WebRTCStatsParsed,
77
} from './types';
8+
import { scheduleTask } from './utils/tasks';
9+
import { CLEANUP_PREV_STATS_TTL_MS } from './utils/constants';
810

911
class NetworkScoresCalculator implements INetworkScoresCalculator {
1012
#lastProcessedStats: { [connectionId: string]: WebRTCStatsParsed } = {};
1113

1214
calculate(data: WebRTCStatsParsed): NetworkScores {
15+
const { connection: { id: connectionId } } = data;
1316
const outbound = this.calculateOutboundScore(data);
1417
const inbound = this.calculateInboundScore(data);
15-
this.#lastProcessedStats[data.connection.id] = data;
18+
this.#lastProcessedStats[connectionId] = data;
19+
20+
scheduleTask({
21+
taskId: connectionId,
22+
delayMs: CLEANUP_PREV_STATS_TTL_MS,
23+
callback: () => (delete this.#lastProcessedStats[connectionId]),
24+
});
25+
1626
return { outbound, inbound };
1727
}
1828

src/parser/RTCStatsParser.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
Logger,
1616
} from '../types';
1717
import { checkIsConnectionClosed, calcBitrate } from './utils';
18+
import { scheduleTask } from '../utils/tasks';
19+
import { CLEANUP_PREV_STATS_TTL_MS } from '../utils/constants';
1820

1921
interface PrevStatsItem {
2022
stats: WebRTCStatsParsed;
@@ -23,14 +25,11 @@ interface PrevStatsItem {
2325

2426
interface WebRTCStatsParserParams {
2527
logger: Logger;
26-
prevConnectionStatsTtlMs?: number;
2728
}
2829

2930
class RTCStatsParser implements StatsParser {
3031
private readonly prevStats = new Map<string, PrevStatsItem | undefined>();
3132

32-
private readonly prevStatsCleanupTimers = new Map<string, NodeJS.Timer>();
33-
3433
private readonly allowedReportTypes: Set<RTCStatsType> = new Set<RTCStatsType>([
3534
'candidate-pair',
3635
'inbound-rtp',
@@ -43,11 +42,8 @@ class RTCStatsParser implements StatsParser {
4342

4443
private readonly logger: Logger;
4544

46-
private readonly prevConnectionStatsTtlMs: number;
47-
4845
constructor(params: WebRTCStatsParserParams) {
4946
this.logger = params.logger;
50-
this.prevConnectionStatsTtlMs = params.prevConnectionStatsTtlMs ?? 55_000;
5147
}
5248

5349
get previouslyParsedStatsConnectionsIds(): string[] {
@@ -128,7 +124,11 @@ class RTCStatsParser implements StatsParser {
128124
ts: Date.now(),
129125
});
130126

131-
this.resetStatsCleanupTimer(connectionId);
127+
scheduleTask({
128+
taskId: connectionId,
129+
delayMs: CLEANUP_PREV_STATS_TTL_MS,
130+
callback: () => (this.prevStats.delete(connectionId)),
131+
});
132132

133133
return mappedStats;
134134
}
@@ -287,21 +287,6 @@ class RTCStatsParser implements StatsParser {
287287

288288
return connectionStats as ParsedConnectionStats;
289289
}
290-
291-
private resetStatsCleanupTimer(connectionId: string): void {
292-
const existingTimer = this.prevStatsCleanupTimers.get(connectionId);
293-
294-
if (existingTimer) {
295-
clearTimeout(existingTimer);
296-
}
297-
298-
const timer = setTimeout(() => {
299-
this.prevStats.delete(connectionId);
300-
this.prevStatsCleanupTimers.delete(connectionId);
301-
}, this.prevConnectionStatsTtlMs);
302-
303-
this.prevStatsCleanupTimers.set(connectionId, timer);
304-
}
305290
}
306291

307292
export default RTCStatsParser;

test/parser/RTCStatsParser.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('wid/lib/parser/RTCStatsParser', () => {
103103
const clock = sandbox.useFakeTimers();
104104

105105
await parser.parse(payload);
106-
await clock.tickAsync(54_999);
106+
await clock.tickAsync(34_999);
107107

108108
expect(parser.previouslyParsedStatsConnectionsIds).to.deep.eq([payload.id]);
109109
});
@@ -114,7 +114,7 @@ describe('wid/lib/parser/RTCStatsParser', () => {
114114
const clock = sandbox.useFakeTimers();
115115

116116
await parser.parse(payload);
117-
await clock.tickAsync(55_000);
117+
await clock.tickAsync(35_000);
118118

119119
expect(parser.previouslyParsedStatsConnectionsIds).to.deep.eq([]);
120120
});

0 commit comments

Comments
 (0)