Skip to content

Commit c477ed1

Browse files
committed
add safeguard to unrecoverable analytics
1 parent 5c1b2d3 commit c477ed1

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

peer/proto

peer/src/google/protobuf/timestamp.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// @ts-nocheck
2+
// @ts-nocheck
3+
// @ts-nocheck
24
// @generated by protobuf-ts 2.9.4 with parameter client_generic
35
// @generated from protobuf file "google/protobuf/timestamp.proto" (package "google.protobuf", syntax proto3)
46
// tslint:disable

peer/src/peer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import {
1515
import { asleep, retry } from "./util.ts";
1616
import { jwtDecode } from "jwt-decode";
1717
import { calculateWebRTCQuality, collectWebRTCStats } from "./quality.ts";
18-
import { Timestamp } from "./google/protobuf/timestamp.ts";
1918

2019
export type { PeerInfo } from "./signaling.ts";
2120

22-
const ANALYTICS_POLL_INTERVAL_MS = 5_000;
21+
const ANALYTICS_POLL_INTERVAL_MS = 60_000;
2322

2423
/**
2524
* Streamline real-time application development.`@pulsebeam/peer` abstracts
@@ -327,12 +326,12 @@ export class Peer {
327326
const events: AnalyticsEvent[] = [];
328327
for (const sess of this.sessions) {
329328
const rawStats = await sess.getStats();
330-
const at = Timestamp.now();
329+
const at = Date.now() * 1_000;
331330
const stats = collectWebRTCStats(rawStats);
332331
const quality = calculateWebRTCQuality(stats);
333332

334333
events.push({
335-
timestamp: at,
334+
timestampUs: BigInt(at),
336335
tags: {
337336
src: this.transport.info,
338337
dst: sess.other,

peer/src/quality.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ export function calculateWebRTCQuality(stats: WebRTCStats): number {
9292
};
9393

9494
const aggregateAndScore = (channelStats: ChannelStats[]): number => {
95-
if (channelStats.length === 0) return 100;
95+
// no data, we assume not connecting
96+
if (channelStats.length === 0) return 0;
9697

9798
const aggregated = aggregateChannelStats(channelStats);
9899

peer/src/signaling.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// tslint:disable
44
import { ServiceType } from "@protobuf-ts/runtime-rpc";
55
import { MessageType } from "@protobuf-ts/runtime";
6-
import { Timestamp } from "./google/protobuf/timestamp.ts";
76
/**
87
* @generated from protobuf message pulsebeam.v1.PrepareReq
98
*/
@@ -326,9 +325,9 @@ export interface AnalyticsReportReq {
326325
*/
327326
export interface AnalyticsEvent {
328327
/**
329-
* @generated from protobuf field: google.protobuf.Timestamp timestamp = 1;
328+
* @generated from protobuf field: sint64 timestamp_us = 1;
330329
*/
331-
timestamp?: Timestamp;
330+
timestampUs: bigint;
332331
/**
333332
* @generated from protobuf field: pulsebeam.v1.AnalyticsTags tags = 2;
334333
*/
@@ -688,7 +687,7 @@ export const AnalyticsReportReq = new AnalyticsReportReq$Type();
688687
class AnalyticsEvent$Type extends MessageType<AnalyticsEvent> {
689688
constructor() {
690689
super("pulsebeam.v1.AnalyticsEvent", [
691-
{ no: 1, name: "timestamp", kind: "message", T: () => Timestamp },
690+
{ no: 1, name: "timestamp_us", kind: "scalar", T: 18 /*ScalarType.SINT64*/, L: 0 /*LongType.BIGINT*/ },
692691
{ no: 2, name: "tags", kind: "message", T: () => AnalyticsTags },
693692
{ no: 3, name: "metrics", kind: "message", T: () => AnalyticsMetrics }
694693
]);

peer/src/transport.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const POLL_TIMEOUT_MS = 900000;
1515
const POLL_RETRY_BASE_DELAY_MS = 50;
1616
const POLL_RETRY_MAX_DELAY_MS = 1000;
1717

18+
const ANALYTICS_POLL_RETRY_BASE_DELAY_MS = 1000;
19+
const ANALYTICS_POLL_RETRY_MAX_DELAY_MS = 4000;
20+
1821
export enum ReservedConnId {
1922
Discovery = 0,
2023
Max = 16,
@@ -339,9 +342,9 @@ export class Transport {
339342
timeout: POLL_TIMEOUT_MS,
340343
};
341344
const retryOpt: RetryOptions = {
342-
baseDelay: POLL_RETRY_BASE_DELAY_MS,
343-
maxDelay: POLL_RETRY_MAX_DELAY_MS,
344-
maxRetries: -1,
345+
baseDelay: ANALYTICS_POLL_RETRY_BASE_DELAY_MS,
346+
maxDelay: ANALYTICS_POLL_RETRY_MAX_DELAY_MS,
347+
maxRetries: 3,
345348
abortSignal: this.abort.signal,
346349
isRecoverable: this.isRecoverable,
347350
};
@@ -360,8 +363,10 @@ export class Transport {
360363

361364
return;
362365
} catch (err) {
363-
this.logger.error("unrecoverable error, force closing", { err });
364-
this.close();
366+
this.logger.error(
367+
"analytics backend is in unrecoverable state, dropping analytics event",
368+
{ err },
369+
);
365370
return;
366371
}
367372
}

0 commit comments

Comments
 (0)