Skip to content

Commit

Permalink
Custom shoudRetry in retryConfig (#5658)
Browse files Browse the repository at this point in the history
Co-authored-by: pfomin <pfomin@ivi.ru>
  • Loading branch information
PavelFomin90 and pfomin authored Aug 22, 2023
1 parent 68c6046 commit 2e0bb0e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions api-extractor/report/hls.js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,7 @@ export type RetryConfig = {
retryDelayMs: number;
maxRetryDelayMs: number;
backoff?: 'exponential' | 'linear';
shouldRetry?: (retryConfig: RetryConfig | null | undefined, retryCount: number, isTimeout: boolean, httpStatus: number | undefined, retry: boolean) => boolean;
};

// Warning: (ae-missing-release-tag) "SourceBufferName" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
Expand Down
7 changes: 7 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ See [API Reference](https://hlsjs-dev.video-dev.org/api-docs/) for a complete li
- [`retryDelayMs: number`](#retrydelayms-number)
- [`maxRetryDelayMs: number`](#maxretrydelayms-number)
- [`backoff?: 'exponential' | 'linear'`](#backoff-exponential--linear)
- [`shouldRetry`](#shouldretry)
- [`startFragPrefetch`](#startfragprefetch)
- [`testBandwidth`](#testbandwidth)
- [`progressive`](#progressive)
Expand Down Expand Up @@ -871,6 +872,12 @@ Maximum delay between retries in milliseconds. With each retry, the delay is inc

Used to determine retry backoff duration: Retry delay = 2^retryCount \* retryDelayMs (exponential).

##### `shouldRetry`

(default: internal shouldRetry function, type: `(retryConfig: RetryConfig | null | undefined, retryCount: number, isTimeout: boolean, httpStatus: number | undefined,retry: boolean) => boolean`)

Override default shouldRetry check

### `startFragPrefetch`

(default: `false`)
Expand Down
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ export type RetryConfig = {
retryDelayMs: number; // Retry delay = 2^retryCount * retryDelayMs (exponential) or retryCount * retryDelayMs (linear)
maxRetryDelayMs: number; // Maximum delay between retries
backoff?: 'exponential' | 'linear'; // used to determine retry backoff duration (see retryDelayMs)
shouldRetry?: (
retryConfig: RetryConfig | null | undefined,
retryCount: number,
isTimeout: boolean,
httpStatus: number | undefined,
retry: boolean
) => boolean;
};

export type StreamControllerConfig = {
Expand Down
18 changes: 14 additions & 4 deletions src/utils/error-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,21 @@ export function shouldRetry(
isTimeout: boolean,
httpStatus?: number | undefined,
): retryConfig is RetryConfig & boolean {
return (
!!retryConfig &&
if (!retryConfig) {
return false;
}
const retry =
retryCount < retryConfig.maxNumRetry &&
(retryForHttpStatus(httpStatus) || !!isTimeout)
);
(retryForHttpStatus(httpStatus) || !!isTimeout);
return retryConfig.shouldRetry
? retryConfig.shouldRetry(
retryConfig,
retryCount,
isTimeout,
httpStatus,
retry
)
: retry;
}

export function retryForHttpStatus(httpStatus: number | undefined) {
Expand Down
1 change: 1 addition & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import './unit/loader/playlist-loader';
import './unit/utils/attr-list';
import './unit/utils/binary-search';
import './unit/utils/buffer-helper';
import './unit/utils/error-helper';
import './unit/utils/discontinuities';
import './unit/utils/exp-golomb';
import './unit/utils/output-filter';
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/utils/error-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { shouldRetry } from '../../../src/utils/error-helper';

describe('ErrorHelper', function () {
it('shouldRetry', function () {
const retryConfig = {
maxNumRetry: 3,
};
expect(shouldRetry(retryConfig, 3, false, '502')).to.be.false;
expect(shouldRetry(null, 3, false, '502')).to.be.false;
expect(shouldRetry(retryConfig, 2, false, '502')).to.be.true;
expect(shouldRetry(retryConfig, 2, false, '404')).to.be.false;

retryConfig.shouldRetry = (
_retryConfig,
_retryCount,
_isTimeout,
httpStatus,
retry
) => {
if (!retry && httpStatus === '404') {
return true;
}

return false;
};
expect(shouldRetry(retryConfig, 5, false, '404', false)).to.be.true;

retryConfig.shouldRetry = (retryConfig, retryCount) => {
return retryConfig.maxNumRetry <= retryCount;
};
expect(shouldRetry(retryConfig, 2, false, '502', true)).to.be.false;
});
});

0 comments on commit 2e0bb0e

Please sign in to comment.