-
Notifications
You must be signed in to change notification settings - Fork 148
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
Allowing 101 listeners #256
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d30dfd2
Adding TypeScript dev dependencies
schmidt-sebastian b72259e
Allowing tests to be run from /src
schmidt-sebastian d24cbca
Merge
schmidt-sebastian f4cb701
Test initialization refactor
schmidt-sebastian f1dce39
Allowing 101 listeners
schmidt-sebastian b567f16
Review comments
schmidt-sebastian 9ee3979
Fixing lint
schmidt-sebastian b3da6b6
Addressing Mike's comments
schmidt-sebastian 0aeee35
Master merge
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/*! | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import assert from 'assert'; | ||
|
||
/** | ||
* An auto-resizing pool that distributes concurrent operations over multiple | ||
* clients of type `T`. | ||
* | ||
* ClientPool is used within Firestore to manage a pool of GAPIC clients and | ||
* automatically initializes multiple clients if we issue more than 100 | ||
* concurrent operations. | ||
*/ | ||
export class ClientPool<T> { | ||
/** Stores each active clients and how many operations it has outstanding. */ | ||
private activeClients: Map<T, number> = new Map(); | ||
|
||
/** | ||
* @param {number} concurrentOperationLimit - The number of operations that | ||
* each client can handle. | ||
* @param {() => T} clientFactory - A factory function called as needed when | ||
* new clients are required. | ||
*/ | ||
constructor( | ||
private readonly concurrentOperationLimit: number, | ||
private readonly clientFactory: () => T) {} | ||
|
||
/** | ||
* Returns an already existing client if it has less than the maximum number | ||
* of concurrent operations or initializes and returns a new client. | ||
*/ | ||
private acquire(): T { | ||
let selectedClient: T|null = null; | ||
let selectedRequestCount = 0; | ||
|
||
this.activeClients.forEach((requestCount, client) => { | ||
if (!selectedClient && requestCount < this.concurrentOperationLimit) { | ||
selectedClient = client; | ||
selectedRequestCount = requestCount; | ||
} | ||
}); | ||
|
||
if (!selectedClient) { | ||
selectedClient = this.clientFactory(); | ||
assert( | ||
!this.activeClients.has(selectedClient), | ||
'The provided client factory returned an existing instance'); | ||
} | ||
|
||
this.activeClients.set(selectedClient, selectedRequestCount + 1); | ||
|
||
return selectedClient!; | ||
} | ||
|
||
/** | ||
* Reduces the number of operations for the provided client, potentially | ||
* removing it from the pool of active clients. | ||
*/ | ||
private release(client: T): void { | ||
let requestCount = this.activeClients.get(client) || 0; | ||
assert(requestCount > 0, 'No active request'); | ||
|
||
requestCount = requestCount! - 1; | ||
this.activeClients.set(client, requestCount); | ||
|
||
if (requestCount === 0) { | ||
this.garbageCollect(); | ||
} | ||
} | ||
|
||
/** | ||
* The number of currently registered clients. | ||
* | ||
* @return {number} Number of currently registered clients. | ||
*/ | ||
// Visible for testing. | ||
get size(): number { | ||
return this.activeClients.size; | ||
} | ||
|
||
/** | ||
* Runs the provided operation in this pool. This function may create an | ||
* additional client if all existing clients already operate at the concurrent | ||
* operation limit. | ||
* | ||
* @param {(client: T) => Promise<V>} op - A callback function that returns a | ||
* Promise. The client T will be returned to the pool when callback finishes. | ||
* @return {Promise<V>} A Promise that resolves with the result of `op`. | ||
*/ | ||
run<V>(op: (client: T) => Promise<V>): Promise<V> { | ||
const client = this.acquire(); | ||
|
||
return op(client) | ||
.catch(err => { | ||
this.release(client); | ||
return Promise.reject(err); | ||
}) | ||
.then(res => { | ||
this.release(client); | ||
return res; | ||
}); | ||
} | ||
|
||
/** | ||
* Deletes clients that are no longer executing operations. Keeps up to one | ||
* idle client to reduce future initialization costs. | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
*/ | ||
private garbageCollect(): void { | ||
let idleClients = 0; | ||
this.activeClients.forEach((requestCount, client) => { | ||
if (requestCount === 0) { | ||
++idleClients; | ||
|
||
if (idleClients > 1) { | ||
this.activeClients.delete(client); | ||
} | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.