Skip to content

Replacement memory leak hotfix #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/PluginChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Http\Client\Common;

use function array_reverse;
use Http\Client\Common\Exception\LoopException;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;

final class PluginChain
{
/** @var Plugin[] */
private $plugins;

/** @var callable */
private $clientCallable;

/** @var int */
private $maxRestarts;

/** @var int */
private $restarts = 0;

/**
* @param Plugin[] $plugins A plugin chain
* @param callable $clientCallable Callable making the HTTP call
* @param array $options {
*
* @var int $max_restarts
* }
*/
public function __construct(array $plugins, callable $clientCallable, array $options = [])
{
$this->plugins = $plugins;
$this->clientCallable = $clientCallable;
$this->maxRestarts = (int) ($options['max_restarts'] ?? 0);
}

private function createChain(): callable
{
$lastCallable = $this->clientCallable;
$reversedPlugins = array_reverse($this->plugins);

foreach ($reversedPlugins as $plugin) {
$lastCallable = function (RequestInterface $request) use ($plugin, $lastCallable) {
return $plugin->handleRequest($request, $lastCallable, $this);
};
}

return $lastCallable;
}

public function __invoke(RequestInterface $request): Promise
{
if ($this->restarts > $this->maxRestarts) {
throw new LoopException('Too many restarts in plugin client', $request);
}

++$this->restarts;

return $this->createChain()($request);
}
}
34 changes: 6 additions & 28 deletions src/PluginClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Http\Client\Common;

use Http\Client\Common\Exception\LoopException;
use Http\Client\Exception as HttplugException;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
Expand Down Expand Up @@ -44,11 +43,11 @@ final class PluginClient implements HttpClient, HttpAsyncClient
private $options;

/**
* @param ClientInterface|HttpAsyncClient $client
* @param Plugin[] $plugins
* @param ClientInterface|HttpAsyncClient $client An HTTP async client
* @param Plugin[] $plugins A plugin chain
* @param array $options {
*
* @var int $max_restarts
* @var int $max_restarts
* }
*/
public function __construct($client, array $plugins = [], array $options = [])
Expand Down Expand Up @@ -120,32 +119,11 @@ private function configure(array $options = []): array
/**
* Create the plugin chain.
*
* @param Plugin[] $pluginList A list of plugins
* @param Plugin[] $plugins A plugin chain
* @param callable $clientCallable Callable making the HTTP call
*/
private function createPluginChain(array $pluginList, callable $clientCallable): callable
private function createPluginChain(array $plugins, callable $clientCallable): callable
{
$firstCallable = $lastCallable = $clientCallable;

while ($plugin = array_pop($pluginList)) {
$lastCallable = function (RequestInterface $request) use ($plugin, $lastCallable, &$firstCallable) {
return $plugin->handleRequest($request, $lastCallable, $firstCallable);
};

$firstCallable = $lastCallable;
}

$firstCalls = 0;
$firstCallable = function (RequestInterface $request) use ($lastCallable, &$firstCalls) {
if ($firstCalls > $this->options['max_restarts']) {
throw new LoopException('Too many restarts in plugin client', $request);
}

++$firstCalls;

return $lastCallable($request);
};

return $firstCallable;
return new PluginChain($plugins, $clientCallable, $this->options);
}
}
93 changes: 93 additions & 0 deletions tests/PluginChainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace tests\Http\Client\Common;

use Http\Client\Common\Exception\LoopException;
use Http\Client\Common\Plugin;
use Http\Client\Common\PluginChain;
use Http\Promise\Promise;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;

class PluginChainTest extends TestCase
{
private function createPlugin(callable $func): Plugin
{
return new class ($func) implements Plugin
{
public $func;

public function __construct(callable $func)
{
$this->func = $func;
}

public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
($this->func)($request, $next, $first);

return $next($request);
}
};
}

public function testChainShouldInvokePluginsInReversedOrder(): void
{
$pluginOrderCalls = [];

$plugin1 = $this->createPlugin(static function () use (&$pluginOrderCalls) {
$pluginOrderCalls[] = 'plugin1';
});
$plugin2 = $this->createPlugin(static function () use (&$pluginOrderCalls) {
$pluginOrderCalls[] = 'plugin2';
});

$request = $this->prophesize(RequestInterface::class);
$responsePromise = $this->prophesize(Promise::class);

$clientCallable = static function () use ($responsePromise) {
return $responsePromise->reveal();
};

$pluginOrderCalls = [];

$plugins = [
$plugin1,
$plugin2,
];

$pluginChain = new PluginChain($plugins, $clientCallable);

$result = $pluginChain($request->reveal());

$this->assertSame($responsePromise->reveal(), $result);
$this->assertSame(['plugin1', 'plugin2'], $pluginOrderCalls);
}

public function testShouldThrowLoopExceptionOnMaxRestarts(): void
{
$this->expectException(LoopException::class);

$request = $this->prophesize(RequestInterface::class);
$responsePromise = $this->prophesize(Promise::class);
$calls = 0;
$clientCallable = static function () use ($responsePromise, &$calls) {
++$calls;

return $responsePromise->reveal();
};

$pluginChain = new PluginChain([], $clientCallable, ['max_restarts' => 2]);

$pluginChain($request->reveal());
$this->assertSame(1, $calls);
$pluginChain($request->reveal());
$this->assertSame(2, $calls);
$pluginChain($request->reveal());
$this->assertSame(3, $calls);
$pluginChain($request->reveal());
}
}