Skip to content

Commit b7ab09f

Browse files
committed
Failed attempts clear interval.
1 parent f26233c commit b7ab09f

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

packages/p2p-media-loader-core/src/hybrid-loader.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import * as StreamUtils from "./utils/stream";
1212
import * as Utils from "./utils/utils";
1313
import debug from "debug";
1414

15+
const FAILED_ATTEMPTS_CLEAR_INTERVAL = 60000;
16+
1517
export class HybridLoader {
1618
private readonly requests: RequestsContainer;
1719
private readonly p2pLoaders: P2PLoadersContainer;
@@ -127,6 +129,7 @@ export class HybridLoader {
127129
private processRequests(queueSegmentIds: Set<string>) {
128130
const { stream } = this.lastRequestedSegment;
129131
const { httpErrorRetries } = this.settings;
132+
const now = performance.now();
130133
for (const request of this.requests.items()) {
131134
const {
132135
type,
@@ -187,6 +190,12 @@ export class HybridLoader {
187190
break;
188191
}
189192
request.markHandledByProcessQueue();
193+
if (
194+
now - request.failedAttempts.lastClearTimestamp >
195+
FAILED_ATTEMPTS_CLEAR_INTERVAL
196+
) {
197+
request.failedAttempts.clear();
198+
}
190199
}
191200
}
192201

@@ -294,7 +303,7 @@ export class HybridLoader {
294303
}
295304

296305
private loadRandomThroughHttp() {
297-
const { simultaneousHttpDownloads } = this.settings;
306+
const { simultaneousHttpDownloads, httpErrorRetries } = this.settings;
298307
const p2pLoader = this.p2pLoaders.currentLoader;
299308
const connectedPeersAmount = p2pLoader.connectedPeersAmount;
300309
if (
@@ -307,11 +316,17 @@ export class HybridLoader {
307316
lastRequestedSegment: this.lastRequestedSegment,
308317
playback: this.playback,
309318
settings: this.settings,
310-
skipSegment: (segment, statuses) =>
311-
!statuses.isHttpDownloadable ||
312-
this.segmentStorage.hasSegment(segment) ||
313-
this.requests.isHybridLoaderRequested(segment) ||
314-
p2pLoader.isLoadingOrLoadedBySomeone(segment),
319+
skipSegment: (segment, statuses) => {
320+
const request = this.requests.get(segment);
321+
return (
322+
!statuses.isHttpDownloadable ||
323+
this.segmentStorage.hasSegment(segment) ||
324+
request?.type !== undefined ||
325+
(request?.failedAttempts.httpAttemptsCount ?? 0) >=
326+
httpErrorRetries ||
327+
p2pLoader.isLoadingOrLoadedBySomeone(segment)
328+
);
329+
},
315330
});
316331
if (!queue.length) return;
317332
const peersAmount = connectedPeersAmount + 1;

packages/p2p-media-loader-core/src/request-container.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ export class RequestsContainer {
7373
}
7474
}
7575

76-
isHybridLoaderRequested(segment: Segment): boolean {
77-
return !!this.requests.get(segment)?.type;
78-
}
79-
8076
destroy() {
8177
for (const request of this.requests.values()) {
8278
request.abortFromProcessQueue();

packages/p2p-media-loader-core/src/request.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export class Request {
139139
if (this._engineCallbacks) {
140140
throw new Error("Segment is already requested by engine");
141141
}
142+
this.failedAttempts.clear();
142143
this._isHandledByProcessQueue = false;
143144
this._engineCallbacks = callbacks;
144145
}
@@ -311,7 +312,12 @@ export class Request {
311312
}
312313

313314
class FailedRequestAttempts {
314-
private readonly attempts: RequestAttempt[] = [];
315+
private attempts: RequestAttempt[] = [];
316+
private _lastClearTimestamp = performance.now();
317+
318+
get lastClearTimestamp() {
319+
return this._lastClearTimestamp;
320+
}
315321

316322
add(attempt: RequestAttempt) {
317323
this.attempts.push(attempt);
@@ -323,6 +329,11 @@ class FailedRequestAttempts {
323329
0
324330
);
325331
}
332+
333+
clear() {
334+
this.attempts = [];
335+
this._lastClearTimestamp = performance.now();
336+
}
326337
}
327338

328339
const requestInnerErrorTypes = ["abort", "bytes-receiving-timeout"] as const;

0 commit comments

Comments
 (0)