Skip to content

Commit

Permalink
Merge branch '4.1' of github.com:walkor/workerman into 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Feb 19, 2024
2 parents 05ede10 + 89417f5 commit 3ce1b6e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
4 changes: 1 addition & 3 deletions Events/Ev.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ public function loop()
*/
public function destroy()
{
foreach ($this->_allEvents as $event) {
$event->stop();
}
\Ev::stop(\Ev::BREAK_ALL);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Events/Uv.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public function add($fd, $flag, $func, $args = null)
$repeat = $flag === self::EV_TIMER_ONCE ? 0 : (int)($fd * 1000);
$param = array($func, (array)$args, $flag, $fd, self::$_timerId);
$timerWatcher = \uv_timer_init();
\uv_timer_start($timerWatcher, 1, $repeat, function($watcher)use($param){
call_user_func_array([$this, 'timerCallback'], [$param]);
\uv_timer_start($timerWatcher, ($flag === self::EV_TIMER_ONCE ? (int)($fd * 1000) :1), $repeat, function($watcher)use($param){
call_user_func_array([$this, 'timerCallback'], [$param]);
});
$this->_eventTimer[self::$_timerId] = $timerWatcher;
return self::$_timerId++;
Expand Down
25 changes: 21 additions & 4 deletions Protocols/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Request
public $properties = array();

/**
* @var int
* @var int
*/
public static $maxFileUploads = 1024;

Expand All @@ -71,6 +71,13 @@ class Request
*/
protected static $_enableCache = true;

/**
* Is safe.
*
* @var bool
*/
protected $_isSafe = true;


/**
* Request constructor.
Expand Down Expand Up @@ -208,8 +215,8 @@ public function protocolVersion()
public function host($without_port = false)
{
$host = $this->header('host');
if ($host && $without_port && $pos = \strpos($host, ':')) {
return \substr($host, 0, $pos);
if ($host && $without_port) {
return preg_replace('/:\d{1,5}$/', '', $host);
}
return $host;
}
Expand Down Expand Up @@ -656,14 +663,24 @@ public function __toString()
return $this->_buffer;
}

/**
* __wakeup.
*
* @return void
*/
public function __wakeup()
{
$this->_isSafe = false;
}

/**
* __destruct.
*
* @return void
*/
public function __destruct()
{
if (isset($this->_data['files'])) {
if (isset($this->_data['files']) && $this->_isSafe) {
\clearstatcache();
\array_walk_recursive($this->_data['files'], function($value, $key){
if ($key === 'tmp_name') {
Expand Down
20 changes: 20 additions & 0 deletions Protocols/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ class Session
*/
protected $_sessionId = null;

/**
* Is safe.
*
* @var bool
*/
protected $_isSafe = true;

/**
* Session constructor.
*
Expand Down Expand Up @@ -402,13 +409,26 @@ public function gc()
static::$_handler->gc(static::$lifetime);
}

/**
* __wakeup.
*
* @return void
*/
public function __wakeup()
{
$this->_isSafe = false;
}

/**
* __destruct.
*
* @return void
*/
public function __destruct()
{
if (!$this->_isSafe) {
return;
}
$this->save();
if (\random_int(1, static::$gcProbability[1]) <= static::$gcProbability[0]) {
$this->gc();
Expand Down
23 changes: 16 additions & 7 deletions Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Worker
*
* @var string
*/
const VERSION = '4.1.11';
const VERSION = '4.1.14';

/**
* Status starting.
Expand Down Expand Up @@ -569,8 +569,8 @@ public static function runAll()
*/
protected static function checkSapiEnv()
{
// Only for cli.
if (\PHP_SAPI !== 'cli') {
// Only for cli and micro.
if (!in_array(\PHP_SAPI, ['cli', 'micro'])) {
exit("Only run in command line mode \n");
}
if (\DIRECTORY_SEPARATOR === '\\') {
Expand Down Expand Up @@ -943,7 +943,7 @@ protected static function parseCommand()
exit;
}

$statistics_file = static::$statusFile ? static::$statusFile : __DIR__ . "/../workerman-$master_pid.$command";
$statistics_file = static::$statusFile ? static::$statusFile : __DIR__ . "/../workerman-$master_pid.status";

// execute command.
switch ($command) {
Expand Down Expand Up @@ -1060,8 +1060,14 @@ protected static function formatStatusData($statistics_file)
}
$status_str = '';
$current_total_request = array();
$worker_info = \unserialize($info[0]);
\ksort($worker_info, SORT_NUMERIC);
$workerInfo = [];
try {
$workerInfo = unserialize($info[0], ['allowed_classes' => false]);
} catch (Throwable $exception) {}
if (!is_array($workerInfo)) {
$workerInfo = [];
}
\ksort($workerInfo, SORT_NUMERIC);
unset($info[0]);
$data_waiting_sort = array();
$read_process_status = false;
Expand Down Expand Up @@ -1096,7 +1102,7 @@ protected static function formatStatusData($statistics_file)
}
}
}
foreach($worker_info as $pid => $info) {
foreach($workerInfo as $pid => $info) {
if (!isset($data_waiting_sort[$pid])) {
$status_str .= "$pid\t" . \str_pad('N/A', 7) . " "
. \str_pad($info['listen'], static::$_maxSocketNameLength) . " "
Expand Down Expand Up @@ -1472,6 +1478,9 @@ protected static function forkWorkersForWindows()

\restore_error_handler();

// Add an empty timer to prevent the event-loop from exiting.
Timer::add(1000000, function (){});

// Display UI.
static::safeEcho(\str_pad($worker->name, 48) . \str_pad($worker->getSocketName(), 36) . \str_pad('1', 10) . " [ok]\n");
$worker->listen();
Expand Down

0 comments on commit 3ce1b6e

Please sign in to comment.