123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- abstract class CoRefPool
- {
- private $mMaxClient;
- private $mFreeClients;
- private $mUsingClients;
- private $mSuspendCIDS;
- private $mStop;
- public function __construct($max_clients)
- {
- $this->mMaxClient = $max_clients;
- $this->mUsingClients = [];
- $this->mFreeClients = [];
- $this->mSuspendCIDS = [];
- $this->mStop = false;
- }
- public function get($cid)
- {
- if(array_key_exists($cid,$this->mUsingClients)) {
- $this->mUsingClients[$cid]['ref_count'] += 1;
- return $this->mUsingClients[$cid]['client'];
- }
- if(!empty($this->mFreeClients)) {
- $client = array_shift($this->mFreeClients);
- $this->mUsingClients[$cid] = ['client' => $client, 'ref_count' => 1,'ifTransacting' => false];
- }
- elseif(count($this->mUsingClients) < $this->mMaxClient) {
- $client = $this->create_client();
- $this->mUsingClients[$cid] = ['client' => $client, 'ref_count' => 1,'ifTransacting' => false];
- }
- else {
- $this->mSuspendCIDS[] = Co::getCid();
- Co::suspend();
- $client = array_shift($this->mFreeClients);
- $this->mUsingClients[$cid] = ['client' => $client, 'ref_count' => 1,'ifTransacting' => false];
- }
- return $client;
- }
- public function find($cid)
- {
- if(array_key_exists($cid,$this->mUsingClients)) {
- return $this->mUsingClients[$cid]['client'];
- } else {
- $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
- Log::record($msg,Log::DEBUG);
- throw new Exception($msg);
- }
- }
- public function reset($cid,$client)
- {
- if(array_key_exists($cid,$this->mUsingClients)) {
- return $this->mUsingClients[$cid]['client'] = $client;
- } else {
- $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
- Log::record($msg,Log::DEBUG);
- throw new Exception($msg);
- }
- }
- public function transaction($cid)
- {
- if(array_key_exists($cid,$this->mUsingClients)) {
- return $this->mUsingClients[$cid]['ifTransacting'];
- } else {
- $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
- Log::record($msg,Log::DEBUG);
- throw new Exception($msg);
- }
- }
- public function begin($cid)
- {
- if(array_key_exists($cid,$this->mUsingClients)) {
- $this->mUsingClients[$cid]['ifTransacting'] = true;
- } else {
- $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
- Log::record($msg,Log::DEBUG);
- throw new Exception($msg);
- }
- }
- public function commit($cid)
- {
- if(array_key_exists($cid,$this->mUsingClients)) {
- $this->mUsingClients[$cid]['ifTransacting'] = false;
- } else {
- $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
- Log::record($msg,Log::DEBUG);
- throw new Exception($msg);
- }
- }
- public function rollback($cid)
- {
- if(array_key_exists($cid,$this->mUsingClients)) {
- $this->mUsingClients[$cid]['ifTransacting'] = false;
- } else {
- $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
- Log::record($msg,Log::DEBUG);
- throw new Exception($msg);
- }
- }
- public function put($cid)
- {
- if(array_key_exists($cid,$this->mUsingClients))
- {
- $this->mUsingClients[$cid]['ref_count'] -= 1;
- $refcount = $this->mUsingClients[$cid]['ref_count'];
- if($refcount == 0)
- {
- $client = $this->mUsingClients[$cid]['client'];
- unset($this->mUsingClients[$cid]);
- $this->mFreeClients[] = $client;
- $suspend_cid = $this->getSuspend();
- if($suspend_cid > 0) {
- Co::resume($suspend_cid);
- }
- }
- }
- else {
- $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
- Log::record($msg,Log::DEBUG);
- throw new Exception($msg);
- }
- }
- 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();
- }
|