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

feat: support 'requestTimeout' option #136

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ In multipart mode, `duration` `filesize` `url` field in `MediaDataSource` struct
| `reuseRedirectedURL?` | `boolean` | `false` | Reuse 301/302 redirected url for subsequence request like seek, reconnect, etc. |
| `referrerPolicy?` | `string` | `no-referrer-when-downgrade` | Indicates the [Referrer Policy][] when using FetchStreamLoader |
| `headers?` | `object` | `undefined` | Indicates additional headers that will be added to request |
| `requestTimeout?` | `number` | `Infinity` | Timeout setting for IO Connection, in **milliseconds**. `requestTimeout` must greater than zero. |


[Referrer Policy]: https://w3c.github.io/webappsec-referrer-policy/#referrer-policy
Expand Down
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const defaultConfig = {
// referrerPolicy: leave as unspecified

headers: undefined,
customLoader: undefined
customLoader: undefined,
requestTimeout: Infinity,
};

export function createDefaultConfig() {
Expand Down
31 changes: 29 additions & 2 deletions src/io/fetch-stream-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ class FetchStreamLoader extends BaseLoader {
if (self.AbortController) {
this._abortController = new self.AbortController();
params.signal = this._abortController.signal;

if (this._config.requestTimeout !== Infinity && this._config.requestTimeout > 0) {
this._timeoutId = self.setTimeout(() => {
this.abort('timeout');
}, this._config.requestTimeout);
}
}

this._status = LoaderStatus.kConnecting;
Expand All @@ -130,6 +136,9 @@ class FetchStreamLoader extends BaseLoader {
res.body.cancel();
return;
}

this.clearFetchTimeout();

if (res.ok && (res.status >= 200 && res.status <= 299)) {
if (res.url !== seekConfig.url) {
if (this._onURLRedirect) {
Expand Down Expand Up @@ -159,9 +168,19 @@ class FetchStreamLoader extends BaseLoader {
}
}).catch((e) => {
if (this._abortController && this._abortController.signal.aborted) {
if (this._abortController.signal.reason === 'timeout') {
this._status = LoaderStatus.kError;
if (this._onError) {
this._onError(LoaderErrors.CONNECTING_TIMEOUT, {code: -1, msg: 'fetch stream loader connecting timeout'});
} else {
throw new RuntimeException('FetchStreamLoader: connecting timeout');
}
}
return;
}

this.clearFetchTimeout();

this._status = LoaderStatus.kError;
if (this._onError) {
this._onError(LoaderErrors.EXCEPTION, {code: -1, msg: e.message});
Expand All @@ -171,14 +190,16 @@ class FetchStreamLoader extends BaseLoader {
});
}

abort() {
abort(reason) {
this._requestAbort = true;

this.clearFetchTimeout();

if (this._status !== LoaderStatus.kBuffering || !Browser.chrome) {
// Chrome may throw Exception-like things here, avoid using if is buffering
if (this._abortController) {
try {
this._abortController.abort();
this._abortController.abort(reason);
} catch (e) {}
}
}
Expand Down Expand Up @@ -261,6 +282,12 @@ class FetchStreamLoader extends BaseLoader {
});
}

clearFetchTimeout() {
if (this._timeoutId) {
self.clearTimeout(this._timeoutId);
this._timeoutId = null;
}
}
}

export default FetchStreamLoader;
1 change: 0 additions & 1 deletion src/io/io-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import SpeedSampler from './speed-sampler.js';
import {LoaderStatus, LoaderErrors} from './loader.js';
import FetchStreamLoader from './fetch-stream-loader.js';
import MozChunkedLoader from './xhr-moz-chunked-loader.js';
import MSStreamLoader from './xhr-msstream-loader.js';
import RangeLoader from './xhr-range-loader.js';
import WebSocketLoader from './websocket-loader.js';
import RangeSeekHandler from './range-seek-handler.js';
Expand Down
24 changes: 24 additions & 0 deletions src/io/xhr-moz-chunked-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,27 @@ class MozChunkedLoader extends BaseLoader {
}
}

if (this._config.requestTimeout !== Infinity && this._config.requestTimeout > 0) {
xhr.requestTimeoutId = window.setTimeout(() => {
xhr.abort();

this._status = LoaderStatus.kError;
if (this._onError) {
this._onError(LoaderErrors.CONNECTING_TIMEOUT, {code: -1, msg: 'MozChunkedLoader connecting timeout'});
} else {
throw new RuntimeException('MozChunkedLoader: connecting timeout');
}
}, this._config.requestTimeout);
}

this._status = LoaderStatus.kConnecting;
xhr.send();
}

abort() {
this._requestAbort = true;
if (this._xhr) {
this._clearRequestTimeout();
this._xhr.abort();
}
this._status = LoaderStatus.kComplete;
Expand All @@ -128,6 +142,8 @@ class MozChunkedLoader extends BaseLoader {
let xhr = e.target;

if (xhr.readyState === 2) { // HEADERS_RECEIVED
this._clearRequestTimeout();

if (xhr.responseURL != undefined && xhr.responseURL !== this._requestURL) {
if (this._onURLRedirect) {
let redirectedURL = this._seekHandler.removeURLParameters(xhr.responseURL);
Expand Down Expand Up @@ -187,6 +203,8 @@ class MozChunkedLoader extends BaseLoader {
}

_onXhrError(e) {
this._clearRequestTimeout();

this._status = LoaderStatus.kError;
let type = 0;
let info = null;
Expand All @@ -206,6 +224,12 @@ class MozChunkedLoader extends BaseLoader {
}
}

_clearRequestTimeout() {
if (this._xhr && this._xhr.requestTimeoutId) {
clearTimeout(this._xhr.requestTimeoutId);
this._xhr.requestTimeoutId = undefined;
}
}
}

export default MozChunkedLoader;
24 changes: 24 additions & 0 deletions src/io/xhr-range-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ class RangeLoader extends BaseLoader {
}
}

if (this._config.requestTimeout !== Infinity && this._config.requestTimeout > 0) {
xhr.requestTimeoutId = window.setTimeout(() => {
xhr.abort();

this._status = LoaderStatus.kError;
if (this._onError) {
this._onError(LoaderErrors.CONNECTING_TIMEOUT, {code: -1, msg: 'RangeLoader connecting timeout'});
} else {
throw new RuntimeException('RangeLoader: connecting timeout');
}
}, this._config.requestTimeout);
}

xhr.send();
}

Expand All @@ -181,6 +194,7 @@ class RangeLoader extends BaseLoader {

_internalAbort() {
if (this._xhr) {
this._clearRequestTimeout();
this._xhr.onreadystatechange = null;
this._xhr.onprogress = null;
this._xhr.onload = null;
Expand All @@ -194,6 +208,8 @@ class RangeLoader extends BaseLoader {
let xhr = e.target;

if (xhr.readyState === 2) { // HEADERS_RECEIVED
this._clearRequestTimeout();

if (xhr.responseURL != undefined) { // if the browser support this property
let redirectedURL = this._seekHandler.removeURLParameters(xhr.responseURL);
if (xhr.responseURL !== this._currentRequestURL && redirectedURL !== this._currentRedirectedURL) {
Expand Down Expand Up @@ -341,6 +357,8 @@ class RangeLoader extends BaseLoader {
}

_onXhrError(e) {
this._clearRequestTimeout();

this._status = LoaderStatus.kError;
let type = 0;
let info = null;
Expand All @@ -361,6 +379,12 @@ class RangeLoader extends BaseLoader {
}
}

_clearRequestTimeout() {
if (this._xhr && this._xhr.requestTimeoutId) {
clearTimeout(this._xhr.requestTimeoutId);
this._xhr.requestTimeoutId = undefined;
}
}
}

export default RangeLoader;