Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add grpc-gcp support in gax #396

Merged
merged 7 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"google-auth-library": "^3.0.0",
"google-proto-files": "^0.18.0",
"grpc": "^1.16.0",
"grpc-gcp": "^0.1.1",
"is-stream-ended": "^0.1.4",
"lodash.at": "^4.6.0",
"lodash.has": "^4.5.2",
Expand Down
15 changes: 10 additions & 5 deletions src/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import * as fs from 'fs';
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
import {getProtoPath} from 'google-proto-files';
import * as grpcTypes from 'grpc'; // for types only
import * as grpcGcp from 'grpc-gcp';
import {OutgoingHttpHeaders} from 'http';
import * as path from 'path';
import * as protobuf from 'protobufjs';
Expand Down Expand Up @@ -286,13 +287,17 @@ export class GrpcClient {
Object.keys(options).forEach(key => {
if (key.indexOf('grpc.') === 0) {
grpcOptions[key] = options[key];
} else if (key.indexOf('grpc_gcp.') === 0) {
// This prefix is used to pass additional arguments that aren't
// options for grpc. Strip the prefix before passing.
const prefixLength = 'grpc_gcp.'.length;
grpcOptions[key.substr(prefixLength)] = options[key];
}
});
const apiConfigDefinition = options['grpc_gcp.apiConfig'];
if (apiConfigDefinition) {
const apiConfig = grpcGcp.createGcpApiConfig(apiConfigDefinition);
grpcOptions['channelFactoryOverride'] =
grpcGcp.gcpChannelFactoryOverride;
grpcOptions['callInvocationTransformer'] =
grpcGcp.gcpCallInvocationTransformer;
grpcOptions['gcpApiConfig'] = apiConfig;
}
return new CreateStub(serviceAddress, credentials, grpcOptions);
});
}
Expand Down
32 changes: 29 additions & 3 deletions test/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describe('grpc', () => {
});

describe('createStub', () => {
const TEST_PATH = path.resolve(__dirname, '../../test');
class DummyStub {
constructor(public address, public creds, public options) {}
}
Expand Down Expand Up @@ -177,7 +178,6 @@ describe('grpc', () => {
port: 443,
'grpc.max_send_message_length': 10 * 1024 * 1024,
'grpc.initial_reconnect_backoff_ms': 10000,
'grpc_gcp.non_grpc_option': 1,
other_dummy_options: 'test',
};
return grpcClient.createStub(DummyStub, opts).then(stub => {
Expand All @@ -186,8 +186,7 @@ describe('grpc', () => {
expect(stub.creds).to.deep.eq(dummyChannelCreds);
// tslint:disable-next-line no-any
(expect(stub.options).has as any).key([
'grpc.max_send_message_length', 'grpc.initial_reconnect_backoff_ms',
'non_grpc_option'
'grpc.max_send_message_length', 'grpc.initial_reconnect_backoff_ms'
]);
// tslint:disable-next-line no-any
(expect(stub.options).to.not.have as any).key([
Expand All @@ -196,6 +195,33 @@ describe('grpc', () => {
});
});

it('supports grpc-gcp options', () => {
const apiConfigDefinition =
require(path.resolve(TEST_PATH, 'test_grpc_config.json'));
const opts = {
servicePath: 'foo.example.com',
port: 443,
'grpc_gcp.apiConfig': apiConfigDefinition,
};
return grpcClient.createStub(DummyStub, opts).then(stub => {
expect(stub).to.be.an.instanceOf(DummyStub);
expect(stub.address).to.eq('foo.example.com:443');
expect(stub.creds).to.deep.eq(dummyChannelCreds);
// tslint:disable-next-line no-any
(expect(stub.options).has as any).key([
'channelFactoryOverride', 'callInvocationTransformer', 'gcpApiConfig'
]);
// tslint:disable-next-line no-any
(expect(stub.options).to.not.have as any).key([
'servicePath', 'port', 'grpc_gcp.apiConfig'
]);
// tslint:disable-next-line no-any
(expect(stub.options['gcpApiConfig']).has as any).key([
'channelPool', 'method'
]);
});
});

it('uses the passed grpc channel', () => {
const customCreds = {channelCreds: 'custom'};
const opts = {
Expand Down
15 changes: 15 additions & 0 deletions test/test_grpc_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"channelPool": {
"maxSize": 1,
"maxConcurrentStreamsLowWatermark": 1
},
"method": [
{
"name": ["method1"],
"affinity": {
"command": "BIND",
"affinityKey": "key1"
}
}
]
}