Skip to content

Commit

Permalink
fix: infinite retries at error-success loop
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed May 7, 2021
1 parent 1d16715 commit 0310a03
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/uploadx/lib/retry-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ describe('ErrorHandler', () => {
errorHandler.attempts = 3;
expect(errorHandler.kind(500)).toBe(ErrorType.Fatal);
});

it('ErrorHandler.observe(offset)', () => {
const errorHandler = new RetryHandler({ maxAttempts: 2 });
errorHandler.observe();
expect(errorHandler.kind(500)).toBe(ErrorType.Retryable);
errorHandler.observe();
expect(errorHandler.kind(500)).toBe(ErrorType.Retryable);
errorHandler.observe();
expect(errorHandler.kind(500)).toBe(ErrorType.Fatal);
});
});
6 changes: 4 additions & 2 deletions src/uploadx/lib/retry-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const defaultRetryConfig: Required<RetryConfig> = {
export class RetryHandler {
public attempts = 0;
config: Required<RetryConfig>;
private observedValue?: string | number;
cancel: () => void = () => {};

constructor(configOptions: RetryConfig = {}) {
Expand Down Expand Up @@ -77,7 +78,8 @@ export class RetryHandler {
});
}

reset(): void {
this.attempts = 0;
observe(value?: string | number): void {
this.observedValue !== value && (this.attempts = 0);
this.observedValue = value;
}
}
2 changes: 1 addition & 1 deletion src/uploadx/lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export abstract class Uploader implements UploadState {
try {
this.url = this.url || (await this.getFileUrl());
this.offset = isNumber(this.offset) ? await this.sendFileContent() : await this.getOffset();
this.retry.reset();
this.retry.observe(this.offset);
if (this.offset === this.size) {
this.remaining = 0;
this.progress = 100;
Expand Down

0 comments on commit 0310a03

Please sign in to comment.