iqueue.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. class IQueueDB
  9. {
  10. private $_redis;
  11. private $_tb_prefix;
  12. //存定义存储表的数量,系统会随机分配存储
  13. private $_tb_num = 3;
  14. private $_comode;
  15. public function __construct($queue_name,$comode = false)
  16. {
  17. if ( !extension_loaded('redis') ) {
  18. throw_exception('redis failed to load');
  19. }
  20. $this->_tb_prefix = $queue_name;
  21. $this->_comode = $comode;
  22. if ($this->_comode) {
  23. $this->_redis = new \Swoole\Coroutine\Redis();
  24. $ret = $this->_redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  25. } else {
  26. $this->_redis = new Redis();
  27. $ret = $this->_redis->connect(C('queue.host'), C('queue.port'), 20);
  28. }
  29. $this->connect();
  30. }
  31. public function connect(): bool
  32. {
  33. if ($this->_comode) {
  34. if ($this->_redis->connected) return true;
  35. $ret = $this->_redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  36. Log::record("Swoole\Coroutine\Redis connect ret = {$ret}", Log::DEBUG);
  37. return $this->_redis->connected;
  38. } else {
  39. if ($this->_redis->isConnected()) return true;
  40. $ret = $this->_redis->connect(C('queue.host'), C('queue.port'), 20);
  41. Log::record("Redis connect ret = {$ret}", Log::DEBUG);
  42. return $this->_redis->isConnected();
  43. }
  44. }
  45. public function close()
  46. {
  47. if ($this->_comode)
  48. {
  49. if ($this->_redis->connected) {
  50. $this->_redis->close();
  51. }
  52. } elseif ($this->_redis->isConnected()) {
  53. $this->_redis->close();
  54. } else {
  55. Log::record("redis has closed",Log::DEBUG);
  56. }
  57. }
  58. public function rpush($value)
  59. {
  60. try {
  61. return $this->_redis->rPush($this->_tb_prefix . rand(1, $this->_tb_num), $value);
  62. } catch(Exception $e) {
  63. throw_exception($e->getMessage());
  64. }
  65. }
  66. public function lpush($value)
  67. {
  68. try {
  69. return $this->_redis->lPush($this->_tb_prefix.rand(1,$this->_tb_num),$value);
  70. } catch(Exception $e) {
  71. throw_exception($e->getMessage());
  72. }
  73. }
  74. public function scan()
  75. {
  76. $list_key = [];
  77. for ($i = 1; $i <= $this->_tb_num; $i++) {
  78. $list_key[] = $this->_tb_prefix . $i;
  79. }
  80. return $list_key;
  81. }
  82. public function rpop($key, $time)
  83. {
  84. $result = $this->_redis->brPop($key, $time);
  85. if ($result) {
  86. return $result[1];
  87. } else {
  88. return null;
  89. }
  90. }
  91. public function lpop($key, $time)
  92. {
  93. $result = $this->_redis->blPop($key, $time);
  94. if ($result) {
  95. return $result[1];
  96. } else {
  97. return null;
  98. }
  99. }
  100. public function clear() {
  101. $this->_redis->flushAll();
  102. }
  103. }
  104. /**
  105. * 队列处理
  106. *
  107. *
  108. * @package
  109. */
  110. class IClient
  111. {
  112. private $mQueuedb;
  113. public function __construct($queueDb)
  114. {
  115. $this->mQueuedb = $queueDb;
  116. }
  117. public function push($key, $value)
  118. {
  119. return $this->mQueuedb->lpush(serialize([$key=>$value]));
  120. }
  121. }
  122. class IServer
  123. {
  124. private $_queuedb;
  125. public function __construct($queueDb) {
  126. $this->_queuedb = $queueDb;
  127. }
  128. public function connect() : bool
  129. {
  130. return $this->_queuedb->connect();
  131. }
  132. public function pop($key,$time)
  133. {
  134. $result = $this->_queuedb->rpop($key,$time);
  135. if($result != null) {
  136. return unserialize($result);
  137. } else {
  138. return false;
  139. }
  140. }
  141. public function lpop($key,$time)
  142. {
  143. $result = $this->_queuedb->lpop($key,$time);
  144. if($result != null) {
  145. return unserialize($result);
  146. } else {
  147. return false;
  148. }
  149. }
  150. public function scan() {
  151. return $this->_queuedb->scan();
  152. }
  153. public function stop()
  154. {
  155. $this->_queuedb->close();
  156. }
  157. }
  158. abstract class ILooper
  159. {
  160. private $_stop = false;
  161. private $mServer;
  162. const MAX_COROUTINE = 1000;
  163. protected function __construct($server)
  164. {
  165. $this->mServer = $server;
  166. }
  167. public function prepare()
  168. {
  169. if (ob_get_level()) ob_end_clean();
  170. pcntl_signal(SIGINT, [$this,'sig_handler']);
  171. pcntl_signal(SIGHUP, [$this,'sig_handler']);
  172. pcntl_signal(SIGQUIT, [$this,'sig_handler']);
  173. pcntl_signal(SIGTERM, [$this,'sig_handler']);
  174. }
  175. abstract protected function handle($msg);
  176. public function stop()
  177. {
  178. Log::record(__FUNCTION__,Log::DEBUG);
  179. $this->_stop = true;
  180. }
  181. public function run()
  182. {
  183. $queues = $this->mServer->scan();
  184. while (true)
  185. {
  186. try
  187. {
  188. if ($this->_stop) break;
  189. perfor_clear();
  190. if(defined('USE_COROUTINE') && USE_COROUTINE)
  191. {
  192. $res = \Swoole\Coroutine::stats();
  193. $num = $res['coroutine_num'];
  194. $mem = memory_get_usage();
  195. if($num < ILooper::MAX_COROUTINE)
  196. {
  197. if($this->mServer->connect() == false) {
  198. \Swoole\Coroutine::sleep(1);
  199. Log::record("Processor redis disconnect.",Log::ERR);
  200. continue;
  201. }
  202. $content = $this->mServer->pop($queues,1);
  203. if(empty($content)) continue;
  204. if($this->_stop)
  205. {
  206. foreach ($content as $key => $params) {
  207. DispatcherClient::instance()->push($key, $params);
  208. }
  209. }
  210. else
  211. {
  212. go(function ()use ($content,$num,$mem) {
  213. $start = microtime(true);
  214. Log::record("BeginGoFunction coroutin_num={$num} memory={$mem}",Log::DEBUG);
  215. $method = $this->handle($content);
  216. $use_time = microtime(true) - $start;
  217. $msg = sprintf("EndGoFunction coroutin_num={$num} memory={$mem} request_time=%.6f method={$method}",$use_time);
  218. Log::record($msg,Log::DEBUG);
  219. });
  220. }
  221. }
  222. else {
  223. \Swoole\Coroutine::sleep(0.1);
  224. }
  225. }
  226. else
  227. {
  228. if($this->mServer->connect() == false) {
  229. sleep(1);
  230. continue;
  231. }
  232. $content = $this->mServer->pop($queues,1);
  233. if(empty($content)) continue;
  234. if($this->_stop)
  235. {
  236. foreach ($content as $key => $params) {
  237. DispatcherClient::instance()->push($key, $params);
  238. }
  239. }
  240. else
  241. {
  242. perfor_clear();
  243. perfor_start();
  244. $this->handle($content);
  245. perfor_end('Handle Request');
  246. $info = perfor_log();
  247. Log::record("{$info} \r\n",Log::DEBUG);
  248. }
  249. }
  250. }
  251. catch (Exception $e)
  252. {
  253. $err = $e->getMessage();
  254. $code = $e->getCode();
  255. Log::record("QueueDB pop err: code={$code} err={$err}",Log::ERR);
  256. }
  257. }
  258. Log::record("ILooper Run quit.", Log::DEBUG);
  259. }
  260. private function sig_handler($signo)
  261. {
  262. Log::record("queue quit at sig_handler.",Log::DEBUG);
  263. switch($signo) {
  264. case SIGINT:
  265. case SIGHUP:
  266. case SIGQUIT:
  267. case SIGTERM:
  268. $this->_stop = true;
  269. break;
  270. default:
  271. break;
  272. }
  273. }
  274. }