123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- abstract class CoPool
- {
- private $mMaxClient;
- private $mFreeClients;
- private $mUsingCount;
- private $mSuspendCIDS;
- private $mStop;
- public function __construct($max_clients)
- {
- $this->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);
- }
|