11<?php
22namespace Swango ;
3+ use Swango \Environment ;
4+
35class HttpServer {
46 protected static $ worker , $ worker_id , $ terminal_server , $ http_request_counter , $ max_coroutine , $ is_stopping = false ;
57 public static function getWorkerId (): ?int {
@@ -25,7 +27,6 @@ public static function isStopping(): bool {
2527 'task_ipc_mode ' => 1 , // 3为使用消息队列通信,争抢模式,无法使用定向投递
2628 'task_max_request ' => 5000 , // task进程处理200个请求后自动退出,防止内存溢出
2729 'backlog ' => 128 , // 最多同时有多少个等待accept的连接
28- 'pid_file ' => \Swango \Environment::getDir ()->base . \Swango \Environment::getName () . '.pid ' ,
2930 'max_request ' => 0 , // worker永不退出
3031 'reload_async ' => true ,
3132 'http_parse_post ' => false , // 不自动解析POST包体
@@ -75,16 +76,17 @@ public function __construct() {
7576 'onRequest '
7677 ]
7778 ];
79+ $ this ->swoole_server_config ['pid_file ' ] = Environment::getDir ()->base . Environment::getName () . '.pid ' ;
7880 }
7981 protected function loadConfig (): void {
80- $ daemon_config = \ Swango \ Environment::getServiceConfig ();
82+ $ daemon_config = Environment::getServiceConfig ();
8183 $ this ->swoole_server_config ['reactor_num ' ] = $ daemon_config ->reactor_num ;
8284 $ this ->swoole_server_config ['worker_num ' ] = $ daemon_config ->worker_num ;
8385 $ this ->swoole_server_config ['task_worker_num ' ] = $ daemon_config ->task_worker_num ;
8486 $ this ->swoole_server_config ['task_max_request ' ] = $ daemon_config ->task_max_request ;
8587 }
8688 protected function createSwooleServer (): void {
87- $ daemon_config = \ Swango \ Environment::getServiceConfig ();
89+ $ daemon_config = Environment::getServiceConfig ();
8890 $ this ->server = new \Swoole \Http \Server ($ daemon_config ->http_server_host , $ daemon_config ->http_server_port );
8991 self ::$ terminal_server = new HttpServer \TerminalServer (
9092 $ this ->server ->addListener ($ daemon_config ->terminal_server_host , $ daemon_config ->terminal_server_port ,
@@ -95,7 +97,7 @@ protected function bindCallBack(): void {
9597 $ this ->server ->on ($ event , $ func );
9698 }
9799 protected function initBeforeStart (): void {
98- mt_srand ((int )(microtime (true ) * 10000 ) * 100 + ip2long (\ Swango \ Environment::getServiceConfig ()->local_ip ));
100+ mt_srand ((int )(microtime (true ) * 10000 ) * 100 + ip2long (Environment::getServiceConfig ()->local_ip ));
99101 define ('SERVER_TEMP_ID ' , mt_rand (0 , 4294967295 ));
100102
101103 $ this ->swoole_server_config ['dispatch_mode ' ] = 3 ;
@@ -112,7 +114,7 @@ public function start($daemonize = false): void {
112114 if ($ this ->getPid () !== null )
113115 exit ("Already running \n" );
114116
115- $ this ->swoole_server_config ['log_file ' ] = \ Swango \ Environment::getDir ()->log . 'swoole.log ' ;
117+ $ this ->swoole_server_config ['log_file ' ] = Environment::getDir ()->log . 'swoole.log ' ;
116118 $ this ->daemonize = $ daemonize ;
117119 $ this ->loadConfig ();
118120 $ this ->createSwooleServer ();
@@ -124,7 +126,7 @@ public function start($daemonize = false): void {
124126 $ this ->server ->start ();
125127 }
126128 public function getPid (): ?int {
127- $ pidfile = \ Swango \ Environment::getDir ()->base . \ Swango \ Environment::getName () . '.pid ' ;
129+ $ pidfile = Environment::getDir ()->base . Environment::getName () . '.pid ' ;
128130 if (file_exists ($ pidfile )) {
129131 $ pid = file_get_contents ($ pidfile );
130132 return $ pid && @posix_kill ($ pid , 0 ) ? $ pid : null ;
@@ -171,26 +173,26 @@ public function talk(array $cmds, string $host = '127.0.0.1', ?int $port = null)
171173 'package_eof ' => "\r\n" ,
172174 'package_max_length ' => 1024 * 1024 * 2
173175 ]);
174- if (! $ client ->connect ($ host , $ port ?? \ Swango \ Environment::getServiceConfig ()->terminal_server_port , - 1 ))
176+ if (! $ client ->connect ($ host , $ port ?? Environment::getServiceConfig ()->terminal_server_port , - 1 ))
175177 exit ("connect failed. Error: {$ client ->errCode }\n" );
176178 $ client ->send (\Swoole \Serialize::pack ($ cmds , SWOOLE_FAST_PACK ) . "\r\n" );
177179 for ($ response = $ client ->recv (); $ response !== '' && $ response !== false ; $ response = $ client ->recv ())
178180 echo $ response ;
179181 $ client ->close ();
180182 }
181183 public function onStart (\Swoole \Server $ server ): void {
182- @cli_set_process_title (\ Swango \ Environment::getName () . ' master ' );
184+ @cli_set_process_title (Environment::getName () . ' master ' );
183185 }
184186 public function onManagerStart (\Swoole \Server $ server ): void {
185- @cli_set_process_title (\ Swango \ Environment::getName () . ' manager ' );
187+ @cli_set_process_title (Environment::getName () . ' manager ' );
186188 }
187189 private function onTaskStart (\Swoole \Server $ serv , $ worker_id ): void {
188190 define ('SWANGO_WORKING_IN_TASK ' , true );
189- \ Swango \ Environment::getWorkingMode ()->reset ();
191+ Environment::getWorkingMode ()->reset ();
190192 }
191193 private function onWorkerStart (\Swoole \Server $ serv , $ worker_id ): void {
192194 define ('SWANGO_WORKING_IN_WORKER ' , true );
193- \ Swango \ Environment::getWorkingMode ()->reset ();
195+ Environment::getWorkingMode ()->reset ();
194196 $ serv ->worker_http_request_counter = 0 ;
195197 \Swango \Cache \RedisPool::initInWorker ();
196198 if ($ worker_id === 0 ) {
@@ -200,7 +202,7 @@ private function onWorkerStart(\Swoole\Server $serv, $worker_id): void {
200202 function (int $ timer_id ) use ($ serv ) {
201203 \Swango \Db \Pool \master::addWorkerCountToAtomic (true );
202204 \Swango \Db \Pool \slave::addWorkerCountToAtomic (true );
203- for ($ dst_worker_id = 1 ; $ dst_worker_id < \ Swango \ Environment::getServiceConfig ()->worker_num ; ++ $ dst_worker_id )
205+ for ($ dst_worker_id = 1 ; $ dst_worker_id < Environment::getServiceConfig ()->worker_num ; ++ $ dst_worker_id )
204206 @$ serv ->sendMessage (pack ('n ' , 3 ), $ dst_worker_id );
205207 });
206208 }
@@ -211,11 +213,11 @@ public function onAllWorkerStart(\Swoole\Server $serv, $worker_id): void {
211213 self ::$ worker = $ serv ;
212214 self ::$ worker_id = $ worker_id ;
213215 if ($ worker_id < $ this ->swoole_server_config ['worker_num ' ]) {
214- @cli_set_process_title (\ Swango \ Environment::getName () . ' worker ' . $ worker_id );
216+ @cli_set_process_title (Environment::getName () . ' worker ' . $ worker_id );
215217 $ this ->onWorkerStart ($ serv , $ worker_id );
216218 } else {
217219 $ this ->onTaskStart ($ serv , $ worker_id );
218- @cli_set_process_title (\ Swango \ Environment::getName () . ' task ' . $ worker_id );
220+ @cli_set_process_title (Environment::getName () . ' task ' . $ worker_id );
219221 }
220222 }
221223 private function recycle (): void {
@@ -241,14 +243,14 @@ public function onWorkerError(\Swoole\Server $serv, $worker_id, $worker_pid, $ex
241243 public function onWorkerStop (\Swoole \Server $ serv , $ worker_id ): void {
242244 if (! $ this ->stopping_process_title_set ) {
243245 $ this ->stopping_process_title_set = true ;
244- @cli_set_process_title (\ Swango \ Environment::getName () . ' worker ' . $ worker_id . ' [stopping] ' );
246+ @cli_set_process_title (Environment::getName () . ' worker ' . $ worker_id . ' [stopping] ' );
245247 }
246248 $ this ->recycle ();
247249 }
248250 public function onWorkerExit (\Swoole \Server $ serv , $ worker_id ): void {
249251 if (! $ this ->stopping_process_title_set ) {
250252 $ this ->stopping_process_title_set = true ;
251- @cli_set_process_title (\ Swango \ Environment::getName () . ' worker ' . $ worker_id . ' [stopping] ' );
253+ @cli_set_process_title (Environment::getName () . ' worker ' . $ worker_id . ' [stopping] ' );
252254 }
253255 $ this ->recycle ();
254256 }
@@ -337,7 +339,7 @@ public function onTask(\Swoole\Server $serv, int $task_id, int $src_worker_id, $
337339 if ($ cmd === 1 || $ cmd === 2 ) {
338340 static $ certs = [];
339341 if (! array_key_exists ($ index , $ certs )) {
340- $ certname = \ Swango \ Environment::getDir ()->data . 'cert/rsa_private_key_ ' . $ index . '.pem ' ;
342+ $ certname = Environment::getDir ()->data . 'cert/rsa_private_key_ ' . $ index . '.pem ' ;
341343 if (! file_exists ($ certname ))
342344 return - 3 ;
343345 $ key = include $ certname ;
0 commit comments