Skip to content

Commit eb06f34

Browse files
author
Holger Woltersdorf
authored
Merge pull request #33 from hollodotme/research/use-idle-sockets
Refactor sockets to be re-used if possible * Adding socket status: INIT, IDLE & BUSY to check for idle & reusable sockets * Reset response property of Socket class when a new request is performed * Do not disconnect & remove socket from list after response was received * Remove sockets only from list, if it's not usable anymore (e.g. timed out or closed) * Refactor socket list to an internal collection in order to cleanup operations on the socket list * Tests for the newly introduced socket methods * Tests for the newly introduced socket collection * Tests for the re-using of idle sockets
2 parents 9596249 + fe4b9c1 commit eb06f34

25 files changed

+1475
-432
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ shared-unit-tests: &shared-unit-tests
119119

120120
- run:
121121
name: Run unit tests
122-
command: vendor/bin/phpunit$PHPUNIT_VERSION.phar -c build --testsuite Unit --log-junit build/logs/junit/junit.xml --coverage-html build/logs/coverage --coverage-clover=coverage.xml
122+
command: php -d auto_prepend_file=build/xdebug-filter.php vendor/bin/phpunit$PHPUNIT_VERSION.phar -c build --testsuite Unit --log-junit build/logs/junit/junit.xml --coverage-html build/logs/coverage --coverage-clover=coverage.xml
123123

124124
- run:
125125
name: Upload code coverage to codecov.io
@@ -152,7 +152,7 @@ shared-integration-tests: &shared-integration-tests
152152

153153
- run:
154154
name: Run integration tests
155-
command: vendor/bin/phpunit$PHPUNIT_VERSION.phar -c build --testsuite Integration --log-junit build/logs/junit/junit.xml
155+
command: php -d auto_prepend_file=build/xdebug-filter.php vendor/bin/phpunit$PHPUNIT_VERSION.phar -c build --testsuite Integration --log-junit build/logs/junit/junit.xml
156156

157157
- store_test_results:
158158
path: build/logs/junit

.docker/php/7.1/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM php:7.1-fpm-alpine
22
ENV PHP_CONF_DIR=/usr/local/etc
33
RUN apk update && apk upgrade && apk add --no-cache $PHPIZE_DEPS \
4-
&& pecl install xdebug-2.7.0 \
4+
&& pecl install xdebug-2.7.1 \
55
&& docker-php-ext-enable xdebug \
66
&& apk del $PHPIZE_DEPS
77
COPY network-socket.pool.conf $PHP_CONF_DIR/php-fpm.d/network-socket.pool.conf

.docker/php/7.2/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM php:7.2-fpm-alpine
22
ENV PHP_CONF_DIR=/usr/local/etc
33
RUN apk update && apk upgrade && apk add --no-cache $PHPIZE_DEPS \
4-
&& pecl install xdebug-2.7.0 \
4+
&& pecl install xdebug-2.7.1 \
55
&& docker-php-ext-enable xdebug \
66
&& apk del $PHPIZE_DEPS
77
COPY network-socket.pool.conf $PHP_CONF_DIR/php-fpm.d/network-socket.pool.conf

.docker/php/7.3/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM php:7.3-fpm-alpine
22
ENV PHP_CONF_DIR=/usr/local/etc
33
RUN apk update && apk upgrade && apk add --no-cache $PHPIZE_DEPS \
4-
&& pecl install xdebug-2.7.0 \
4+
&& pecl install xdebug-2.7.1 \
55
&& docker-php-ext-enable xdebug \
66
&& apk del $PHPIZE_DEPS
77
COPY network-socket.pool.conf $PHP_CONF_DIR/php-fpm.d/network-socket.pool.conf

.docker/php/network-socket.pool.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[network]
33
user = www-data
44
group = www-data
5-
listen = 127.0.0.1:9001
5+
listen = 0.0.0.0:9001
66
listen.owner = www-data
77
listen.group = www-data
88
pm = dynamic

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a CHANGELOG](http://keepachangelog.com).
55

6+
## [2.7.0] - YYYY-MM-DD
7+
8+
### Added
9+
10+
* Re-using of idle sockets - [#33]
11+
612
## [2.6.0] - 2019-04-02
713

814
### Added
@@ -180,6 +186,7 @@ Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](http
180186
* Getters/Setters for connect timeout, read/write timeout, keep alive, socket persistence from `Client` (now part of the socket connection)
181187
* Method `Client->getValues()`
182188

189+
[2.7.0]: https://github.com/hollodotme/fast-cgi-client/compare/v2.6.0...v2.7.0
183190
[2.6.0]: https://github.com/hollodotme/fast-cgi-client/compare/v2.5.0...v2.6.0
184191
[2.5.0]: https://github.com/hollodotme/fast-cgi-client/compare/v2.4.3...v2.5.0
185192
[2.4.3]: https://github.com/hollodotme/fast-cgi-client/compare/v2.4.2...v2.4.3
@@ -204,3 +211,4 @@ Based on [Pierrick Charron](https://github.com/adoy)'s [PHP-FastCGI-Client](http
204211
[#20]: https://github.com/hollodotme/fast-cgi-client/issues/20
205212
[#26]: https://github.com/hollodotme/fast-cgi-client/issues/26
206213
[#27]: https://github.com/hollodotme/fast-cgi-client/issues/27
214+
[#33]: https://github.com/hollodotme/fast-cgi-client/pull/33

bin/persistant.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hollodotme\FastCGI\Bin;
4+
5+
use hollodotme\FastCGI\Client;
6+
use hollodotme\FastCGI\Requests\PostRequest;
7+
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;
8+
use function error_reporting;
9+
use function ini_set;
10+
11+
error_reporting( E_ALL );
12+
ini_set( 'display_errors', 'On' );
13+
14+
require __DIR__ . '/../vendor/autoload.php';
15+
16+
$connection = new UnixDomainSocket( '/var/run/php-uds.sock' );
17+
$client = new Client( $connection );
18+
19+
$workerPath = __DIR__ . '/exampleWorker.php';
20+
21+
$request = new PostRequest( $workerPath, 'test=persistant' );
22+
23+
for ( $i = 1; $i < 10; $i++ )
24+
{
25+
echo "{$i}. Request\n";
26+
$response = $client->sendRequest( $request );
27+
printf( "Socket-ID: %s\n%s\n\n", $response->getRequestId(), $response->getOutput() );
28+
}

build/phpunit.xml

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
<phpunit
2-
bootstrap="../tests/bootstrap.php"
3-
verbose="true"
4-
beStrictAboutOutputDuringTests="true"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true">
9-
<testsuites>
2+
bootstrap="../tests/bootstrap.php"
3+
verbose="true"
4+
beStrictAboutOutputDuringTests="true"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true">
9+
<php>
10+
<env name="network-socket-host" value="127.0.0.1"/>
11+
<env name="network-socket-port" value="9001"/>
12+
<env name="unix-domain-socket" value="/var/run/php-uds.sock"/>
13+
<env name="restricted-unix-domain-socket" value="/var/run/php-ruds.sock"/>
14+
<env name="non-existing-unix-domain-socket" value="/tmp/not/existing.sock"/>
15+
<env name="invalid-unix-domain-socket" value="/Fixtures/test.sock"/>
16+
</php>
17+
<testsuites>
1018
<testsuite name="Unit">
11-
<directory suffix="Test.php">../tests/Unit</directory>
12-
</testsuite>
19+
<directory suffix="Test.php">../tests/Unit</directory>
20+
</testsuite>
1321
<testsuite name="Integration">
14-
<directory suffix="Test.php">../tests/Integration</directory>
15-
</testsuite>
16-
</testsuites>
17-
<filter>
18-
<whitelist processUncoveredFilesFromWhitelist="true">
19-
<directory suffix=".php">../src</directory>
20-
</whitelist>
21-
</filter>
22+
<directory suffix="Test.php">../tests/Integration</directory>
23+
</testsuite>
24+
</testsuites>
25+
<filter>
26+
<whitelist processUncoveredFilesFromWhitelist="true">
27+
<directory suffix=".php">../src</directory>
28+
</whitelist>
29+
</filter>
2230
</phpunit>

build/xdebug-filter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
if ( !function_exists( 'xdebug_set_filter' ) )
4+
{
5+
return;
6+
}
7+
8+
/** @noinspection PhpUndefinedFunctionInspection */
9+
/** @noinspection PhpUndefinedConstantInspection */
10+
xdebug_set_filter(
11+
XDEBUG_FILTER_CODE_COVERAGE,
12+
XDEBUG_PATH_WHITELIST,
13+
[dirname( __DIR__ ) . '/src']
14+
);

composer.json

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
11
{
2-
"name": "hollodotme/fast-cgi-client",
3-
"description": "A PHP fast CGI client to send requests (a)synchronously to PHP-FPM.",
4-
"keywords": [
5-
"fastcgi",
6-
"php-fpm",
7-
"socket"
8-
],
9-
"minimum-stability": "dev",
10-
"prefer-stable": true,
11-
"license": "MIT",
12-
"authors": [
13-
{
14-
"name": "Holger Woltersdorf",
15-
"email": "[email protected]"
16-
}
17-
],
18-
"bin": [
19-
"bin/fcgiget"
20-
],
21-
"autoload": {
22-
"psr-4": {
23-
"hollodotme\\FastCGI\\": "src/"
24-
}
25-
},
26-
"autoload-dev": {
27-
"psr-4": {
28-
"hollodotme\\FastCGI\\Tests\\": "tests/"
29-
}
30-
},
31-
"require": {
32-
"php": ">=7.1"
33-
},
34-
"require-dev": {
35-
"tm/tooly-composer-script": "^1.0"
36-
},
37-
"scripts": {
38-
"post-install-cmd": "Tooly\\ScriptHandler::installPharTools",
39-
"post-update-cmd": "Tooly\\ScriptHandler::installPharTools"
40-
},
41-
"extra": {
42-
"tools": {
43-
"phpunit7": {
44-
"url": "https://phar.phpunit.de/phpunit-7.phar",
45-
"only-dev": true
46-
},
47-
"phpunit8": {
48-
"url": "https://phar.phpunit.de/phpunit-8.phar",
49-
"only-dev": true
50-
}
51-
}
52-
}
2+
"name": "hollodotme/fast-cgi-client",
3+
"description": "A PHP fast CGI client to send requests (a)synchronously to PHP-FPM.",
4+
"keywords": [
5+
"fastcgi",
6+
"php-fpm",
7+
"socket"
8+
],
9+
"minimum-stability": "dev",
10+
"prefer-stable": true,
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Holger Woltersdorf",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"bin": [
19+
"bin/fcgiget"
20+
],
21+
"autoload": {
22+
"psr-4": {
23+
"hollodotme\\FastCGI\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"hollodotme\\FastCGI\\Tests\\": "tests/"
29+
}
30+
},
31+
"require": {
32+
"php": ">=7.1"
33+
},
34+
"require-dev": {
35+
"tm/tooly-composer-script": "^1.0"
36+
},
37+
"scripts": {
38+
"post-install-cmd": "Tooly\\ScriptHandler::installPharTools",
39+
"post-update-cmd": "Tooly\\ScriptHandler::installPharTools"
40+
},
41+
"extra": {
42+
"tools": {
43+
"phpunit7": {
44+
"url": "https://phar.phpunit.de/phpunit-7.phar",
45+
"replace": true,
46+
"only-dev": true
47+
},
48+
"phpunit8": {
49+
"url": "https://phar.phpunit.de/phpunit-8.phar",
50+
"replace": true,
51+
"only-dev": true
52+
}
53+
}
54+
}
5355
}

0 commit comments

Comments
 (0)