Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 6c3c397

Browse files
feat: enable channel pooling (#1065)
* feat: enable channel pooling * fix unit tests * bump grpc-gcp version and tweak the channel pool config * inline json to avoid compliants from pack-n-play. Also only install the channel pool on data connection. And improve error reporting for pack n play * fixes * typo * style Co-authored-by: Mattie Fu <[email protected]>
1 parent 9951e75 commit 6c3c397

4 files changed

Lines changed: 98 additions & 55 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"escape-string-regexp": "^4.0.0",
5555
"extend": "^3.0.2",
5656
"google-gax": "^2.29.5",
57+
"grpc-gcp": "0.4.1",
5758
"is": "^3.0.1",
5859
"is-utf8": "^0.2.1",
5960
"lodash.snakecase": "^4.1.1",

src/index.ts

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {replaceProjectIdToken} from '@google-cloud/projectify';
1616
import {promisifyAll} from '@google-cloud/promisify';
1717
import arrify = require('arrify');
1818
import * as extend from 'extend';
19-
import {GoogleAuth, CallOptions} from 'google-gax';
19+
import {GoogleAuth, CallOptions, grpc as gaxVendoredGrpc} from 'google-gax';
2020
import * as gax from 'google-gax';
2121
import * as protos from '../protos/protos';
2222

@@ -33,6 +33,7 @@ import {google} from '../protos/protos';
3333
import {ServiceError} from 'google-gax';
3434
import * as v2 from './v2';
3535
import {PassThrough, Duplex} from 'stream';
36+
import grpcGcpModule = require('grpc-gcp');
3637

3738
// eslint-disable-next-line @typescript-eslint/no-var-requires
3839
const streamEvents = require('stream-events');
@@ -42,6 +43,9 @@ const PKG = require('../../package.json');
4243

4344
const {grpc} = new gax.GrpcClient();
4445

46+
// Enable channel pooling
47+
const grpcGcp = grpcGcpModule(gaxVendoredGrpc);
48+
4549
export interface GetInstancesCallback {
4650
(
4751
err: ServiceError | null,
@@ -408,17 +412,6 @@ export class Bigtable {
408412
}
409413
}
410414

411-
options = Object.assign(
412-
{
413-
libName: 'gccl',
414-
libVersion: PKG.version,
415-
scopes,
416-
'grpc.keepalive_time_ms': 30000,
417-
'grpc.keepalive_timeout_ms': 10000,
418-
},
419-
options
420-
);
421-
422415
const defaultBaseUrl = 'bigtable.googleapis.com';
423416
const defaultAdminBaseUrl = 'bigtableadmin.googleapis.com';
424417

@@ -428,52 +421,61 @@ export class Bigtable {
428421

429422
let customEndpointBaseUrl;
430423
let customEndpointPort;
424+
let sslCreds;
431425

432426
if (customEndpoint) {
433427
const customEndpointParts = customEndpoint.split(':');
434428
customEndpointBaseUrl = customEndpointParts[0];
435-
customEndpointPort = customEndpointParts[1];
429+
customEndpointPort = Number(customEndpointParts[1]);
430+
sslCreds = grpc.credentials.createInsecure();
436431
}
437432

433+
const baseOptions = Object.assign({
434+
libName: 'gccl',
435+
libVersion: PKG.version,
436+
port: customEndpointPort || 443,
437+
sslCreds,
438+
scopes,
439+
'grpc.keepalive_time_ms': 30000,
440+
'grpc.keepalive_timeout_ms': 10000,
441+
}) as gax.ClientOptions;
442+
443+
const dataOptions = Object.assign(
444+
{},
445+
baseOptions,
446+
{
447+
servicePath: customEndpointBaseUrl || defaultBaseUrl,
448+
'grpc.callInvocationTransformer': grpcGcp.gcpCallInvocationTransformer,
449+
'grpc.channelFactoryOverride': grpcGcp.gcpChannelFactoryOverride,
450+
'grpc.gcpApiConfig': grpcGcp.createGcpApiConfig({
451+
channelPool: {
452+
minSize: 2,
453+
maxSize: 4,
454+
maxConcurrentStreamsLowWatermark: 10,
455+
debugHeaderIntervalSecs: 600,
456+
},
457+
}),
458+
},
459+
options
460+
) as gax.ClientOptions;
461+
462+
const adminOptions = Object.assign(
463+
{},
464+
baseOptions,
465+
{
466+
servicePath: customEndpointBaseUrl || defaultAdminBaseUrl,
467+
},
468+
options
469+
);
470+
438471
this.options = {
439-
BigtableClient: Object.assign(
440-
{
441-
servicePath: customEndpoint ? customEndpointBaseUrl : defaultBaseUrl,
442-
port: customEndpoint ? Number(customEndpointPort) : 443,
443-
sslCreds: customEndpoint
444-
? grpc.credentials.createInsecure()
445-
: undefined,
446-
},
447-
options
448-
) as gax.ClientOptions,
449-
BigtableInstanceAdminClient: Object.assign(
450-
{
451-
servicePath: customEndpoint
452-
? customEndpointBaseUrl
453-
: defaultAdminBaseUrl,
454-
port: customEndpoint ? Number(customEndpointPort) : 443,
455-
sslCreds: customEndpoint
456-
? grpc.credentials.createInsecure()
457-
: undefined,
458-
},
459-
options
460-
) as gax.ClientOptions,
461-
BigtableTableAdminClient: Object.assign(
462-
{
463-
servicePath: customEndpoint
464-
? customEndpointBaseUrl
465-
: defaultAdminBaseUrl,
466-
port: customEndpoint ? Number(customEndpointPort) : 443,
467-
sslCreds: customEndpoint
468-
? grpc.credentials.createInsecure()
469-
: undefined,
470-
},
471-
options
472-
) as gax.ClientOptions,
472+
BigtableClient: dataOptions,
473+
BigtableInstanceAdminClient: adminOptions,
474+
BigtableTableAdminClient: adminOptions,
473475
};
474476

475477
this.api = {};
476-
this.auth = new GoogleAuth(options);
478+
this.auth = new GoogleAuth(Object.assign({}, baseOptions, options));
477479
this.projectId = options.projectId || '{{projectId}}';
478480
this.appProfileId = options.appProfileId;
479481
this.projectName = `projects/${this.projectId}`;

system-test/install.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ describe('📦 pack-n-play test', () => {
4646
).toString(),
4747
},
4848
};
49-
await packNTest(options);
49+
try {
50+
await packNTest(options);
51+
} catch (e) {
52+
// all of the actionable information is on the output attribute
53+
if (e.output) {
54+
e.message += 'output: ' + e.output;
55+
}
56+
throw e;
57+
}
5058
});
5159
});

test/index.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ describe('Bigtable', () => {
190190
assert.deepStrictEqual(
191191
options_,
192192
Object.assign(
193+
{},
194+
options_,
193195
{
194196
libName: 'gccl',
195197
libVersion: PKG.version,
@@ -233,18 +235,24 @@ describe('Bigtable', () => {
233235

234236
assert.deepStrictEqual(bigtable.options, {
235237
BigtableClient: Object.assign(
238+
{},
239+
bigtable.options['BigtableClient'],
236240
{
237241
servicePath: 'bigtable.googleapis.com',
238242
},
239243
expectedOptions
240244
),
241245
BigtableInstanceAdminClient: Object.assign(
246+
{},
247+
bigtable.options['BigtableInstanceAdminClient'],
242248
{
243249
servicePath: 'bigtableadmin.googleapis.com',
244250
},
245251
expectedOptions
246252
),
247253
BigtableTableAdminClient: Object.assign(
254+
{},
255+
bigtable.options['BigtableTableAdminClient'],
248256
{
249257
servicePath: 'bigtableadmin.googleapis.com',
250258
},
@@ -283,9 +291,21 @@ describe('Bigtable', () => {
283291
);
284292

285293
assert.deepStrictEqual(bigtable.options, {
286-
BigtableClient: expectedOptions,
287-
BigtableInstanceAdminClient: expectedOptions,
288-
BigtableTableAdminClient: expectedOptions,
294+
BigtableClient: Object.assign(
295+
{},
296+
bigtable.options['BigtableClient'],
297+
expectedOptions
298+
),
299+
BigtableInstanceAdminClient: Object.assign(
300+
{},
301+
bigtable.options['BigtableInstanceAdminClient'],
302+
expectedOptions
303+
),
304+
BigtableTableAdminClient: Object.assign(
305+
{},
306+
bigtable.options['BigtableTableAdminClient'],
307+
expectedOptions
308+
),
289309
});
290310
});
291311

@@ -315,9 +335,21 @@ describe('Bigtable', () => {
315335
assert.strictEqual(bigtable.customEndpoint, options.apiEndpoint);
316336

317337
assert.deepStrictEqual(bigtable.options, {
318-
BigtableClient: expectedOptions,
319-
BigtableInstanceAdminClient: expectedOptions,
320-
BigtableTableAdminClient: expectedOptions,
338+
BigtableClient: Object.assign(
339+
{},
340+
bigtable.options['BigtableClient'],
341+
expectedOptions
342+
),
343+
BigtableInstanceAdminClient: Object.assign(
344+
{},
345+
bigtable.options['BigtableInstanceAdminClient'],
346+
expectedOptions
347+
),
348+
BigtableTableAdminClient: Object.assign(
349+
{},
350+
bigtable.options['BigtableTableAdminClient'],
351+
expectedOptions
352+
),
321353
});
322354
});
323355

0 commit comments

Comments
 (0)