Skip to content

Commit

Permalink
cache auth client
Browse files Browse the repository at this point in the history
The authClient from the google-auth-library ought to be cached. Otherwise we end up creating a new AuthClient on each request which ends up fetching fresh credentials on every API request. This can potentially double the latency of each API request.
  • Loading branch information
ofrobots committed Jun 17, 2015
1 parent b1ecc00 commit 9dfccaa
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ util.shouldRetryRequest = shouldRetryRequest;
* @param {function} callback - The callback function.
*/
function getAuthClient(config, callback) {
if (config.authClient) { // cached authClient
callback(null, config.authClient);
return;
}
var googleAuth = new GoogleAuth();

if (config.keyFile) {
Expand All @@ -500,6 +504,7 @@ function getAuthClient(config, callback) {
authClient = authClient.createScoped(config.scopes);
}

config.authClient = authClient;
callback(null, authClient);
}
}
Expand Down

0 comments on commit 9dfccaa

Please sign in to comment.