Skip to content

Commit

Permalink
Failed attempts clear interval.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Dec 29, 2023
1 parent f26233c commit b7ab09f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
27 changes: 21 additions & 6 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import * as StreamUtils from "./utils/stream";
import * as Utils from "./utils/utils";
import debug from "debug";

const FAILED_ATTEMPTS_CLEAR_INTERVAL = 60000;

export class HybridLoader {
private readonly requests: RequestsContainer;
private readonly p2pLoaders: P2PLoadersContainer;
Expand Down Expand Up @@ -127,6 +129,7 @@ export class HybridLoader {
private processRequests(queueSegmentIds: Set<string>) {
const { stream } = this.lastRequestedSegment;
const { httpErrorRetries } = this.settings;
const now = performance.now();
for (const request of this.requests.items()) {
const {
type,
Expand Down Expand Up @@ -187,6 +190,12 @@ export class HybridLoader {
break;
}
request.markHandledByProcessQueue();
if (
now - request.failedAttempts.lastClearTimestamp >
FAILED_ATTEMPTS_CLEAR_INTERVAL
) {
request.failedAttempts.clear();
}
}
}

Expand Down Expand Up @@ -294,7 +303,7 @@ export class HybridLoader {
}

private loadRandomThroughHttp() {
const { simultaneousHttpDownloads } = this.settings;
const { simultaneousHttpDownloads, httpErrorRetries } = this.settings;
const p2pLoader = this.p2pLoaders.currentLoader;
const connectedPeersAmount = p2pLoader.connectedPeersAmount;
if (
Expand All @@ -307,11 +316,17 @@ export class HybridLoader {
lastRequestedSegment: this.lastRequestedSegment,
playback: this.playback,
settings: this.settings,
skipSegment: (segment, statuses) =>
!statuses.isHttpDownloadable ||
this.segmentStorage.hasSegment(segment) ||
this.requests.isHybridLoaderRequested(segment) ||
p2pLoader.isLoadingOrLoadedBySomeone(segment),
skipSegment: (segment, statuses) => {
const request = this.requests.get(segment);
return (
!statuses.isHttpDownloadable ||
this.segmentStorage.hasSegment(segment) ||
request?.type !== undefined ||
(request?.failedAttempts.httpAttemptsCount ?? 0) >=
httpErrorRetries ||
p2pLoader.isLoadingOrLoadedBySomeone(segment)
);
},
});
if (!queue.length) return;
const peersAmount = connectedPeersAmount + 1;
Expand Down
4 changes: 0 additions & 4 deletions packages/p2p-media-loader-core/src/request-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ export class RequestsContainer {
}
}

isHybridLoaderRequested(segment: Segment): boolean {
return !!this.requests.get(segment)?.type;
}

destroy() {
for (const request of this.requests.values()) {
request.abortFromProcessQueue();
Expand Down
13 changes: 12 additions & 1 deletion packages/p2p-media-loader-core/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class Request {
if (this._engineCallbacks) {
throw new Error("Segment is already requested by engine");
}
this.failedAttempts.clear();
this._isHandledByProcessQueue = false;
this._engineCallbacks = callbacks;
}
Expand Down Expand Up @@ -311,7 +312,12 @@ export class Request {
}

class FailedRequestAttempts {
private readonly attempts: RequestAttempt[] = [];
private attempts: RequestAttempt[] = [];
private _lastClearTimestamp = performance.now();

get lastClearTimestamp() {
return this._lastClearTimestamp;
}

add(attempt: RequestAttempt) {
this.attempts.push(attempt);
Expand All @@ -323,6 +329,11 @@ class FailedRequestAttempts {
0
);
}

clear() {
this.attempts = [];
this._lastClearTimestamp = performance.now();
}
}

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

0 comments on commit b7ab09f

Please sign in to comment.