Skip to content

Commit

Permalink
Merge pull request aws#695 from jeskew/fix/memory-leak
Browse files Browse the repository at this point in the history
Defer inspection of memoized credential provider
  • Loading branch information
jeskew committed Jul 17, 2015
2 parents 78e2278 + 4a48225 commit 1a69410
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/Credentials/CredentialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,30 @@ public static function chain()
*/
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 ($provider) {
static $result;
static $isConstant;

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;
// Create the initial promise that will be used as the cached value
// until it expires.
if (null === $result) {
$result = $provider();
}

// Return credentials that could expire and refresh when needed.
return $result
->then(function (CredentialsInterface $creds) use ($provider, &$result) {
->then(function (CredentialsInterface $creds) use ($provider, &$isConstant, &$result) {
// Determine if these are constant credentials.
if (!$creds->getExpiration()) {
$isConstant = true;
return $creds;
}

// Refresh expired credentials.
if (!$creds->isExpired()) {
return $creds;
Expand Down

0 comments on commit 1a69410

Please sign in to comment.