Skip to content

Commit

Permalink
feat: deferred initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster committed Mar 5, 2020
1 parent b0b280f commit be97b87
Show file tree
Hide file tree
Showing 34 changed files with 1,895 additions and 557 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ export class BigQueryStorageClient {
private _innerApiCalls: {[name: string]: Function};
private _pathTemplates: {[name: string]: gax.PathTemplate};
private _terminated = false;
private _opts: ClientOptions;
private _gaxModule: typeof gax | typeof gax.fallback;
private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
auth: gax.GoogleAuth;
bigQueryStorageStub: Promise<{[name: string]: Function}>;
bigQueryStorageStub?: Promise<{[name: string]: Function}>;

/**
* Construct an instance of BigQueryStorageClient.
Expand Down Expand Up @@ -91,28 +96,31 @@ export class BigQueryStorageClient {
// If we are in browser, we are already using fallback because of the
// "browser" field in package.json.
// But if we were explicitly requested to use fallback, let's do it now.
const gaxModule = !isBrowser && opts.fallback ? gax.fallback : gax;
this._gaxModule = !isBrowser && opts.fallback ? gax.fallback : gax;

// Create a `gaxGrpc` object, with any grpc-specific options
// sent to the client.
opts.scopes = (this.constructor as typeof BigQueryStorageClient).scopes;
const gaxGrpc = new gaxModule.GrpcClient(opts);
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);

// Save options to use in initialize() method.
this._opts = opts;

// Save the auth object to the client, for use by other methods.
this.auth = (gaxGrpc.auth as gax.GoogleAuth);
this.auth = (this._gaxGrpc.auth as gax.GoogleAuth);

// Determine the client header string.
const clientHeader = [
`gax/${gaxModule.version}`,
`gax/${this._gaxModule.version}`,
`gapic/${version}`,
];
if (typeof process !== 'undefined' && 'versions' in process) {
clientHeader.push(`gl-node/${process.versions.node}`);
} else {
clientHeader.push(`gl-web/${gaxModule.version}`);
clientHeader.push(`gl-web/${this._gaxModule.version}`);
}
if (!opts.fallback) {
clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
clientHeader.push(`grpc/${this._gaxGrpc.grpcVersion}`);
}
if (opts.libName && opts.libVersion) {
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
Expand All @@ -122,7 +130,7 @@ export class BigQueryStorageClient {
// For browsers, pass the JSON content.

const nodejsProtoPath = path.join(__dirname, '..', '..', 'protos', 'protos.json');
const protos = gaxGrpc.loadProto(
this._protos = this._gaxGrpc.loadProto(
opts.fallback ?
require("../../protos/protos.json") :
nodejsProtoPath
Expand All @@ -132,38 +140,52 @@ export class BigQueryStorageClient {
// identifiers to uniquely identify resources within the API.
// Create useful helper objects for these.
this._pathTemplates = {
readSessionPathTemplate: new gaxModule.PathTemplate(
readSessionPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/locations/{location}/sessions/{session}'
),
streamPathTemplate: new gaxModule.PathTemplate(
streamPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/locations/{location}/streams/{stream}'
),
};

// Some of the methods on this service provide streaming responses.
// Provide descriptors for these.
this._descriptors.stream = {
readRows: new gaxModule.StreamDescriptor(gax.StreamType.SERVER_STREAMING)
readRows: new this._gaxModule.StreamDescriptor(gax.StreamType.SERVER_STREAMING)
};

// Put together the default options sent with requests.
const defaults = gaxGrpc.constructSettings(
this._defaults = this._gaxGrpc.constructSettings(
'google.cloud.bigquery.storage.v1beta1.BigQueryStorage', gapicConfig as gax.ClientConfig,
opts.clientConfig || {}, {'x-goog-api-client': clientHeader.join(' ')});

// Set up a dictionary of "inner API calls"; the core implementation
// of calling the API is handled in `google-gax`, with this code
// merely providing the destination and request information.
this._innerApiCalls = {};
}

/**
* Initialize the client.
* Performs asynchronous operations (such as authentication) and prepares the client.
* This function will be called automatically when any class method is called for the
* first time, but if you need to initialize it before calling an actual method,
* feel free to call initialize() directly.
*/
initialize() {
// If the client stub promise is already initialized, return immediately.
if (this.bigQueryStorageStub) {
return;
}

// Put together the "service stub" for
// google.cloud.bigquery.storage.v1beta1.BigQueryStorage.
this.bigQueryStorageStub = gaxGrpc.createStub(
opts.fallback ?
(protos as protobuf.Root).lookupService('google.cloud.bigquery.storage.v1beta1.BigQueryStorage') :
this.bigQueryStorageStub = this._gaxGrpc.createStub(
this._opts.fallback ?
(this._protos as protobuf.Root).lookupService('google.cloud.bigquery.storage.v1beta1.BigQueryStorage') :
// tslint:disable-next-line no-any
(protos as any).google.cloud.bigquery.storage.v1beta1.BigQueryStorage,
opts) as Promise<{[method: string]: Function}>;
(this._protos as any).google.cloud.bigquery.storage.v1beta1.BigQueryStorage,
this._opts) as Promise<{[method: string]: Function}>;

// Iterate over each of the methods that the service provides
// and create an API call method for each.
Expand All @@ -182,9 +204,9 @@ export class BigQueryStorageClient {
throw err;
});

const apiCall = gaxModule.createApiCall(
const apiCall = this._gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
this._defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
Expand Down Expand Up @@ -343,6 +365,7 @@ export class BigQueryStorageClient {
'table_reference.project_id': request.tableReference!.projectId || '',
'table_reference.dataset_id': request.tableReference!.datasetId || '',
});
this.initialize();
return this._innerApiCalls.createReadSession(request, options, callback);
}
batchCreateReadSessionStreams(
Expand Down Expand Up @@ -409,6 +432,7 @@ export class BigQueryStorageClient {
] = gax.routingHeader.fromParams({
'session.name': request.session!.name || '',
});
this.initialize();
return this._innerApiCalls.batchCreateReadSessionStreams(request, options, callback);
}
finalizeStream(
Expand Down Expand Up @@ -481,6 +505,7 @@ export class BigQueryStorageClient {
] = gax.routingHeader.fromParams({
'stream.name': request.stream!.name || '',
});
this.initialize();
return this._innerApiCalls.finalizeStream(request, options, callback);
}
splitReadStream(
Expand Down Expand Up @@ -560,6 +585,7 @@ export class BigQueryStorageClient {
] = gax.routingHeader.fromParams({
'original_stream.name': request.originalStream!.name || '',
});
this.initialize();
return this._innerApiCalls.splitReadStream(request, options, callback);
}

Expand Down Expand Up @@ -598,6 +624,7 @@ export class BigQueryStorageClient {
] = gax.routingHeader.fromParams({
'read_position.stream.name': request.readPosition!.stream!.name || '',
});
this.initialize();
return this._innerApiCalls.readRows(request, options);
}

Expand Down Expand Up @@ -709,8 +736,9 @@ export class BigQueryStorageClient {
* The client will no longer be usable and all future behavior is undefined.
*/
close(): Promise<void> {
this.initialize();
if (!this._terminated) {
return this.bigQueryStorageStub.then(stub => {
return this.bigQueryStorageStub!.then(stub => {
this._terminated = true;
stub.close();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,21 @@ describe('v1beta1.BigQueryStorageClient', () => {
});
assert(client);
});
it('has close method', () => {
const client = new bigquerystorageModule.v1beta1.BigQueryStorageClient({
credentials: { client_email: 'bogus', private_key: 'bogus' },
projectId: 'bogus',
});
client.close();
});
describe('createReadSession', () => {
it('invokes createReadSession without error', done => {
const client = new bigquerystorageModule.v1beta1.BigQueryStorageClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.ICreateReadSessionRequest = {};
request.tableReference = {};
Expand All @@ -129,6 +138,8 @@ describe('v1beta1.BigQueryStorageClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.ICreateReadSessionRequest = {};
request.tableReference = {};
Expand Down Expand Up @@ -157,6 +168,8 @@ describe('v1beta1.BigQueryStorageClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.IBatchCreateReadSessionStreamsRequest = {};
request.session = {};
Expand All @@ -181,6 +194,8 @@ describe('v1beta1.BigQueryStorageClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.IBatchCreateReadSessionStreamsRequest = {};
request.session = {};
Expand All @@ -207,6 +222,8 @@ describe('v1beta1.BigQueryStorageClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.IFinalizeStreamRequest = {};
request.stream = {};
Expand All @@ -231,6 +248,8 @@ describe('v1beta1.BigQueryStorageClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.IFinalizeStreamRequest = {};
request.stream = {};
Expand All @@ -257,6 +276,8 @@ describe('v1beta1.BigQueryStorageClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.ISplitReadStreamRequest = {};
request.originalStream = {};
Expand All @@ -281,6 +302,8 @@ describe('v1beta1.BigQueryStorageClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.ISplitReadStreamRequest = {};
request.originalStream = {};
Expand All @@ -307,6 +330,8 @@ describe('v1beta1.BigQueryStorageClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.IReadRowsRequest = {};
request.readPosition = {};
Expand All @@ -331,6 +356,8 @@ describe('v1beta1.BigQueryStorageClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Initialize client before mocking
client.initialize();
// Mock request
const request: protosTypes.google.cloud.bigquery.storage.v1beta1.IReadRowsRequest = {};
request.readPosition = {};
Expand Down
Loading

0 comments on commit be97b87

Please sign in to comment.