Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax TTFB timeout on manifest request #5364

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export const hlsDefaultConfig: HlsConfig = {
},
manifestLoadPolicy: {
default: {
maxTimeToFirstByteMs: 10000,
maxTimeToFirstByteMs: Infinity,
robwalch marked this conversation as resolved.
Show resolved Hide resolved
maxLoadTimeMs: 20000,
timeoutRetry: {
maxNumRetry: 2,
Expand Down
10 changes: 7 additions & 3 deletions src/utils/fetch-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ class FetchLoader implements Loader<LoaderContext> {
callbacks.onProgress;
const isArrayBuffer = context.responseType === 'arraybuffer';
const LENGTH = isArrayBuffer ? 'byteLength' : 'length';
const { maxTimeToFirstByteMs, maxLoadTimeMs } = config.loadPolicy;

this.context = context;
this.config = config;
this.callbacks = callbacks;
this.request = this.fetchSetup(context, initParams);
self.clearTimeout(this.requestTimeout);
config.timeout = config.loadPolicy.maxTimeToFirstByteMs;
config.timeout =
maxTimeToFirstByteMs && Number.isFinite(maxTimeToFirstByteMs)
? maxTimeToFirstByteMs
: maxLoadTimeMs;
this.requestTimeout = self.setTimeout(() => {
this.abortInternal();
callbacks.onTimeout(stats, context, this.response);
Expand All @@ -104,11 +108,11 @@ class FetchLoader implements Loader<LoaderContext> {
const first = Math.max(self.performance.now(), stats.loading.start);

self.clearTimeout(this.requestTimeout);
config.timeout = config.loadPolicy.maxLoadTimeMs;
config.timeout = maxLoadTimeMs;
this.requestTimeout = self.setTimeout(() => {
this.abortInternal();
callbacks.onTimeout(stats, context, this.response);
}, config.loadPolicy.maxLoadTimeMs - (first - stats.loading.start));
}, maxLoadTimeMs - (first - stats.loading.start));

if (!response.ok) {
const { status, statusText } = response;
Expand Down
24 changes: 15 additions & 9 deletions src/utils/xhr-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class XhrLoader implements Loader<LoaderContext> {
}

const headers = this.context.headers;
const { maxTimeToFirstByteMs, maxLoadTimeMs } = config.loadPolicy;
if (headers) {
for (const header in headers) {
xhr.setRequestHeader(header, headers[header]);
Expand All @@ -145,10 +146,13 @@ class XhrLoader implements Loader<LoaderContext> {
xhr.responseType = context.responseType as XMLHttpRequestResponseType;
// setup timeout before we perform request
self.clearTimeout(this.requestTimeout);
config.timeout = config.loadPolicy.maxTimeToFirstByteMs;
config.timeout =
maxTimeToFirstByteMs && Number.isFinite(maxTimeToFirstByteMs)
? maxTimeToFirstByteMs
: maxLoadTimeMs;
this.requestTimeout = self.setTimeout(
this.loadtimeout.bind(this),
config.loadPolicy.maxTimeToFirstByteMs
config.timeout
);
xhr.send();
}
Expand All @@ -174,13 +178,15 @@ class XhrLoader implements Loader<LoaderContext> {
stats.loading.start
);
// readyState >= 2 AND readyState !==4 (readyState = HEADERS_RECEIVED || LOADING) rearm timeout as xhr not finished yet
self.clearTimeout(this.requestTimeout);
config.timeout = config.loadPolicy.maxLoadTimeMs;
this.requestTimeout = self.setTimeout(
this.loadtimeout.bind(this),
config.loadPolicy.maxLoadTimeMs -
(stats.loading.first - stats.loading.start)
);
if (config.timeout !== config.loadPolicy.maxLoadTimeMs) {
self.clearTimeout(this.requestTimeout);
config.timeout = config.loadPolicy.maxLoadTimeMs;
this.requestTimeout = self.setTimeout(
this.loadtimeout.bind(this),
config.loadPolicy.maxLoadTimeMs -
(stats.loading.first - stats.loading.start)
);
}
}

if (readyState === 4) {
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/controller/error-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ describe('ErrorController Integration Tests', function () {
)
)
);
timers.tick(hls.config.playlistLoadPolicy.default.maxLoadTimeMs);
timers.tick(hls.config.playlistLoadPolicy.default.maxLoadTimeMs);
// tick 3 times to trigger 2 retries and then an error
timers.tick(hls.config.manifestLoadPolicy.default.maxLoadTimeMs + 1);
timers.tick(hls.config.manifestLoadPolicy.default.maxLoadTimeMs + 1);
timers.tick(hls.config.manifestLoadPolicy.default.maxLoadTimeMs);
}).then(
expectFatalErrorEventToStopPlayer(
hls,
Expand Down