Skip to content

Commit

Permalink
全协程http服务
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed May 19, 2021
1 parent fda0569 commit 48fa836
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 433 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.DS_Store
*.xml
.idea
composer.lock
vendor
.phpunit.result.cache
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"require-dev": {
"symfony/var-dumper": "^4.3|^5.1",
"topthink/think-tracing": "^1.0",
"topthink/think-queue": "^3.0"
"topthink/think-queue": "^3.0",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
Expand All @@ -32,6 +33,11 @@
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"think\\tests\\swoole\\": "tests/"
}
},
"extra": {
"think": {
"services": [
Expand Down
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="ThinkPHP Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
35 changes: 6 additions & 29 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace think\swoole;

use think\swoole\concerns\InteractsWithCoordinator;
use think\swoole\concerns\InteractsWithHttp;
use think\swoole\concerns\InteractsWithPools;
use think\swoole\concerns\InteractsWithQueue;
Expand All @@ -28,8 +27,7 @@
*/
class Manager
{
use InteractsWithCoordinator,
InteractsWithServer,
use InteractsWithServer,
InteractsWithSwooleTable,
InteractsWithHttp,
InteractsWithWebsocket,
Expand All @@ -40,39 +38,18 @@ class Manager
WithContainer,
WithApplication;

/**
* Server events.
*
* @var array
*/
protected $events = [
'start',
'shutDown',
'workerStart',
'workerStop',
'workerError',
'workerExit',
'packet',
'task',
'finish',
'pipeMessage',
'managerStart',
'managerStop',
'request',
];

/**
* Initialize.
*/
protected function initialize(): void
{
$this->prepareTables();
$this->preparePools();
$this->prepareWebsocket();
$this->setSwooleServerListeners();
$this->prepareRpcServer();
$this->prepareQueue();
$this->prepareRpcClient();
$this->prepareHttp();
//$this->prepareWebsocket();
//$this->prepareRpcServer();
//$this->prepareQueue();
//$this->prepareRpcClient();
}

}
10 changes: 2 additions & 8 deletions src/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,9 @@ public function init($fd = null)

public function clear($snapshot = true)
{
if ($snapshot && $this->getSnapshot()) {
if ($snapshot && $app = $this->getSnapshot()) {
$app->clearInstances();
unset($this->snapshots[$this->getSnapshotId()]);

// 垃圾回收
$divisor = $this->config->get('swoole.gc.divisor', 100);
$probability = $this->config->get('swoole.gc.probability', 1);
if (random_int(1, $divisor) <= $probability) {
gc_collect_cycles();
}
}

Context::clear();
Expand Down
151 changes: 9 additions & 142 deletions src/command/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,30 @@

namespace think\swoole\command;

use Swoole\Http\Server as HttpServer;
use Swoole\WebSocket\Server as WebsocketServer;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\swoole\Manager;
use think\swoole\PidManager;

/**
* Swoole HTTP 命令行,支持操作:start|stop|restart|reload
* 支持应用配置目录下的swoole.php文件进行参数配置
*/
class Server extends Command
{
public function configure()
{
$this->setName('swoole')
->addArgument('action', Argument::OPTIONAL, 'start|stop|restart|reload', 'start')
->setDescription('Swoole HTTP Server for ThinkPHP');
->setDescription('Swoole HTTP Server for ThinkPHP');
}

protected function initialize(Input $input, Output $output)
public function handle(Manager $manager)
{
$this->app->bind(\Swoole\Server::class, function () {
return $this->createSwooleServer();
});
$this->checkEnvironment();

$this->app->bind(PidManager::class, function () {
return new PidManager($this->app->config->get('swoole.server.options.pid_file'));
});
}
$this->output->writeln('Starting swoole http server...');

public function handle()
{
$this->checkEnvironment();
$host = $manager->getConfig('server.host');
$port = $manager->getConfig('server.port');

$action = $this->input->getArgument('action');
$this->output->writeln("Swoole http server started: <http://{$host}:{$port}>");
$this->output->writeln('You can exit with <info>`CTRL-C`</info>');

if (in_array($action, ['start', 'stop', 'reload', 'restart'])) {
$this->app->invokeMethod([$this, $action], [], true);
} else {
$this->output->writeln("<error>Invalid argument action:{$action}, Expected start|stop|restart|reload .</error>");
}
$manager->start();
}

/**
Expand All @@ -75,117 +55,4 @@ protected function checkEnvironment()
}
}

/**
* 启动server
* @access protected
* @param Manager $manager
* @param PidManager $pidManager
* @return void
*/
protected function start(Manager $manager, PidManager $pidManager)
{
if ($pidManager->isRunning()) {
$this->output->writeln('<error>swoole http server process is already running.</error>');
return;
}

$this->output->writeln('Starting swoole http server...');

$host = $manager->getConfig('server.host');
$port = $manager->getConfig('server.port');

$this->output->writeln("Swoole http server started: <http://{$host}:{$port}>");
$this->output->writeln('You can exit with <info>`CTRL-C`</info>');

$manager->run();
}

/**
* 柔性重启server
* @access protected
* @param PidManager $manager
* @return void
*/
protected function reload(PidManager $manager)
{
if (!$manager->isRunning()) {
$this->output->writeln('<error>no swoole http server process running.</error>');
return;
}

$this->output->writeln('Reloading swoole http server...');

if (!$manager->killProcess(SIGUSR1)) {
$this->output->error('> failure');

return;
}

$this->output->writeln('> success');
}

/**
* 停止server
* @access protected
* @param PidManager $manager
* @return void
*/
protected function stop(PidManager $manager)
{
if (!$manager->isRunning()) {
$this->output->writeln('<error>no swoole http server process running.</error>');
return;
}

$this->output->writeln('Stopping swoole http server...');

$isRunning = $manager->killProcess(SIGTERM, 15);

if ($isRunning) {
$this->output->error('Unable to stop the swoole_http_server process.');
return;
}

$this->output->writeln('> success');
}

/**
* 重启server
* @access protected
* @param Manager $manager
* @param PidManager $pidManager
* @return void
*/
protected function restart(Manager $manager, PidManager $pidManager)
{
if ($pidManager->isRunning()) {
$this->stop($pidManager);
}

$this->start($manager, $pidManager);
}

/**
* Create swoole server.
*/
protected function createSwooleServer()
{
$isWebsocket = $this->app->config->get('swoole.websocket.enable', false);

$serverClass = $isWebsocket ? WebsocketServer::class : HttpServer::class;
$config = $this->app->config;
$host = $config->get('swoole.server.host');
$port = $config->get('swoole.server.port');
$socketType = $config->get('swoole.server.socket_type', SWOOLE_SOCK_TCP);
$mode = $config->get('swoole.server.mode', SWOOLE_PROCESS);

/** @var \Swoole\Server $server */
$server = new $serverClass($host, $port, $mode, $socketType);

$options = $config->get('swoole.server.options');

$server->set($options);
return $server;
}

}
25 changes: 0 additions & 25 deletions src/concerns/InteractsWithCoordinator.php

This file was deleted.

Loading

1 comment on commit 48fa836

@HyperLife1119
Copy link
Contributor

@HyperLife1119 HyperLife1119 commented on 48fa836 May 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对了,还要支持一个功能,在命令行启动应用程序的时候,应该支持指定env,多环境部署

Please sign in to comment.