config->get('coroutine.timeout', 30000); } if ($timeout > 0) { $this->timeout = $timeout; } else { $this->timeout = self::$maxTimeout; } $this->result = CNull::getInstance(); $this->requestTime = microtime(true); } /** * 获取协程执行结果 * * @return mixed|null */ public function getResult() { if ($this->isTimeout() && !$this->ioBack) { return null; } return $this->result; } /** * 协程超时异常 * * @throws Exception */ public function throwTimeOutException() { throw new Exception("[coroutine]: Time Out, [class]: " . get_class($this) . ", [Request]: $this->request"); } /** * 判断协程是否超时 * * @return bool */ public function isTimeout() { if (!$this->ioBack && (1000 * (microtime(true) - $this->requestTime) > $this->timeout)) { return true; } return false; } /** * 通知调度器进行下一次迭代 * * @return bool */ public function nextRun() { if (empty(getInstance()->scheduler->IOCallBack[$this->requestId])) { return true; } foreach (getInstance()->scheduler->IOCallBack[$this->requestId] as $k => $coroutine) { if ($coroutine->ioBack && !empty(getInstance()->scheduler->taskMap[$this->requestId])) { unset(getInstance()->scheduler->IOCallBack[$this->requestId][$k]); getInstance()->scheduler->schedule(getInstance()->scheduler->taskMap[$this->requestId]); } else { break; } } return true; } /** * 销毁 */ public function destroy() { $this->ioBack = false; $this->ioBackKey = null; $this->isBreak = false; $this->requestId = null; } /** * 属性不用于序列化 * * @return array */ public function __unsleep() { return ['context']; } /** * 发送异步请求后不需要执行回调 * * @return bool */ public function breakx() { if ($this->requestId && $this->ioBackKey !== null) { unset(getInstance()->scheduler->IOCallBack[$this->requestId][$this->ioBackKey]); $this->isBreak = true; } return true; } /** * 手工设置超时时间 * * @param int $timeout 超时时间,单位毫秒 * @return $this */ public function setTimeout($timeout) { $this->timeout = $timeout; return $this; } /** * 发送异步请求 * * @param $callback */ abstract public function send($callback); }