Skip to content

Commit

Permalink
Do not use defer (#111)
Browse files Browse the repository at this point in the history
* Do not use defer

* Reverted tests
  • Loading branch information
Nyholm authored and dbu committed Oct 9, 2018
1 parent f77e88b commit 54a4fc9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 34 deletions.
17 changes: 5 additions & 12 deletions spec/Plugin/RetryPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo
}
};

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
$promise->wait()->shouldReturn($response);
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
}

function it_throws_exception_on_multiple_exceptions(RequestInterface $request)
Expand All @@ -55,7 +53,7 @@ function it_throws_exception_on_multiple_exceptions(RequestInterface $request)
};

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
$promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
$promise->shouldThrow($exception2)->duringWait();
}

Expand All @@ -78,7 +76,7 @@ function it_returns_response_on_second_try(RequestInterface $request, ResponseIn
};

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
$promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
$promise->wait()->shouldReturn($response);
}

Expand All @@ -100,13 +98,8 @@ function it_does_not_keep_history_of_old_failure(RequestInterface $request, Resp
}
};

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
$promise->wait()->shouldReturn($response);

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
$promise->wait()->shouldReturn($response);
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
}

function it_has_an_exponential_default_delay(RequestInterface $request, Exception\HttpException $exception)
Expand Down
27 changes: 5 additions & 22 deletions src/Plugin/RetryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Http\Client\Common\Plugin;

use Http\Client\Common\Deferred;
use Http\Client\Common\Plugin;
use Http\Client\Exception;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -77,31 +76,20 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
{
$chainIdentifier = spl_object_hash((object) $first);

$promise = $next($request);
$deferred = new Deferred(function () use ($promise) {
$promise->wait(false);
});

$onFulfilled = function (ResponseInterface $response) use ($chainIdentifier, $deferred) {
return $next($request)->then(function (ResponseInterface $response) use ($request, $chainIdentifier) {
if (array_key_exists($chainIdentifier, $this->retryStorage)) {
unset($this->retryStorage[$chainIdentifier]);
}

$deferred->resolve($response);

return $response;
};

$onRejected = function (Exception $exception) use ($request, $next, $onFulfilled, &$onRejected, $chainIdentifier, $deferred) {
}, function (Exception $exception) use ($request, $next, $first, $chainIdentifier) {
if (!array_key_exists($chainIdentifier, $this->retryStorage)) {
$this->retryStorage[$chainIdentifier] = 0;
}

if ($this->retryStorage[$chainIdentifier] >= $this->retry) {
unset($this->retryStorage[$chainIdentifier]);

$deferred->reject($exception);

throw $exception;
}

Expand All @@ -114,15 +102,10 @@ public function handleRequest(RequestInterface $request, callable $next, callabl

// Retry in synchrone
++$this->retryStorage[$chainIdentifier];
$promise = $this->handleRequest($request, $next, $first);

$next($request)->then($onFulfilled, $onRejected);

throw $exception;
};

$promise->then($onFulfilled, $onRejected);

return $deferred;
return $promise->wait();
});
}

/**
Expand Down

0 comments on commit 54a4fc9

Please sign in to comment.