Skip to content

Commit

Permalink
Perf improvement for caching static credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed May 10, 2015
1 parent d25dea0 commit c066a9e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Credentials/CredentialProvider.php
Expand Up @@ -125,8 +125,23 @@ public static function memoize(callable $provider)
// Create the initial promise that will be used as the cached value
// until it expires.
$result = $provider();
$isConstant = false;

return function () use (&$result, $provider) {
return function () use (&$result, &$isConstant, $provider) {
// Constant credentials will be returned constantly.
if ($isConstant) {
return $result;
}

// Determine if these are constant credentials.
if ($result->getState() === Promise\PromiseInterface::FULFILLED
&& !$result->wait()->getExpiration()
) {
$isConstant = true;
return $result;
}

// Return credentials that could expire and refresh when needed.
return $result
->then(function (CredentialsInterface $creds) use ($provider, &$result) {
// Refresh expired credentials.
Expand Down Expand Up @@ -202,7 +217,8 @@ public static function ini($profile = null, $filename = null)
if (!isset($data[$profile]['aws_access_key_id'])
|| !isset($data[$profile]['aws_secret_access_key'])
) {
return self::reject("No credentials present in INI profile '$profile' ($filename)");
return self::reject("No credentials present in INI profile "
. "'$profile' ($filename)");
}

return Promise\promise_for(
Expand Down

0 comments on commit c066a9e

Please sign in to comment.