Skip to content

Commit fb5660f

Browse files
committed
detect grpc fork support
1 parent 9a17344 commit fb5660f

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/Environment/Fork/ForkWorker.php

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@
1818
*/
1919
class ForkWorker extends SocketWorker
2020
{
21+
/**
22+
* @return bool
23+
*/
24+
public static function isSupported(): bool
25+
{
26+
if (!extension_loaded("pcntl")) {
27+
return false;
28+
}
29+
30+
// GRPC only supports forks if explicitly enabled using the GRPC_ENABLE_FORK_SUPPORT environment variable
31+
// see https://github.com/grpc/grpc/issues/13412
32+
// If you don't use GRPC, but have the extension loaded, you can always define your workers manually
33+
// or override the automatic worker selection using the TASKMASTER_WORKER environment variable
34+
if (extension_loaded("grpc") && getenv("GRPC_ENABLE_FORK_SUPPORT") !== "1") {
35+
return false;
36+
}
37+
38+
return true;
39+
}
40+
2141
/**
2242
* @return WorkerInstanceInterface
2343
*/

src/Environment/Process/ProcessWorker.php

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
*/
1717
class ProcessWorker extends SocketWorker
1818
{
19+
/**
20+
* @return bool
21+
*/
22+
public static function isSupported(): bool
23+
{
24+
return function_exists("proc_open") && PHP_OS_FAMILY !== "Windows";
25+
}
26+
1927
/**
2028
* @return ProcessWorkerInstance
2129
*/

src/Taskmaster.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ protected function getSelectableStreams(): array
340340
*
341341
* @param SocketInterface|null $socket
342342
* @return resource|null
343-
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
344343
*/
345344
protected function getSelectableReadStreamFromSocket(?SocketInterface $socket): mixed
346345
{
@@ -463,9 +462,9 @@ public function autoDetectWorkers(int $count, bool $loadFromEnv = true, bool $al
463462
}
464463

465464
if (!$worker) {
466-
if (extension_loaded("pcntl")) {
465+
if (ForkWorker::isSupported()) {
467466
$worker = new ForkWorker();
468-
} elseif (function_exists("proc_open") && PHP_OS_FAMILY !== "Windows") {
467+
} elseif (ProcessWorker::isSupported()) {
469468
$worker = new ProcessWorker();
470469
} else {
471470
$worker = new SyncWorker();

0 commit comments

Comments
 (0)