Skip to content

Commit 9cdcef5

Browse files
callmehiphopstephenplusplus
authored andcommitted
grpc: listen for metadata event instead of status (#1444)
grpc: listen for metadata event instead of status
1 parent 86e5fd8 commit 9cdcef5

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

lib/common/grpc-service.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,16 @@ GrpcService.prototype.requestStream = function(protoOpts, reqOpts) {
325325

326326
request: function() {
327327
return service[protoOpts.method](reqOpts, grpcOpts)
328-
.on('status', function(status) {
329-
var grcpStatus = GrpcService.decorateStatus_(status);
330-
331-
this.emit('response', grcpStatus || status);
328+
.on('metadata', function() {
329+
// retry-request requires a server response before it starts emitting
330+
// data. The closest mechanism grpc provides is a metadata event, but
331+
// this does not provide any kind of response status. So we're faking
332+
// it here with code `0` which translates to HTTP 200.
333+
//
334+
// https://github.com/GoogleCloudPlatform/gcloud-node/pull/1444#discussion_r71812636
335+
var grcpStatus = GrpcService.decorateStatus_({ code: 0 });
336+
337+
this.emit('response', grcpStatus);
332338
});
333339
}
334340
};

test/common/grpc-service.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,7 @@ describe('GrpcService', function() {
10621062
);
10631063
});
10641064

1065-
it('should emit the status as a response event', function(done) {
1066-
var grpcError500 = { code: 2 };
1065+
it('should emit the metadata event as a response event', function(done) {
10671066
var fakeStream = through.obj();
10681067

10691068
ProtoService.prototype.method = function() {
@@ -1075,13 +1074,14 @@ describe('GrpcService', function() {
10751074
};
10761075

10771076
fakeStream
1077+
.on('error', done)
10781078
.on('response', function(resp) {
1079-
assert.deepEqual(resp, GrpcService.GRPC_ERROR_CODE_TO_HTTP[2]);
1079+
assert.deepEqual(resp, GrpcService.GRPC_ERROR_CODE_TO_HTTP[0]);
10801080
done();
10811081
});
10821082

10831083
grpcService.requestStream(PROTO_OPTS, REQ_OPTS);
1084-
fakeStream.emit('status', grpcError500);
1084+
fakeStream.emit('metadata');
10851085
});
10861086

10871087
it('should emit the response error', function(done) {

0 commit comments

Comments
 (0)