Skip to content

Commit 52e8bcf

Browse files
committed
Allow overriding retryOpts.
1 parent b46e673 commit 52e8bcf

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

packages/common-grpc/package.json

Lines changed: 1 addition & 1 deletion
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.2.0",
4949
"through2": "^2.0.3"
5050
},
5151
"devDependencies": {

packages/common-grpc/src/service.js

Lines changed: 7 additions & 4 deletions
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,12 +373,14 @@ 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

380382
request: function() {
383+
setImmediate(() => stream.emit('request'));
381384
return service[protoOpts.method](reqOpts, grpcMetadata, grpcOpts)
382385
.on('metadata', function() {
383386
// retry-request requires a server response before it starts emitting
@@ -391,7 +394,7 @@ GrpcService.prototype.requestStream = function(protoOpts, reqOpts) {
391394
this.emit('response', grcpStatus);
392395
});
393396
}
394-
};
397+
}, protoOpts.retryOpts);
395398

396399
return retryRequest(null, retryOpts)
397400
.on('error', function(err) {

packages/common-grpc/test/service.js

Lines changed: 30 additions & 0 deletions
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,34 @@ describe('GrpcService', function() {
11111113
fakeStream.emit('metadata');
11121114
});
11131115

1116+
it('should emit a `request` event on each request', function(done) {
1117+
var fakeStream = through.obj();
1118+
1119+
ProtoService.prototype.method = function() {
1120+
return fakeStream;
1121+
};
1122+
1123+
retryRequestOverride = function(reqOpts, options) {
1124+
// Simulate three retries.
1125+
setImmediate(function() {
1126+
options.request();
1127+
options.request();
1128+
});
1129+
return options.request();
1130+
};
1131+
1132+
var requestStream = grpcService.requestStream(PROTO_OPTS, REQ_OPTS);
1133+
1134+
var requestEmitted = 0;
1135+
requestStream.on('request', function() {
1136+
requestEmitted++;
1137+
if (requestEmitted === 3) {
1138+
done();
1139+
}
1140+
});
1141+
1142+
});
1143+
11141144
it('should emit the response error', function(done) {
11151145
var grpcError500 = { code: 2 };
11161146
var requestStream = grpcService.requestStream(PROTO_OPTS, REQ_OPTS);

0 commit comments

Comments
 (0)