GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: retry multipart uploads on File.save() (#1385) · googleapis/google-cloud-node@2d8701d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handwritten/storage
    • package.json
    • src
      • file.ts
    • test
      • file.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handwritten/storage/package.json‎

Copy file name to clipboardExpand all lines: handwritten/storage/package.json
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@google-cloud/paginator": "^3.0.0",
5757
"@google-cloud/promisify": "^2.0.0",
5858
"arrify": "^2.0.0",
59+
"async-retry": "^1.3.1",
5960
"compressible": "^2.0.12",
6061
"date-and-time": "^0.14.2",
6162
"duplexify": "^4.0.0",
@@ -79,6 +80,7 @@
7980
"@grpc/proto-loader": "^0.5.1",
8081
"@microsoft/api-documenter": "^7.8.10",
8182
"@microsoft/api-extractor": "^7.8.10",
83+
"@types/async-retry": "^1.4.2",
8284
"@types/compressible": "^2.0.0",
8385
"@types/concat-stream": "^1.6.0",
8486
"@types/configstore": "^4.0.0",
Collapse file

‎handwritten/storage/src/file.ts‎

Copy file name to clipboardExpand all lines: handwritten/storage/src/file.ts
+39-9Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
const duplexify: DuplexifyConstructor = require('duplexify');
6565
import {normalize, objectKeyToLowercase, unicodeJSONStringify} from './util';
6666
import {GaxiosError, Headers, request as gaxiosRequest} from 'gaxios';
67+
import retry = require('async-retry');
6768

6869
export type GetExpirationDateResponse = [Date];
6970
export interface GetExpirationDateCallback {
@@ -3517,6 +3518,8 @@ class File extends ServiceObject<File> {
35173518
* Resumable uploads are automatically enabled and must be shut off explicitly
35183519
* by setting `options.resumable` to `false`.
35193520
*
3521+
* Multipart uploads with retryable error codes will be retried 3 times with exponential backoff.
3522+
*
35203523
* <p class="notice">
35213524
* There is some overhead when using a resumable upload that can cause
35223525
* noticeable performance degradation while uploading a series of small
@@ -3560,15 +3563,42 @@ class File extends ServiceObject<File> {
35603563
typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
35613564
const options =
35623565
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
3563-
3564-
const writable = this.createWriteStream(options)
3565-
.on('error', callback!)
3566-
.on('finish', callback!);
3567-
if (options.onUploadProgress) {
3568-
writable.on('progress', options.onUploadProgress);
3566+
const isMultipart = options.resumable === false;
3567+
const returnValue = retry(
3568+
async (bail: (err: Error) => void) => {
3569+
await new Promise<void>((resolve, reject) => {
3570+
const writable = this.createWriteStream(options)
3571+
.on('error', err => {
3572+
if (isMultipart && util.shouldRetryRequest(err)) {
3573+
return reject(err);
3574+
} else {
3575+
return bail(err);
3576+
}
3577+
})
3578+
.on('finish', () => {
3579+
return resolve();
3580+
});
3581+
if (options.onUploadProgress) {
3582+
writable.on('progress', options.onUploadProgress);
3583+
}
3584+
writable.end(data);
3585+
});
3586+
},
3587+
{
3588+
retries: 3,
3589+
}
3590+
);
3591+
if (!callback) {
3592+
return returnValue;
3593+
} else {
3594+
return returnValue
3595+
.then(() => {
3596+
if (callback) {
3597+
return callback();
3598+
}
3599+
})
3600+
.catch(callback);
35693601
}
3570-
3571-
writable.end(data);
35723602
}
35733603
setStorageClass(
35743604
storageClass: string,
@@ -3816,7 +3846,7 @@ class File extends ServiceObject<File> {
38163846
* that a callback is omitted.
38173847
*/
38183848
promisifyAll(File, {
3819-
exclude: ['publicUrl', 'request', 'setEncryptionKey'],
3849+
exclude: ['publicUrl', 'request', 'save', 'setEncryptionKey'],
38203850
});
38213851

38223852
/**
Collapse file

‎handwritten/storage/test/file.ts‎

Copy file name to clipboardExpand all lines: handwritten/storage/test/file.ts
+139Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ import {
5454
MoveOptions,
5555
} from '../src/file';
5656

57+
class HTTPError extends Error {
58+
code: number;
59+
constructor(message: string, code: number) {
60+
super(message);
61+
this.code = code;
62+
}
63+
}
64+
5765
let promisified = false;
5866
let makeWritableStreamOverride: Function | null;
5967
let handleRespOverride: Function | null;
@@ -64,6 +72,9 @@ const fakeUtil = Object.assign({}, util, {
6472
makeWritableStream(...args: Array<{}>) {
6573
(makeWritableStreamOverride || util.makeWritableStream)(...args);
6674
},
75+
shouldRetryRequest(err: HTTPError) {
76+
return err.code === 500;
77+
},
6778
});
6879

6980
const fakePromisify = {
@@ -77,6 +88,7 @@ const fakePromisify = {
7788
assert.deepStrictEqual(options.exclude, [
7889
'publicUrl',
7990
'request',
91+
'save',
8092
'setEncryptionKey',
8193
]);
8294
},
@@ -3853,6 +3865,133 @@ describe('File', () => {
38533865

38543866
describe('save', () => {
38553867
const DATA = 'Data!';
3868+
const BUFFER_DATA = Buffer.from(DATA, 'utf8');
3869+
3870+
class DelayedStreamNoError extends Transform {
3871+
_transform(chunk: string | Buffer, _encoding: string, done: Function) {
3872+
this.push(chunk);
3873+
setTimeout(() => {
3874+
done();
3875+
}, 5);
3876+
}
3877+
}
3878+
3879+
class DelayedStream500Error extends Transform {
3880+
retryCount: number;
3881+
constructor(retryCount: number) {
3882+
super();
3883+
this.retryCount = retryCount;
3884+
}
3885+
_transform(chunk: string | Buffer, _encoding: string, done: Function) {
3886+
this.push(chunk);
3887+
setTimeout(() => {
3888+
if (this.retryCount === 1) {
3889+
done(new HTTPError('first error', 500));
3890+
} else {
3891+
done();
3892+
}
3893+
}, 5);
3894+
}
3895+
}
3896+
3897+
describe('retry mulipart upload', () => {
3898+
it('should save a string with no errors', async () => {
3899+
const options = {resumable: false};
3900+
file.createWriteStream = () => {
3901+
return new DelayedStreamNoError();
3902+
};
3903+
await file.save(DATA, options, assert.ifError);
3904+
});
3905+
3906+
it('string upload should retry on first failure', async () => {
3907+
const options = {resumable: false};
3908+
let retryCount = 0;
3909+
file.createWriteStream = () => {
3910+
retryCount++;
3911+
return new DelayedStream500Error(retryCount);
3912+
};
3913+
await file.save(DATA, options);
3914+
assert.ok(retryCount === 2);
3915+
});
3916+
3917+
it('string upload should not retry if nonretryable error code', async () => {
3918+
const options = {resumable: false};
3919+
let retryCount = 0;
3920+
file.createWriteStream = () => {
3921+
class DelayedStream403Error extends Transform {
3922+
_transform(
3923+
chunk: string | Buffer,
3924+
_encoding: string,
3925+
done: Function
3926+
) {
3927+
this.push(chunk);
3928+
setTimeout(() => {
3929+
retryCount++;
3930+
if (retryCount === 1) {
3931+
done(new HTTPError('first error', 403));
3932+
} else {
3933+
done();
3934+
}
3935+
}, 5);
3936+
}
3937+
}
3938+
return new DelayedStream403Error();
3939+
};
3940+
try {
3941+
await file.save(DATA, options);
3942+
throw Error('unreachable');
3943+
} catch (e) {
3944+
assert.strictEqual(e.message, 'first error');
3945+
}
3946+
});
3947+
3948+
it('should save a buffer with no errors', async () => {
3949+
const options = {resumable: false};
3950+
file.createWriteStream = () => {
3951+
return new DelayedStreamNoError();
3952+
};
3953+
await file.save(DATA, options, assert.ifError);
3954+
});
3955+
3956+
it('buffer upload should retry on first failure', async () => {
3957+
const options = {resumable: false};
3958+
let retryCount = 0;
3959+
file.createWriteStream = () => {
3960+
retryCount++;
3961+
return new DelayedStream500Error(retryCount);
3962+
};
3963+
await file.save(BUFFER_DATA, options);
3964+
assert.ok(retryCount === 2);
3965+
});
3966+
3967+
it('non-multipart upload should not retry', async () => {
3968+
const options = {resumable: true};
3969+
let retryCount = 0;
3970+
file.createWriteStream = () => {
3971+
retryCount++;
3972+
return new DelayedStream500Error(retryCount);
3973+
};
3974+
try {
3975+
await file.save(DATA, options);
3976+
throw Error('unreachable');
3977+
} catch (e) {
3978+
assert.strictEqual(e.message, 'first error');
3979+
}
3980+
});
3981+
});
3982+
3983+
it('should execute callback', async () => {
3984+
const options = {resumable: true};
3985+
let retryCount = 0;
3986+
file.createWriteStream = () => {
3987+
retryCount++;
3988+
return new DelayedStream500Error(retryCount);
3989+
};
3990+
3991+
file.save(DATA, options, (err: HTTPError) => {
3992+
assert.strictEqual(err.code, 500);
3993+
});
3994+
});
38563995

38573996
it('should accept an options object', done => {
38583997
const options = {};

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.