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 f065769
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ util.shouldRetryRequest = shouldRetryRequest;
* for authenticating API requests.
*
* @param {object} config - Configuration object.
* @param {object=} config.authClient - AuthClient object. If not provided,
* it will be created and cached here.
* @param {object=} config.credentials - Credentials object.
* @param {string=} config.email - Account email address, required for PEM/P12
* usage.
Expand All @@ -476,6 +478,12 @@ util.shouldRetryRequest = shouldRetryRequest;
* @param {function} callback - The callback function.
*/
function getAuthClient(config, callback) {
if (config.authClient) {
setImmediate(function() {
callback(null, config.authClient);
}
return;
}
var googleAuth = new GoogleAuth();

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

config.authClient = authClient;
callback(null, authClient);
}
}
Expand All @@ -510,6 +519,8 @@ util.getAuthClient = getAuthClient;
* Authenticate a request by extending its headers object with an access token.
*
* @param {object} config - Configuration object.
* @param {object=} config.authClient - AuthClient object. If not provided,
* it will be created and cached here.
* @param {object=} config.credentials - Credentials object.
* @param {string=} config.email - Account email address, required for PEM/P12
* usage.
Expand Down Expand Up @@ -563,6 +574,8 @@ util.authorizeRequest = authorizeRequest;
* response is related to rate limits or certain intermittent server errors.
* We will exponentially backoff subsequent requests by default. (default:
* true)
* @param {object=} config.authClient - AuthClient object. If not provided,
* it will be created and cached here.
* @param {object=} config.credentials - Credentials object.
* @param {boolean=} config.customEndpoint - If true, just return the provided
* request options. Default: false.
Expand Down

0 comments on commit f065769

Please sign in to comment.