Skip to content

Commit

Permalink
feat: add close() API (#126)
Browse files Browse the repository at this point in the history
* feat: add close() API

* Return a Promise

* Make generated code compile

* Udpate google-gax

* Apply an obvious fix
  • Loading branch information
schmidt-sebastian authored and alexander-fenster committed Nov 12, 2019
1 parent 263d168 commit 9588ddf
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 36 deletions.
2 changes: 1 addition & 1 deletion templates/typescript_gapic/package.json.njk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ limitations under the License.
"build/protos"
],
"dependencies": {
"google-gax": "^1.9.0"
"google-gax": "^1.10.0"
},
"devDependencies": {
"@types/mocha": "^5.2.5",
Expand Down
42 changes: 35 additions & 7 deletions templates/typescript_gapic/src/$version/$service_client.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ limitations under the License.

import * as gax from 'google-gax';
{% if service.longRunning.length > 0 -%}
import {Callback, LROperation, Descriptors, ClientOptions} from 'google-gax';
import {APICallback, Callback, CallOptions, LROperation, Descriptors, ClientOptions} from 'google-gax';
{%- else -%}
import {Callback, Descriptors, ClientOptions} from 'google-gax';
import {APICallback, Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
{%- endif %}
import * as path from 'path';
{% if (service.paging.length > 0) %}
Expand Down Expand Up @@ -56,10 +56,12 @@ export interface PaginationResponse<
*/
export class {{ service.name }}Client {
private _descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}};
private _{{ service.name.toCamelCase() }}Stub: Promise<{[name: string]: Function}>;
private _innerApiCalls: {[name: string]: Function};
{%- if (service.pathTemplates.length > 0) %}
private _pathTemplates: {[name: string]: gax.PathTemplate};
{%- endif %}
private _terminated = false;
auth: gax.GoogleAuth;

/**
Expand Down Expand Up @@ -235,7 +237,7 @@ export class {{ service.name }}Client {

// Put together the "service stub" for
// {{api.naming.protoPackage}}.{{ service.name }}.
const {{ service.name.toCamelCase() }}Stub = gaxGrpc.createStub(
this._{{ service.name.toCamelCase() }}Stub = gaxGrpc.createStub(
opts.fallback ?
(protos as protobuf.Root).lookupService('{{api.naming.protoPackage}}.{{ service.name }}') :
// tslint:disable-next-line no-any
Expand All @@ -254,21 +256,32 @@ export class {{ service.name }}Client {
];

for (const methodName of {{ service.name.toCamelCase() }}StubMethods) {
const innerCallPromise = {{ service.name.toCamelCase() }}Stub.then(
(stub: {[method: string]: Function}) => (...args: Array<{}>) => {
const innerCallPromise = this._{{ service.name.toCamelCase() }}Stub.then(
stub => (...args: Array<{}>) => {
return stub[methodName].apply(stub, args);
},
(err: Error|null|undefined) => () => {
throw err;
});

this._innerApiCalls[methodName] = gaxModule.createApiCall(
const apiCall = gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
);

this._innerApiCalls[methodName] = (
argument: {},
callOptions?: CallOptions,
callback?: APICallback
) => {
if (this._terminated) {
return Promise.reject('The client has already been closed.');
}
return apiCall(argument, callOptions, callback);
};
}
}

Expand Down Expand Up @@ -589,4 +602,19 @@ export class {{ service.name }}Client {
{%- endfor %}
{%- endfor %}
{%- endif %}

/**
* Terminate the GRPC channel and close the client.
*
* The client will no longer be usable and all future behavior is undefined.
*/
close(): Promise<void> {
if (!this._terminated) {
return this._{{ service.name.toCamelCase() }}Stub.then(stub => {
this._terminated = true;
stub.close();
});
}
return Promise.resolve();
}
}
2 changes: 1 addition & 1 deletion typescript/test/testdata/keymanager/package.json.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build/protos"
],
"dependencies": {
"google-gax": "^1.9.0"
"google-gax": "^1.10.0"
},
"devDependencies": {
"@types/mocha": "^5.2.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// ** All changes to this file may be overwritten. **

import * as gax from 'google-gax';
import {Callback, Descriptors, ClientOptions} from 'google-gax';
import {APICallback, Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
import * as path from 'path';

import { Transform } from 'stream';
Expand Down Expand Up @@ -58,7 +58,9 @@ export interface PaginationResponse<
*/
export class KeyManagementServiceClient {
private _descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}};
private _keyManagementServiceStub: Promise<{[name: string]: Function}>;
private _innerApiCalls: {[name: string]: Function};
private _terminated = false;
auth: gax.GoogleAuth;

/**
Expand Down Expand Up @@ -175,7 +177,7 @@ export class KeyManagementServiceClient {

// Put together the "service stub" for
// google.cloud.kms.v1.KeyManagementService.
const keyManagementServiceStub = gaxGrpc.createStub(
this._keyManagementServiceStub = gaxGrpc.createStub(
opts.fallback ?
(protos as protobuf.Root).lookupService('google.cloud.kms.v1.KeyManagementService') :
// tslint:disable-next-line no-any
Expand All @@ -188,21 +190,32 @@ export class KeyManagementServiceClient {
['listKeyRings', 'listCryptoKeys', 'listCryptoKeyVersions', 'listImportJobs', 'getKeyRing', 'getCryptoKey', 'getCryptoKeyVersion', 'getPublicKey', 'getImportJob', 'createKeyRing', 'createCryptoKey', 'createCryptoKeyVersion', 'importCryptoKeyVersion', 'createImportJob', 'updateCryptoKey', 'updateCryptoKeyVersion', 'encrypt', 'decrypt', 'asymmetricSign', 'asymmetricDecrypt', 'updateCryptoKeyPrimaryVersion', 'destroyCryptoKeyVersion', 'restoreCryptoKeyVersion'];

for (const methodName of keyManagementServiceStubMethods) {
const innerCallPromise = keyManagementServiceStub.then(
(stub: {[method: string]: Function}) => (...args: Array<{}>) => {
const innerCallPromise = this._keyManagementServiceStub.then(
stub => (...args: Array<{}>) => {
return stub[methodName].apply(stub, args);
},
(err: Error|null|undefined) => () => {
throw err;
});

this._innerApiCalls[methodName] = gaxModule.createApiCall(
const apiCall = gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
);

this._innerApiCalls[methodName] = (
argument: {},
callOptions?: CallOptions,
callback?: APICallback
) => {
if (this._terminated) {
return Promise.reject('The client has already been closed.');
}
return apiCall(argument, callOptions, callback);
};
}
}

Expand Down Expand Up @@ -2068,4 +2081,19 @@ export class KeyManagementServiceClient {
callSettings
);
}

/**
* Terminate the GRPC channel and close the client.
*
* The client will no longer be usable and all future behavior is undefined.
*/
close(): Promise<void> {
if (!this._terminated) {
return this._keyManagementServiceStub.then(stub => {
this._terminated = true;
stub.close();
});
}
return Promise.resolve();
}
}
2 changes: 1 addition & 1 deletion typescript/test/testdata/showcase/package.json.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build/protos"
],
"dependencies": {
"google-gax": "^1.9.0"
"google-gax": "^1.10.0"
},
"devDependencies": {
"@types/mocha": "^5.2.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// ** All changes to this file may be overwritten. **

import * as gax from 'google-gax';
import {Callback, LROperation, Descriptors, ClientOptions} from 'google-gax';
import {APICallback, Callback, CallOptions, LROperation, Descriptors, ClientOptions} from 'google-gax';
import * as path from 'path';

import { Transform } from 'stream';
Expand Down Expand Up @@ -51,7 +51,9 @@ export interface PaginationResponse<
*/
export class EchoClient {
private _descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}};
private _echoStub: Promise<{[name: string]: Function}>;
private _innerApiCalls: {[name: string]: Function};
private _terminated = false;
auth: gax.GoogleAuth;

/**
Expand Down Expand Up @@ -193,7 +195,7 @@ export class EchoClient {

// Put together the "service stub" for
// google.showcase.v1beta1.Echo.
const echoStub = gaxGrpc.createStub(
this._echoStub = gaxGrpc.createStub(
opts.fallback ?
(protos as protobuf.Root).lookupService('google.showcase.v1beta1.Echo') :
// tslint:disable-next-line no-any
Expand All @@ -206,21 +208,32 @@ export class EchoClient {
['echo', 'expand', 'collect', 'chat', 'pagedExpand', 'wait'];

for (const methodName of echoStubMethods) {
const innerCallPromise = echoStub.then(
(stub: {[method: string]: Function}) => (...args: Array<{}>) => {
const innerCallPromise = this._echoStub.then(
stub => (...args: Array<{}>) => {
return stub[methodName].apply(stub, args);
},
(err: Error|null|undefined) => () => {
throw err;
});

this._innerApiCalls[methodName] = gaxModule.createApiCall(
const apiCall = gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
);

this._innerApiCalls[methodName] = (
argument: {},
callOptions?: CallOptions,
callback?: APICallback
) => {
if (this._terminated) {
return Promise.reject('The client has already been closed.');
}
return apiCall(argument, callOptions, callback);
};
}
}

Expand Down Expand Up @@ -580,4 +593,19 @@ export class EchoClient {
callSettings
);
}

/**
* Terminate the GRPC channel and close the client.
*
* The client will no longer be usable and all future behavior is undefined.
*/
close(): Promise<void> {
if (!this._terminated) {
return this._echoStub.then(stub => {
this._terminated = true;
stub.close();
});
}
return Promise.resolve();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build/protos"
],
"dependencies": {
"google-gax": "^1.9.0"
"google-gax": "^1.10.0"
},
"devDependencies": {
"@types/mocha": "^5.2.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// ** All changes to this file may be overwritten. **

import * as gax from 'google-gax';
import {Callback, Descriptors, ClientOptions} from 'google-gax';
import {APICallback, Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
import * as path from 'path';

import * as protosTypes from '../../protos/protos';
Expand All @@ -34,7 +34,9 @@ const version = require('../../../package.json').version;
*/
export class TextToSpeechClient {
private _descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}};
private _textToSpeechStub: Promise<{[name: string]: Function}>;
private _innerApiCalls: {[name: string]: Function};
private _terminated = false;
auth: gax.GoogleAuth;

/**
Expand Down Expand Up @@ -137,7 +139,7 @@ export class TextToSpeechClient {

// Put together the "service stub" for
// google.cloud.texttospeech.v1.TextToSpeech.
const textToSpeechStub = gaxGrpc.createStub(
this._textToSpeechStub = gaxGrpc.createStub(
opts.fallback ?
(protos as protobuf.Root).lookupService('google.cloud.texttospeech.v1.TextToSpeech') :
// tslint:disable-next-line no-any
Expand All @@ -150,21 +152,32 @@ export class TextToSpeechClient {
['listVoices', 'synthesizeSpeech'];

for (const methodName of textToSpeechStubMethods) {
const innerCallPromise = textToSpeechStub.then(
(stub: {[method: string]: Function}) => (...args: Array<{}>) => {
const innerCallPromise = this._textToSpeechStub.then(
stub => (...args: Array<{}>) => {
return stub[methodName].apply(stub, args);
},
(err: Error|null|undefined) => () => {
throw err;
});

this._innerApiCalls[methodName] = gaxModule.createApiCall(
const apiCall = gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
);

this._innerApiCalls[methodName] = (
argument: {},
callOptions?: CallOptions,
callback?: APICallback
) => {
if (this._terminated) {
return Promise.reject('The client has already been closed.');
}
return apiCall(argument, callOptions, callback);
};
}
}

Expand Down Expand Up @@ -336,4 +349,19 @@ export class TextToSpeechClient {
return this._innerApiCalls.synthesizeSpeech(request, options, callback);
}


/**
* Terminate the GRPC channel and close the client.
*
* The client will no longer be usable and all future behavior is undefined.
*/
close(): Promise<void> {
if (!this._terminated) {
return this._textToSpeechStub.then(stub => {
this._terminated = true;
stub.close();
});
}
return Promise.resolve();
}
}
2 changes: 1 addition & 1 deletion typescript/test/testdata/translate/package.json.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build/protos"
],
"dependencies": {
"google-gax": "^1.9.0"
"google-gax": "^1.10.0"
},
"devDependencies": {
"@types/mocha": "^5.2.5",
Expand Down
Loading

0 comments on commit 9588ddf

Please sign in to comment.