Skip to content

Commit 3e7df0f

Browse files
kolodnystephenplusplus
authored andcommitted
Allow overriding retryOpts. (#2764)
* Allow overriding `retryOpts`. * Removed extra newline * Use retry-request request events
1 parent b46e673 commit 3e7df0f

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

packages/common-grpc/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"grpc": "^1.7.1",
4646
"is": "^3.2.0",
4747
"modelo": "^4.2.0",
48-
"retry-request": "^3.0.0",
48+
"retry-request": "^3.3.0",
4949
"through2": "^2.0.3"
5050
},
5151
"devDependencies": {

packages/common-grpc/src/service.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ GrpcService.prototype.request = function(protoOpts, reqOpts, callback) {
272272
// executed with this as the "response", we return it to the user as an error.
273273
var respError;
274274

275-
var retryOpts = {
275+
var retryOpts = extend({
276276
retries: this.maxRetries,
277+
currentRetryAttempt: 0,
277278
shouldRetryFn: GrpcService.shouldRetryRequest_,
278279

279280
// retry-request determines if it should retry from the incoming HTTP
@@ -303,7 +304,7 @@ GrpcService.prototype.request = function(protoOpts, reqOpts, callback) {
303304
onResponse(null, resp);
304305
});
305306
}
306-
};
307+
}, protoOpts.retryOpts);
307308

308309
return retryRequest(null, retryOpts, function(err, resp) {
309310
if (!err && resp === respError) {
@@ -372,8 +373,9 @@ GrpcService.prototype.requestStream = function(protoOpts, reqOpts) {
372373
return stream;
373374
}
374375

375-
var retryOpts = {
376+
var retryOpts = extend({
376377
retries: this.maxRetries,
378+
currentRetryAttempt: 0,
377379
objectMode: objectMode,
378380
shouldRetryFn: GrpcService.shouldRetryRequest_,
379381

@@ -391,14 +393,15 @@ GrpcService.prototype.requestStream = function(protoOpts, reqOpts) {
391393
this.emit('response', grcpStatus);
392394
});
393395
}
394-
};
396+
}, protoOpts.retryOpts);
395397

396398
return retryRequest(null, retryOpts)
397399
.on('error', function(err) {
398400
var grpcError = GrpcService.decorateError_(err);
399401

400402
stream.destroy(grpcError || err);
401403
})
404+
.on('request', stream.emit.bind(stream, 'request'))
402405
.pipe(stream);
403406
};
404407

packages/common-grpc/test/service.js

+12
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ describe('GrpcService', function() {
617617
retryRequestOptions.retries,
618618
grpcService.maxRetries
619619
);
620+
assert.strictEqual(retryRequestOptions.currentRetryAttempt, 0);
620621

621622
retryRequestCallback(error, response);
622623
});
@@ -1082,6 +1083,7 @@ describe('GrpcService', function() {
10821083
retryRequestOptions.retries,
10831084
grpcService.maxRetries
10841085
);
1086+
assert.strictEqual(retryRequestOptions.currentRetryAttempt, 0);
10851087
assert.strictEqual(retryRequestOptions.objectMode, true);
10861088
assert.strictEqual(
10871089
retryRequestOptions.shouldRetryFn,
@@ -1111,6 +1113,16 @@ describe('GrpcService', function() {
11111113
fakeStream.emit('metadata');
11121114
});
11131115

1116+
it('should forward `request` events', function(done) {
1117+
var requestStream = grpcService.requestStream(PROTO_OPTS, REQ_OPTS);
1118+
1119+
requestStream.on('request', function() {
1120+
done()
1121+
});
1122+
1123+
retryStream.emit('request');
1124+
});
1125+
11141126
it('should emit the response error', function(done) {
11151127
var grpcError500 = { code: 2 };
11161128
var requestStream = grpcService.requestStream(PROTO_OPTS, REQ_OPTS);

0 commit comments

Comments
 (0)