Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build:

.PHONY: tests
tests:
docker-compose run --rm php vendor/bin/phpunit --no-progress
docker-compose run --rm php vendor/bin/phpunit

.PHONY: infection
infection:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"friendsofphp/php-cs-fixer": "^3.90",
"infection/infection": "^0.33",
"phpunit/phpunit": "^13.0",
"rector/rector": "^1.2",
"rector/rector": "^2.4",
"vimeo/psalm": "^6.10"
},
"autoload": {
Expand Down
16 changes: 2 additions & 14 deletions src/IPC/FdPasser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
use Socket;

use function array_key_exists;
use function assert;
use function defined;
use function function_exists;
use function gettype;
use function is_array;
use function is_resource;
use function strlen;

use function assert;
use function is_string;
use function strlen;

use const JSON_THROW_ON_ERROR;
use const MSG_DONTWAIT;
Expand Down Expand Up @@ -56,8 +55,6 @@ public function sendFd(Socket $controlSocket, Socket $fdToSend, array $metadata
throw new IPCException('SCM_RIGHTS is not defined');
}

$this->logger->debug('Sending FD with metadata', ['metadata' => $metadata]);

$metadataJson = json_encode($metadata, JSON_THROW_ON_ERROR);
if ('[]' === $metadataJson) {
$metadataJson = '{}';
Expand All @@ -80,8 +77,6 @@ public function sendFd(Socket $controlSocket, Socket $fdToSend, array $metadata
$this->logger->error('sendmsg failed', [
'error' => $this->socketWrapper->strerror($this->socketWrapper->lastError($controlSocket)),
]);
} else {
$this->logger->debug('FD sent', ['bytes' => $result]);
}

return false !== $result;
Expand Down Expand Up @@ -123,11 +118,6 @@ public function receiveFd(Socket $controlSocket): ?array
return null;
}

$this->logger->debug('recvmsg returned bytes', ['bytes' => $result]);
$this->logger->debug('Message type', ['type' => gettype($message)]);

$this->logger->debug('Message keys', ['keys' => array_keys($message)]);

assert(is_array($message['control']));

if (!array_key_exists(0, $message['control'])) {
Expand Down Expand Up @@ -178,8 +168,6 @@ public function receiveFd(Socket $controlSocket): ?array
$metadata = [];
}

$this->logger->debug('FD received successfully', ['metadata' => $metadata]);

return [
'fd' => $receivedFd,
'metadata' => $metadata,
Expand Down
16 changes: 4 additions & 12 deletions src/Master/CentralizedMaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ public function start(): void
$this->run();
}

#[Override]
public function stop(): void
{
parent::stop();
}

#[Override]
public function getMetrics(): array
{
Expand Down Expand Up @@ -309,7 +303,6 @@ private function runEventDrivenWorker(int $workerId, Socket $workerSocket): void
$this->logger->debug('External socket resource set for worker', [
'worker_id' => $workerId,
'mode' => 'centralized',
'note' => 'Unix socket for IPC - EvTimer fallback recommended',
]);

$server->enableNotification();
Expand Down Expand Up @@ -355,13 +348,12 @@ private function runCallbackWorker(int $workerId, Socket $workerSocket): void
{
$workerShouldStop = false;

pcntl_signal(SIGTERM, function () use (&$workerShouldStop): void {
$signalHandler = function () use (&$workerShouldStop): void {
$workerShouldStop = true;
});
};

pcntl_signal(SIGINT, function () use (&$workerShouldStop): void {
$workerShouldStop = true;
});
pcntl_signal(SIGTERM, $signalHandler);
pcntl_signal(SIGINT, $signalHandler);

$this->logger->info('Worker entering receive loop', ['worker_id' => $workerId]);

Expand Down
22 changes: 5 additions & 17 deletions src/Master/SharedSocketMaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ public function start(): void
$this->run();
}

#[Override]
public function stop(): void
{
parent::stop();
}

#[Override]
public function getMetrics(): array
{
Expand Down Expand Up @@ -222,20 +216,14 @@ private function runCallbackWorker(int $workerId): void

$workerShouldStop = false;

pcntl_signal(SIGTERM, function () use (&$workerShouldStop): void {
$signalHandler = function () use (&$workerShouldStop): void {
$workerShouldStop = true;
});
};

pcntl_signal(SIGINT, function () use (&$workerShouldStop): void {
$workerShouldStop = true;
});
pcntl_signal(SIGTERM, $signalHandler);
pcntl_signal(SIGINT, $signalHandler);

try {
$socket = $this->createReusePortSocket($workerId);
} catch (WorkerPoolException $e) {
$this->logger->error($e->getMessage(), ['worker_id' => $workerId]);
throw $e;
}
$socket = $this->createReusePortSocket($workerId);

$this->logger->info('Worker listening', [
'worker_id' => $workerId,
Expand Down
4 changes: 2 additions & 2 deletions src/Signal/SignalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ final class SignalHandler

public function register(int $signal, Closure $handler): void
{
$needsInstall = !isset($this->handlers[$signal]);
$needsInstall = false === isset($this->handlers[$signal]);

if (false === isset($this->handlers[$signal])) {
if ($needsInstall) {
$this->handlers[$signal] = [];
}

Expand Down
6 changes: 1 addition & 5 deletions src/Worker/EventDrivenWorkerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,8 @@ interface EventDrivenWorkerInterface
*
* @param int $workerId Worker ID (1, 2, 3, ..., N)
* @param ServerInterface $server Server instance for Worker Pool interaction
* - hasRequest() - check for requests
* - getRequest() - get next request
* - respond() - send response
* - hasPendingResponse() - check for pending responses
*
* @return void (never returns - infinite loop inside)
* @return void
*/
public function run(int $workerId, ServerInterface $server): void;
}
5 changes: 2 additions & 3 deletions src/Worker/HttpWorkerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
use Socket;
use Throwable;

use function assert;
use function count;
use function is_string;
use function sprintf;
use function strlen;

use function assert;
use function is_string;

use const SOL_SOCKET;
use const SO_RCVTIMEO;
use const SO_SNDTIMEO;
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/AbstractMasterCoverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function handle(mixed $clientSocket, array $metadata): void {}
$waitRef = new ReflectionMethod($master, 'waitForWorkers');
$waitRef->invoke($master);

$this->assertTrue(true);
$this->assertCount(1, $master->getWorkers());
}

#[Test]
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/CentralizedMasterCoverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function setUp(): void
#[Override]
protected function tearDown(): void
{
(new ErrorHandler(new NullLogger()))->reset();
new ErrorHandler(new NullLogger())->reset();
parent::tearDown();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/CentralizedMasterSocketResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function setUp(): void
#[Override]
protected function tearDown(): void
{
(new ErrorHandler(new NullLogger()))->reset();
new ErrorHandler(new NullLogger())->reset();
parent::tearDown();
}

Expand Down
46 changes: 34 additions & 12 deletions tests/Integration/ConcurrencyIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class ConcurrencyIntegrationTest extends TestCase
#[Override]
protected function tearDown(): void
{
(new ErrorHandler(new NullLogger()))->reset();
new ErrorHandler(new NullLogger())->reset();
parent::tearDown();
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public function handle(mixed $clientSocket, array $metadata): void

$requestId = self::$counter;

usleep(10000); // Simulate some work
usleep(10000);

$response = "HTTP/1.1 200 OK\r\n\r\nRequest: $requestId";
socket_write($clientSocket, $response);
Expand All @@ -92,7 +92,7 @@ public function handle(mixed $clientSocket, array $metadata): void

$pid = pcntl_fork();

if ($pid === 0) {
if (0 === $pid) {
$master->start();
exit(0);
}
Expand All @@ -105,14 +105,18 @@ public function handle(mixed $clientSocket, array $metadata): void
for ($i = 0; $i < $concurrentRequests; $i++) {
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if (@socket_connect($client, '127.0.0.1', $port)) {
$previousErrorReporting = error_reporting(0);
if (socket_connect($client, '127.0.0.1', $port)) {
error_reporting($previousErrorReporting);
socket_set_nonblock($client);
socket_write($client, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");

$response = '';
$attempts = 0;
while ($attempts < 100) {
$chunk = @socket_read($client, 1024);
$previousErrorReporting = error_reporting(0);
$chunk = socket_read($client, 1024);
error_reporting($previousErrorReporting);
if ($chunk) {
$response .= $chunk;
}
Expand All @@ -128,6 +132,8 @@ public function handle(mixed $clientSocket, array $metadata): void
}

socket_close($client);
} else {
error_reporting($previousErrorReporting);
}

usleep(5000);
Expand Down Expand Up @@ -195,7 +201,7 @@ public function handle(mixed $clientSocket, array $metadata): void

$pid = pcntl_fork();

if ($pid === 0) {
if (0 === $pid) {
$master->start();
exit(0);
}
Expand All @@ -207,11 +213,19 @@ public function handle(mixed $clientSocket, array $metadata): void
for ($i = 0; $i < 4; $i++) {
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if (@socket_connect($client, '127.0.0.1', $port)) {
$previousErrorReporting = error_reporting(0);
if (socket_connect($client, '127.0.0.1', $port)) {
error_reporting($previousErrorReporting);
socket_write($client, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");

$response = '';
while ($chunk = @socket_read($client, 1024)) {
while (true) {
$previousErrorReporting = error_reporting(0);
$chunk = socket_read($client, 1024);
error_reporting($previousErrorReporting);
if (false === $chunk || '' === $chunk) {
break;
}
$response .= $chunk;
}

Expand All @@ -220,6 +234,8 @@ public function handle(mixed $clientSocket, array $metadata): void
}

socket_close($client);
} else {
error_reporting($previousErrorReporting);
}

usleep(50000);
Expand Down Expand Up @@ -275,7 +291,7 @@ public function handle(mixed $clientSocket, array $metadata): void

$pid = pcntl_fork();

if ($pid === 0) {
if (0 === $pid) {
$master->start();
exit(0);
}
Expand All @@ -287,22 +303,28 @@ public function handle(mixed $clientSocket, array $metadata): void
for ($i = 0; $i < 50; $i++) {
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if (@socket_connect($client, '127.0.0.1', $port)) {
$previousErrorReporting = error_reporting(0);
if (socket_connect($client, '127.0.0.1', $port)) {
error_reporting($previousErrorReporting);
socket_write($client, "GET / HTTP/1.1\r\n\r\n");
$response = @socket_read($client, 1024);
$previousErrorReporting = error_reporting(0);
$response = socket_read($client, 1024);
error_reporting($previousErrorReporting);

if ($response && str_contains($response, 'HTTP')) {
++$successCount;
}

socket_close($client);
} else {
error_reporting($previousErrorReporting);
}
}

posix_kill($pid, SIGTERM);
pcntl_waitpid($pid, $status);

if ($successCount === 0) {
if (0 === $successCount) {
$this->markTestSkipped('No successful rapid connections');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/ForkBasedMasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class ForkBasedMasterTest extends TestCase
#[Override]
protected function tearDown(): void
{
(new ErrorHandler(new NullLogger()))->reset();
new ErrorHandler(new NullLogger())->reset();
parent::tearDown();
}

Expand Down
Loading
Loading