Skip to content

Commit

Permalink
Merge pull request #968 from twomiao/master
Browse files Browse the repository at this point in the history
Update worker
  • Loading branch information
walkor authored Oct 17, 2023
2 parents 74bce26 + 74e8dfb commit 1c24fe4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
12 changes: 2 additions & 10 deletions src/Events/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ class Swoole implements EventInterface
*/
protected $errorHandler = null;

/**
* Construct
*/
public function __construct()
{
// Avoid process exit due to no listening
Timer::tick(100000000, function () {
});
}

/**
* {@inheritdoc}
* @throws Throwable
Expand Down Expand Up @@ -219,6 +209,8 @@ public function deleteAllTimer(): void
*/
public function run(): void
{
// Avoid process exit due to no listening
Timer::tick(100000000, static fn() => null);
Event::wait();
}

Expand Down
30 changes: 22 additions & 8 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use function stream_socket_recvfrom;
use function substr;
use function array_walk;
use function get_class;

/**
* Worker class
Expand Down Expand Up @@ -627,6 +628,9 @@ protected static function init(): void
// State.
static::$status = static::STATUS_STARTING;

// Avoiding incorrect user calls.
static::resetGlobalEvent();

// For statistics.
static::$globalStatistics['start_timestamp'] = time();

Expand All @@ -640,6 +644,20 @@ protected static function init(): void
Timer::init();
}

/**
* reset globalEvent Instance.
*
* @return void
*/
protected static function resetGlobalEvent(): void
{
if (static::$status === static::STATUS_STARTING &&
static::$globalEvent instanceof EventInterface) {
static::$eventLoopClass = get_class(static::$globalEvent);
static::$globalEvent = null;
}
}

/**
* Lock.
*
Expand Down Expand Up @@ -1822,15 +1840,11 @@ protected static function reload(): void
foreach (static::$pidMap as $workerId => $workerPidArray) {
$worker = static::$workers[$workerId];
if ($worker->reloadable) {
foreach ($workerPidArray as $pid) {
$reloadablePidArray[$pid] = $pid;
}
} else {
foreach ($workerPidArray as $pid) {
// Send reload signal to a worker process which reloadable is false.
posix_kill($pid, $sig);
}
$reloadablePidArray += $workerPidArray;
continue;
}
// Send reload signal to a worker process which reloadable is false.
array_walk($workerPidArray, static fn ($pid) => posix_kill($pid, $sig));
}
// Get all pids that are waiting reload.
static::$pidsToRestart = array_intersect(static::$pidsToRestart, $reloadablePidArray);
Expand Down

0 comments on commit 1c24fe4

Please sign in to comment.