mMaxClient = $max_clients; $this->mUsingCount = 0; $this->mFreeClients = []; $this->mSuspendCIDS = []; $this->mStop = false; } public function get($args) { if(!empty($this->mFreeClients)) { $client = array_shift($this->mFreeClients); } elseif($this->mUsingCount < $this->mMaxClient) { $client = $this->create_client($args); } else { $this->mSuspendCIDS[] = Co::getCid(); Co::suspend(); $client = array_shift($this->mFreeClients); } $this->mUsingCount++; return $client; } public function put($client) { $this->mFreeClients[] = $client; $this->mUsingCount--; $cid = $this->getSuspend(); if($cid > 0) { Co::resume($cid); } } private function getSuspend() { if(empty($this->mSuspendCIDS)) { return 0; } else { $cid = array_shift($this->mSuspendCIDS); return $cid; } } public function stoped() { return $this->mStop; } public function stop() { $this->mStop = true; } abstract public function create_client($args); }