Skip to content

Commit 48fa836

Browse files
committed
全协程http服务
1 parent fda0569 commit 48fa836

16 files changed

+113
-433
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.DS_Store
2-
*.xml
32
.idea
43
composer.lock
54
vendor
5+
.phpunit.result.cache

composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"require-dev": {
2323
"symfony/var-dumper": "^4.3|^5.1",
2424
"topthink/think-tracing": "^1.0",
25-
"topthink/think-queue": "^3.0"
25+
"topthink/think-queue": "^3.0",
26+
"phpunit/phpunit": "^9.5"
2627
},
2728
"autoload": {
2829
"psr-4": {
@@ -32,6 +33,11 @@
3233
"src/helpers.php"
3334
]
3435
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"think\\tests\\swoole\\": "tests/"
39+
}
40+
},
3541
"extra": {
3642
"think": {
3743
"services": [

phpunit.xml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
beStrictAboutTestsThatDoNotTestAnything="false"
5+
bootstrap="tests/bootstrap.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnError="false"
12+
stopOnFailure="false"
13+
verbose="true"
14+
>
15+
<testsuites>
16+
<testsuite name="ThinkPHP Test Suite">
17+
<directory suffix="Test.php">./tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
<filter>
21+
<whitelist processUncoveredFilesFromWhitelist="true">
22+
<directory suffix=".php">./src</directory>
23+
</whitelist>
24+
</filter>
25+
</phpunit>

src/Manager.php

+6-29
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace think\swoole;
1313

14-
use think\swoole\concerns\InteractsWithCoordinator;
1514
use think\swoole\concerns\InteractsWithHttp;
1615
use think\swoole\concerns\InteractsWithPools;
1716
use think\swoole\concerns\InteractsWithQueue;
@@ -28,8 +27,7 @@
2827
*/
2928
class Manager
3029
{
31-
use InteractsWithCoordinator,
32-
InteractsWithServer,
30+
use InteractsWithServer,
3331
InteractsWithSwooleTable,
3432
InteractsWithHttp,
3533
InteractsWithWebsocket,
@@ -40,39 +38,18 @@ class Manager
4038
WithContainer,
4139
WithApplication;
4240

43-
/**
44-
* Server events.
45-
*
46-
* @var array
47-
*/
48-
protected $events = [
49-
'start',
50-
'shutDown',
51-
'workerStart',
52-
'workerStop',
53-
'workerError',
54-
'workerExit',
55-
'packet',
56-
'task',
57-
'finish',
58-
'pipeMessage',
59-
'managerStart',
60-
'managerStop',
61-
'request',
62-
];
63-
6441
/**
6542
* Initialize.
6643
*/
6744
protected function initialize(): void
6845
{
6946
$this->prepareTables();
7047
$this->preparePools();
71-
$this->prepareWebsocket();
72-
$this->setSwooleServerListeners();
73-
$this->prepareRpcServer();
74-
$this->prepareQueue();
75-
$this->prepareRpcClient();
48+
$this->prepareHttp();
49+
//$this->prepareWebsocket();
50+
//$this->prepareRpcServer();
51+
//$this->prepareQueue();
52+
//$this->prepareRpcClient();
7653
}
7754

7855
}

src/Sandbox.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,9 @@ public function init($fd = null)
104104

105105
public function clear($snapshot = true)
106106
{
107-
if ($snapshot && $this->getSnapshot()) {
107+
if ($snapshot && $app = $this->getSnapshot()) {
108+
$app->clearInstances();
108109
unset($this->snapshots[$this->getSnapshotId()]);
109-
110-
// 垃圾回收
111-
$divisor = $this->config->get('swoole.gc.divisor', 100);
112-
$probability = $this->config->get('swoole.gc.probability', 1);
113-
if (random_int(1, $divisor) <= $probability) {
114-
gc_collect_cycles();
115-
}
116110
}
117111

118112
Context::clear();

src/command/Server.php

+9-142
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,30 @@
1111

1212
namespace think\swoole\command;
1313

14-
use Swoole\Http\Server as HttpServer;
15-
use Swoole\WebSocket\Server as WebsocketServer;
1614
use think\console\Command;
17-
use think\console\Input;
18-
use think\console\input\Argument;
19-
use think\console\Output;
2015
use think\swoole\Manager;
21-
use think\swoole\PidManager;
2216

23-
/**
24-
* Swoole HTTP 命令行,支持操作:start|stop|restart|reload
25-
* 支持应用配置目录下的swoole.php文件进行参数配置
26-
*/
2717
class Server extends Command
2818
{
2919
public function configure()
3020
{
3121
$this->setName('swoole')
32-
->addArgument('action', Argument::OPTIONAL, 'start|stop|restart|reload', 'start')
33-
->setDescription('Swoole HTTP Server for ThinkPHP');
22+
->setDescription('Swoole HTTP Server for ThinkPHP');
3423
}
3524

36-
protected function initialize(Input $input, Output $output)
25+
public function handle(Manager $manager)
3726
{
38-
$this->app->bind(\Swoole\Server::class, function () {
39-
return $this->createSwooleServer();
40-
});
27+
$this->checkEnvironment();
4128

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

47-
public function handle()
48-
{
49-
$this->checkEnvironment();
31+
$host = $manager->getConfig('server.host');
32+
$port = $manager->getConfig('server.port');
5033

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

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

6040
/**
@@ -75,117 +55,4 @@ protected function checkEnvironment()
7555
}
7656
}
7757

78-
/**
79-
* 启动server
80-
* @access protected
81-
* @param Manager $manager
82-
* @param PidManager $pidManager
83-
* @return void
84-
*/
85-
protected function start(Manager $manager, PidManager $pidManager)
86-
{
87-
if ($pidManager->isRunning()) {
88-
$this->output->writeln('<error>swoole http server process is already running.</error>');
89-
return;
90-
}
91-
92-
$this->output->writeln('Starting swoole http server...');
93-
94-
$host = $manager->getConfig('server.host');
95-
$port = $manager->getConfig('server.port');
96-
97-
$this->output->writeln("Swoole http server started: <http://{$host}:{$port}>");
98-
$this->output->writeln('You can exit with <info>`CTRL-C`</info>');
99-
100-
$manager->run();
101-
}
102-
103-
/**
104-
* 柔性重启server
105-
* @access protected
106-
* @param PidManager $manager
107-
* @return void
108-
*/
109-
protected function reload(PidManager $manager)
110-
{
111-
if (!$manager->isRunning()) {
112-
$this->output->writeln('<error>no swoole http server process running.</error>');
113-
return;
114-
}
115-
116-
$this->output->writeln('Reloading swoole http server...');
117-
118-
if (!$manager->killProcess(SIGUSR1)) {
119-
$this->output->error('> failure');
120-
121-
return;
122-
}
123-
124-
$this->output->writeln('> success');
125-
}
126-
127-
/**
128-
* 停止server
129-
* @access protected
130-
* @param PidManager $manager
131-
* @return void
132-
*/
133-
protected function stop(PidManager $manager)
134-
{
135-
if (!$manager->isRunning()) {
136-
$this->output->writeln('<error>no swoole http server process running.</error>');
137-
return;
138-
}
139-
140-
$this->output->writeln('Stopping swoole http server...');
141-
142-
$isRunning = $manager->killProcess(SIGTERM, 15);
143-
144-
if ($isRunning) {
145-
$this->output->error('Unable to stop the swoole_http_server process.');
146-
return;
147-
}
148-
149-
$this->output->writeln('> success');
150-
}
151-
152-
/**
153-
* 重启server
154-
* @access protected
155-
* @param Manager $manager
156-
* @param PidManager $pidManager
157-
* @return void
158-
*/
159-
protected function restart(Manager $manager, PidManager $pidManager)
160-
{
161-
if ($pidManager->isRunning()) {
162-
$this->stop($pidManager);
163-
}
164-
165-
$this->start($manager, $pidManager);
166-
}
167-
168-
/**
169-
* Create swoole server.
170-
*/
171-
protected function createSwooleServer()
172-
{
173-
$isWebsocket = $this->app->config->get('swoole.websocket.enable', false);
174-
175-
$serverClass = $isWebsocket ? WebsocketServer::class : HttpServer::class;
176-
$config = $this->app->config;
177-
$host = $config->get('swoole.server.host');
178-
$port = $config->get('swoole.server.port');
179-
$socketType = $config->get('swoole.server.socket_type', SWOOLE_SOCK_TCP);
180-
$mode = $config->get('swoole.server.mode', SWOOLE_PROCESS);
181-
182-
/** @var \Swoole\Server $server */
183-
$server = new $serverClass($host, $port, $mode, $socketType);
184-
185-
$options = $config->get('swoole.server.options');
186-
187-
$server->set($options);
188-
return $server;
189-
}
190-
19158
}

src/concerns/InteractsWithCoordinator.php

-25
This file was deleted.

0 commit comments

Comments
 (0)