Skip to content

Commit

Permalink
Fix: upload should not retry infinitely on 409 or 429 (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenboyu2 authored Jan 16, 2018
1 parent 6f02373 commit 1cb1268
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/api/BaseUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ class BaseUpload extends Base {

// TODO(tonyjin): Normalize error object and clean up error handling

// Automatically handle name conflict errors
if (error && error.status === 409) {
if (this.retryCount >= MAX_RETRY) {
this.errorCallback(error);
// Automatically handle name conflict errors
} else if (error && error.status === 409) {
if (this.overwrite) {
// Error response contains file ID to upload a new file version for
retryUploadFunc({
Expand All @@ -45,6 +47,7 @@ class BaseUpload extends Base {
fileName: `${fileName.substr(0, fileName.lastIndexOf('.'))}-${Date.now()}${extension}`
});
}
this.retryCount += 1;
// When rate limited, retry after interval defined in header
} else if (error && (error.status === 429 || error.code === 'too_many_requests')) {
let retryAfterMs = DEFAULT_RETRY_DELAY_MS;
Expand All @@ -64,6 +67,7 @@ class BaseUpload extends Base {
}),
retryAfterMs
);
this.retryCount += 1;

// If another error status that isn't name conflict or rate limiting, fail upload
} else if (
Expand All @@ -73,7 +77,7 @@ class BaseUpload extends Base {
) {
this.errorCallback(error);
// Retry with exponential backoff for other failures since these are likely to be network errors
} else if (this.retryCount < MAX_RETRY) {
} else {
this.retryTimeout = setTimeout(
() =>
retryUploadFunc({
Expand All @@ -82,8 +86,6 @@ class BaseUpload extends Base {
2 ** this.retryCount * MS_IN_S
);
this.retryCount += 1;
} else {
this.errorCallback(error);
}
};
}
Expand Down

0 comments on commit 1cb1268

Please sign in to comment.