Skip to content

Commit

Permalink
fix: no range/offset retry delay option (#312)
Browse files Browse the repository at this point in the history
* refactor: handle unknown offset

* refactor: emit post-upload state

* refactor:  no range/offset retry delay option

* docs: update
  • Loading branch information
kukhariev authored Jul 14, 2021
1 parent 50c3fe4 commit e9ac7d0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class AppHomeComponent {
- `shouldRetry` Overrides the built-in function that determines whether the operation should be retried
- `minDelay` Minimum (initial) retry interval. Default value: `500`
- `maxDelay` Maximum retry interval. Default value: `50_000`
- `onBusyDelay` Delay used between retries for non-error responses with missing range/offset
- `timeout` Time interval after which unfinished requests must be retried
- `token` Authorization token as a `string` or function returning a `string` or `Promise<string>`
Expand Down
6 changes: 6 additions & 0 deletions src/uploadx/lib/retry-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ export interface RetryConfig {
shouldRetryCodes?: number[];
/** Overrides the built-in function that determines whether the operation should be repeated */
shouldRetry?: ShouldRetryFunction;
/** The minimum retry delay */
minDelay?: number;
/** The maximum retry delay */
maxDelay?: number;
/** Delay used between retries for non-error responses with missing range/offset */
onBusyDelay?: number;
/** Time interval after which hanged requests must be retried */
timeout?: number;
}

Expand All @@ -33,6 +38,7 @@ const defaultRetryConfig: Required<RetryConfig> = {
},
minDelay: 500,
maxDelay: 50000,
onBusyDelay: 1000,
timeout: 0
};

Expand Down
9 changes: 8 additions & 1 deletion src/uploadx/lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ export abstract class Uploader implements UploadState {
this.remaining = 0;
this.progress = 100;
this.status = 'complete';
} else if (!this.offset) {
this.stateChange(this);
await this.retry.wait(this.getRetryAfterFromBackend() || this.retry.config.onBusyDelay);
}
} catch (e) {
e instanceof Error && console.error(e);
Expand All @@ -158,12 +161,16 @@ export abstract class Uploader implements UploadState {
// force getOffset() on http errors and repeat request on network errors
this.responseStatus >= 400 && (this.offset = undefined);
this.status = 'retry';
await this.retry.wait(Number(this.responseHeaders['retry-after']) * 1000);
await this.retry.wait(this.getRetryAfterFromBackend());
}
}
}
}

private getRetryAfterFromBackend(): number {
return Number(this.getValueFromResponse('retry-after')) * 1000;
}

/**
* Performs http requests
*/
Expand Down

0 comments on commit e9ac7d0

Please sign in to comment.