Skip to content
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

Switch FastCGI library (hollodotme/fast-cgi-client) #133

Merged
merged 5 commits into from Dec 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
# Unreleased

- [#133](https://github.com/gordalina/cachetool/pull/133) Switch FastCGI library (hollodotme/fast-cgi-client)
- [#132](https://github.com/gordalina/cachetool/pull/132) CI in PHP 7.4
- [#129](https://github.com/gordalina/cachetool/pull/129) Implement --batch and --exclude options for opcache:compile:scripts command
- [#131](https://github.com/gordalina/cachetool/pull/131) Add SERVER_ADDR, REMOTE_ADDR, REMOTE_PORT in FCGI call
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -3,7 +3,7 @@
"description": "Manage your OPcache & APCu cache through the CLI",
"keywords": ["cache", "opcache", "apcu", "fastcgi", "opcode", "fpm"],
"require": {
"adoy/fastcgi-client": "~1.0",
"hollodotme/fast-cgi-client": "~3.0",
"monolog/monolog": "~1.0 || ~2.0",
"php": ">=7.1.0",
"psr/log": "~1.0",
Expand Down
52 changes: 27 additions & 25 deletions src/Adapter/FastCGI.php
Expand Up @@ -12,7 +12,11 @@
namespace CacheTool\Adapter;

use CacheTool\Code;
use Adoy\FastCGI\Client;
use hollodotme\FastCGI\Client;
use hollodotme\FastCGI\Interfaces\ConfiguresSocketConnection;
use hollodotme\FastCGI\SocketConnections\NetworkSocket;
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;
use hollodotme\FastCGI\Requests\PostRequest;

class FastCGI extends AbstractAdapter
{
Expand All @@ -21,6 +25,11 @@ class FastCGI extends AbstractAdapter
*/
protected $client;

/**
* @var ConfiguresSocketConnection
*/
protected $connection;

/**
* @var Array of patterns matching php socket files
*/
Expand Down Expand Up @@ -63,15 +72,21 @@ public function __construct($host = null, $chroot = null)

if (false !== strpos($host, ':')) {
[$host, $port] = explode(':', $host);
$this->client = new Client($host, $port);
$this->connection = new NetworkSocket(
$host, # Hostname
$port, # Port
5000, # Connect timeout in milliseconds (default: 5000)
120000 # Read/write timeout in milliseconds (default: 5000)
);
} else {
// socket
$this->client = new Client('unix://' . $host, -1);
$this->connection = new UnixDomainSocket(
$host, # Socket path to php-fpm
5000, # Connect timeout in milliseconds (default: 5000)
120000 # Read/write timeout in milliseconds (default: 5000)
);
}

$this->client->setReadWriteTimeout(60 * 1000);
$this->client->setPersistentSocket(false);
$this->client->setKeepAlive(true);
$this->client = new Client();

if ($chroot !== null) {
$this->chroot = rtrim($chroot, '/');
Expand All @@ -83,15 +98,10 @@ public function __construct($host = null, $chroot = null)
*/
protected function doRun(Code $code)
{
$response = $this->request($code);
$parts = explode("\r\n\r\n", $response);

// remove headers
array_shift($parts);
$body = implode("\r\n\r\n", $parts);
$body = $this->request($code);

if (@unserialize($body) === false) {
throw new \RuntimeException(sprintf("Error: %s", $response));
throw new \RuntimeException(sprintf("Error: %s", $body));
}

return $body;
Expand All @@ -105,24 +115,16 @@ protected function request(Code $code)
try {
$code->writeTo($file);

$environment = [
'SERVER_ADDR' => '127.0.0.1',
'REMOTE_ADDR' => '127.0.0.1',
'REMOTE_PORT' => '65000',
'REQUEST_METHOD' => 'POST',
'REQUEST_URI' => '/',
'SCRIPT_FILENAME' => $this->getScriptFileName($file),
];

$this->logger->info(sprintf('FastCGI: Requesting FPM using socket: %s', $this->host));
$response = $this->client->request($environment, '');
$request = new PostRequest($this->getScriptFileName($file), '');
$response = $this->client->sendRequest($this->connection, $request);
$this->logger->debug(sprintf('FastCGI: Response: %s', json_encode($response)));

if (!@unlink($file)) {
$this->logger->debug(sprintf('FastCGI: Could not delete file: %s', $file));
}

return $response;
return $response->getBody();
} catch (\Exception $e) {
if (!@unlink($file)) {
$this->logger->debug(sprintf('FastCGI: Could not delete file: %s', $file));
Expand Down
9 changes: 1 addition & 8 deletions tests/CacheToolTest.php
Expand Up @@ -49,14 +49,7 @@ public function testFactoryWithTempDirNotWritable()
$cachetool = CacheTool::factory($adapter, $tempDir, $this->getLogger());
$this->assertSame($tempDir, $cachetool->getTempDir());
}

public function testLoggerWithAdapter()
{
$cachetool = new CacheTool(null, $this->getLogger());
$cachetool->setAdapter(new Adapter\Cli);
$cachetool->setLogger($this->getLogger());
}


/**
* @expectedException \InvalidArgumentException
*/
Expand Down