iqueue.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. declare(strict_types=1);
  3. namespace queue;
  4. require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
  5. use Redis;
  6. use Exception;
  7. use Log;
  8. use refill\util;
  9. use Swoole;
  10. use refill;
  11. class IQueueDB
  12. {
  13. private $_redis;
  14. private $_queue_name;
  15. //存定义存储表的数量,系统会随机分配存储
  16. public function __construct($queue_name)
  17. {
  18. if ( !extension_loaded('redis') ) {
  19. throw_exception('redis failed to load');
  20. }
  21. $this->_queue_name = $queue_name;
  22. $this->_redis = new Redis();
  23. $this->connect();
  24. }
  25. public function connect(): bool
  26. {
  27. if ($this->_redis->isConnected()) {
  28. return true;
  29. }
  30. else {
  31. $this->close();
  32. $ret = $this->_redis->connect(C('queue.host'), C('queue.port'));
  33. Log::record("IQueueDB::connect ret = {$ret}", Log::DEBUG);
  34. return $this->_redis->isConnected();
  35. }
  36. }
  37. public function close()
  38. {
  39. if ($this->_redis->isConnected()) {
  40. $this->_redis->close();
  41. } else {
  42. Log::record("redis has closed",Log::DEBUG);
  43. }
  44. }
  45. public function rpush($value)
  46. {
  47. try
  48. {
  49. if($this->connect()) {
  50. $ret = $this->_redis->rPush($this->_queue_name, $value);
  51. return $ret;
  52. } else {
  53. Log::record("IQueueDB::rpush connect=false", Log::DEBUG);
  54. return false;
  55. }
  56. }
  57. catch(Exception $e) {
  58. Log::record("IQueueDB::rpush " . $e->getMessage(),Log::ERR);
  59. return false;
  60. }
  61. }
  62. public function lpush($value)
  63. {
  64. try
  65. {
  66. if ($this->connect()) {
  67. $ret = $this->_redis->lPush($this->_queue_name, $value);
  68. return $ret;
  69. } else {
  70. Log::record("IQueueDB::lpush connect=false", Log::DEBUG);
  71. return false;
  72. }
  73. }
  74. catch(Exception $e) {
  75. Log::record("IQueueDB::lpush " . $e->getMessage(),Log::ERR);
  76. return false;
  77. }
  78. }
  79. public function scan()
  80. {
  81. $list_key[] = $this->_queue_name;
  82. return $list_key;
  83. }
  84. public function rpop()
  85. {
  86. $key = $this->_queue_name;
  87. $result = $this->_redis->rPop($key);
  88. return $result;
  89. }
  90. public function lpop()
  91. {
  92. $key = $this->_queue_name;
  93. $result = $this->_redis->lPop($key);
  94. return $result;
  95. }
  96. public function brpop($key, $time)
  97. {
  98. $result = $this->_redis->brPop($key, $time);
  99. if ($result) {
  100. return $result[1];
  101. } else {
  102. return null;
  103. }
  104. }
  105. public function blpop($key, $time)
  106. {
  107. $result = $this->_redis->blPop($key, $time);
  108. if ($result) {
  109. return $result[1];
  110. } else {
  111. return null;
  112. }
  113. }
  114. public function clear() {
  115. $this->_redis->flushAll();
  116. }
  117. }
  118. /**
  119. * 队列处理
  120. *
  121. *
  122. * @package
  123. */
  124. class IClient
  125. {
  126. private $mQueuedb;
  127. public function __construct($queueDb)
  128. {
  129. $this->mQueuedb = $queueDb;
  130. }
  131. public function push($key, $value)
  132. {
  133. return $this->mQueuedb->lpush(serialize([$key=>$value]));
  134. }
  135. }
  136. class IServer
  137. {
  138. private $_queuedb;
  139. public function __construct($queueDb) {
  140. $this->_queuedb = $queueDb;
  141. }
  142. public function connect() : bool
  143. {
  144. return $this->_queuedb->connect();
  145. }
  146. public function rpop()
  147. {
  148. $result = $this->_queuedb->rpop();
  149. if($result != null) {
  150. return unserialize($result);
  151. } else {
  152. return false;
  153. }
  154. }
  155. public function lpop()
  156. {
  157. $result = $this->_queuedb->lpop();
  158. if($result != null) {
  159. return unserialize($result);
  160. } else {
  161. return false;
  162. }
  163. }
  164. public function brpop($key,$time)
  165. {
  166. $result = $this->_queuedb->brpop($key,$time);
  167. if($result != null) {
  168. return unserialize($result);
  169. } else {
  170. return false;
  171. }
  172. }
  173. public function blpop($key,$time)
  174. {
  175. $result = $this->_queuedb->lpop($key,$time);
  176. if($result != null) {
  177. return unserialize($result);
  178. } else {
  179. return false;
  180. }
  181. }
  182. public function scan() {
  183. return $this->_queuedb->scan();
  184. }
  185. public function stop()
  186. {
  187. $this->_queuedb->close();
  188. }
  189. }
  190. abstract class ILooper
  191. {
  192. private $_stop = false;
  193. private $_pause = 0;
  194. private $_cid = 0;
  195. private $mServer;
  196. const MAX_COROUTINE = 500;
  197. protected function __construct($server)
  198. {
  199. $this->mServer = $server;
  200. }
  201. public function prepare()
  202. {
  203. if (ob_get_level()) ob_end_clean();
  204. pcntl_signal(SIGINT, [$this,'sig_handler']);
  205. pcntl_signal(SIGHUP, [$this,'sig_handler']);
  206. pcntl_signal(SIGQUIT, [$this,'sig_handler']);
  207. pcntl_signal(SIGTERM, [$this,'sig_handler']);
  208. }
  209. abstract protected function handle($msg);
  210. public function pause()
  211. {
  212. Log::record("pause start subcoroutine cid={$this->_cid} pause={$this->_pause}",Log::DEBUG);
  213. $this->_pause = 1;
  214. do{
  215. Swoole\Coroutine::sleep(1);
  216. } while($this->_pause == 1);
  217. Log::record("pause end subcoroutine cid={$this->_cid} pause={$this->_pause}",Log::DEBUG);
  218. $this->wait();
  219. }
  220. public function resume()
  221. {
  222. Log::record("resume start subcoroutine cid={$this->_cid} pause={$this->_pause}",Log::DEBUG);
  223. if($this->_pause == 2) {
  224. $this->_pause = 0;
  225. Swoole\Coroutine::resume($this->_cid);
  226. }
  227. Log::record("resume end subcoroutine cid={$this->_cid} pause={$this->_pause}",Log::DEBUG);
  228. }
  229. public function stop()
  230. {
  231. Log::record(__FUNCTION__,Log::DEBUG);
  232. $this->_stop = true;
  233. $this->wait();
  234. }
  235. private function wait()
  236. {
  237. Log::record("wait start subcoroutine cid={$this->_cid} pause={$this->_pause}",Log::DEBUG);
  238. do {
  239. $res = Swoole\Coroutine::stats();
  240. $num = $res['coroutine_num'];
  241. if($num > 10) {
  242. Swoole\Coroutine::sleep(1);
  243. }
  244. } while($num > 10);
  245. do
  246. {
  247. $count = 0;
  248. $coros = Swoole\Coroutine::list();
  249. foreach ($coros as $cid)
  250. {
  251. $pcid = Swoole\Coroutine::getPcid($cid);
  252. if($pcid == $this->_cid) {
  253. $count += 1;
  254. }
  255. }
  256. if($count > 0) {
  257. Swoole\Coroutine::sleep(1);
  258. }
  259. Log::record("wait running subcoroutine count = {$count} quit.",Log::DEBUG);
  260. }
  261. while($count > 0);
  262. Log::record("wait end subcoroutine quit all",Log::DEBUG);
  263. }
  264. public function run()
  265. {
  266. refill\RefillFactory::instance();
  267. $this->_cid = Swoole\Coroutine::getCid();
  268. Log::record("runloop cid={$this->_cid}",Log::DEBUG);
  269. $queues = $this->mServer->scan();
  270. while (true)
  271. {
  272. try
  273. {
  274. if($this->_pause == 1) {
  275. Log::record("runlooper subcoroutine pause",Log::DEBUG);
  276. $this->_pause = 2;
  277. Swoole\Coroutine::suspend();
  278. Log::record("runlooper subcoroutine resume",Log::DEBUG);
  279. }
  280. if ($this->_stop) break;
  281. perfor_clear();
  282. if(defined('USE_COROUTINE') && USE_COROUTINE)
  283. {
  284. $res = Swoole\Coroutine::stats();
  285. $num = $res['coroutine_num'];
  286. $mem = memory_get_usage();
  287. if($num < ILooper::MAX_COROUTINE)
  288. {
  289. if($this->mServer->connect() == false) {
  290. Swoole\Coroutine::sleep(1);
  291. Log::record("Processor redis disconnect.",Log::ERR);
  292. continue;
  293. }
  294. if(defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP)
  295. {
  296. $content = $this->mServer->brpop($queues,1);
  297. if(empty($content)) {
  298. continue;
  299. }
  300. }
  301. else
  302. {
  303. $content = $this->mServer->rpop();
  304. if(empty($content)) {
  305. Swoole\Coroutine::sleep(1);
  306. continue;
  307. }
  308. }
  309. if($this->_stop)
  310. {
  311. foreach ($content as $key => $params) {
  312. util::push_queue($key, $params);
  313. }
  314. }
  315. else
  316. {
  317. go(function () use ($content, $num, $mem) {
  318. $start = microtime(true);
  319. Log::record("BeginGoFunction coroutin_num={$num} memory={$mem}", Log::DEBUG);
  320. $method = $this->handle($content);
  321. $use_time = microtime(true) - $start;
  322. $msg = sprintf("EndGoFunction coroutin_num={$num} memory={$mem} request_time=%.6f method={$method}", $use_time);
  323. Log::record($msg, Log::DEBUG);
  324. });
  325. }
  326. }
  327. else {
  328. Swoole\Coroutine::sleep(0.1);
  329. }
  330. }
  331. else
  332. {
  333. if($this->mServer->connect() == false) {
  334. sleep(1);
  335. continue;
  336. }
  337. $content = $this->mServer->pop($queues,1);
  338. if(empty($content)) continue;
  339. if($this->_stop)
  340. {
  341. foreach ($content as $key => $params) {
  342. DispatcherClient::instance()->push($key, $params);
  343. }
  344. }
  345. else
  346. {
  347. perfor_clear();
  348. perfor_start();
  349. $this->handle($content);
  350. perfor_end('Handle Request');
  351. $info = perfor_log();
  352. Log::record("{$info} \r\n",Log::DEBUG);
  353. }
  354. }
  355. }
  356. catch (Exception $e)
  357. {
  358. $err = $e->getMessage();
  359. $code = $e->getCode();
  360. Log::record("QueueDB pop err: code={$code} err={$err}",Log::ERR);
  361. }
  362. }
  363. Log::record("ILooper Run quit.", Log::DEBUG);
  364. }
  365. private function sig_handler($signo)
  366. {
  367. Log::record("queue quit at sig_handler.",Log::DEBUG);
  368. switch($signo) {
  369. case SIGINT:
  370. case SIGHUP:
  371. case SIGQUIT:
  372. case SIGTERM:
  373. $this->_stop = true;
  374. break;
  375. default:
  376. break;
  377. }
  378. }
  379. }