Skip to content

Commit

Permalink
Call GrpcClient.close() when we dispose of clients
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Oct 8, 2019
1 parent c3654c1 commit 0d2f97e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
5 changes: 3 additions & 2 deletions dev/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export class Firestore {

this._clientPool = new ClientPool(
MAX_CONCURRENT_REQUESTS_PER_CLIENT,
() => {
/* clientFactory= */ () => {
let client: GapicClient;

if (this._settings.ssl === false) {
Expand All @@ -401,7 +401,8 @@ export class Firestore {

logger('Firestore', null, 'Initialized Firestore GAPIC Client');
return client;
}
},
/* clientDestructor= */ (client: GapicClient) => client.close()
);

logger('Firestore', null, 'Initialized Firestore');
Expand Down
8 changes: 6 additions & 2 deletions dev/src/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ export class ClientPool<T> {
* can handle.
* @param clientFactory A factory function called as needed when new clients
* are required.
* @param clientDestructor A cleanup function that is called for each client
* when the client is disposed of.
*/
constructor(
private readonly concurrentOperationLimit: number,
private readonly clientFactory: () => T
) {}
private readonly clientFactory: () => T,
private readonly clientDestructor: (client:T) => void = () => {})
{}

/**
* Returns an already existing client if it has less than the maximum number
Expand Down Expand Up @@ -171,6 +174,7 @@ export class ClientPool<T> {
++idleClients;

if (idleClients > 1) {
this.clientDestructor(client);
this.activeClients.delete(client);
}
}
Expand Down
13 changes: 11 additions & 2 deletions dev/src/v1/firestore_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class FirestoreClient {

// Put together the "service stub" for
// google.firestore.v1.Firestore.
const firestoreStub = gaxGrpc.createStub(
this._firestoreStub = gaxGrpc.createStub(
opts.fallback
? protos.lookupService('google.firestore.v1.Firestore')
: protos.google.firestore.v1.Firestore,
Expand All @@ -221,7 +221,7 @@ class FirestoreClient {
'listCollectionIds',
];
for (const methodName of firestoreStubMethods) {
const innerCallPromise = firestoreStub.then(
const innerCallPromise = this._firestoreStub.then(
stub => (...args) => {
return stub[methodName].apply(stub, args);
},
Expand Down Expand Up @@ -271,6 +271,15 @@ class FirestoreClient {
];
}

/**
* Terminate the GRPC channel and close the client.
*
* The client will no longer be usable and all future behavior is undefined.
*/
close() {
this._firestoreStub.then(grpcClient => grpcClient.close());
}

/**
* Return the project ID used by this class.
* @param {function(Error, string)} callback - the callback to
Expand Down
17 changes: 17 additions & 0 deletions dev/test/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ describe('Client pool', () => {
);
});

it('garbage collection calls destructor', () => {
const garbageCollect = new Deferred();

const clientPool = new ClientPool<{}>(1, () => {
return {};
}, () => garbageCollect.resolve());

const operationPromises = deferredPromises(2);

clientPool.run(REQUEST_TAG, () => operationPromises[0].promise);
clientPool.run(REQUEST_TAG, () => operationPromises[1].promise);

operationPromises.forEach(deferred => deferred.resolve());

return garbageCollect.promise;
});

it('forwards success', () => {
const clientPool = new ClientPool<{}>(1, () => {
return {};
Expand Down

0 comments on commit 0d2f97e

Please sign in to comment.