CoRefPool.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. abstract class CoRefPool
  3. {
  4. private $mMaxClient;
  5. private $mFreeClients;
  6. private $mUsingClients;
  7. private $mSuspendCIDS;
  8. private $mStop;
  9. public function __construct($max_clients)
  10. {
  11. $this->mMaxClient = $max_clients;
  12. $this->mUsingClients = [];
  13. $this->mFreeClients = [];
  14. $this->mSuspendCIDS = [];
  15. $this->mStop = false;
  16. }
  17. public function get($cid)
  18. {
  19. if(array_key_exists($cid,$this->mUsingClients)) {
  20. $this->mUsingClients[$cid]['ref_count'] += 1;
  21. return $this->mUsingClients[$cid]['client'];
  22. }
  23. if(!empty($this->mFreeClients)) {
  24. $client = array_shift($this->mFreeClients);
  25. $this->mUsingClients[$cid] = ['client' => $client, 'ref_count' => 1,'ifTransacting' => false];
  26. }
  27. elseif(count($this->mUsingClients) < $this->mMaxClient) {
  28. $client = $this->create_client();
  29. $this->mUsingClients[$cid] = ['client' => $client, 'ref_count' => 1,'ifTransacting' => false];
  30. }
  31. else {
  32. $this->mSuspendCIDS[] = Co::getCid();
  33. Co::suspend();
  34. $client = array_shift($this->mFreeClients);
  35. $this->mUsingClients[$cid] = ['client' => $client, 'ref_count' => 1,'ifTransacting' => false];
  36. }
  37. return $client;
  38. }
  39. public function find($cid)
  40. {
  41. if(array_key_exists($cid,$this->mUsingClients)) {
  42. return $this->mUsingClients[$cid]['client'];
  43. } else {
  44. $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
  45. Log::record($msg,Log::DEBUG);
  46. throw new Exception($msg);
  47. }
  48. }
  49. public function reset($cid,$client)
  50. {
  51. if(array_key_exists($cid,$this->mUsingClients)) {
  52. return $this->mUsingClients[$cid]['client'] = $client;
  53. } else {
  54. $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
  55. Log::record($msg,Log::DEBUG);
  56. throw new Exception($msg);
  57. }
  58. }
  59. public function transaction($cid)
  60. {
  61. if(array_key_exists($cid,$this->mUsingClients)) {
  62. return $this->mUsingClients[$cid]['ifTransacting'];
  63. } else {
  64. $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
  65. Log::record($msg,Log::DEBUG);
  66. throw new Exception($msg);
  67. }
  68. }
  69. public function begin($cid)
  70. {
  71. if(array_key_exists($cid,$this->mUsingClients)) {
  72. $this->mUsingClients[$cid]['ifTransacting'] = true;
  73. } else {
  74. $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
  75. Log::record($msg,Log::DEBUG);
  76. throw new Exception($msg);
  77. }
  78. }
  79. public function commit($cid)
  80. {
  81. if(array_key_exists($cid,$this->mUsingClients)) {
  82. $this->mUsingClients[$cid]['ifTransacting'] = false;
  83. } else {
  84. $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
  85. Log::record($msg,Log::DEBUG);
  86. throw new Exception($msg);
  87. }
  88. }
  89. public function rollback($cid)
  90. {
  91. if(array_key_exists($cid,$this->mUsingClients)) {
  92. $this->mUsingClients[$cid]['ifTransacting'] = false;
  93. } else {
  94. $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
  95. Log::record($msg,Log::DEBUG);
  96. throw new Exception($msg);
  97. }
  98. }
  99. public function put($cid)
  100. {
  101. if(array_key_exists($cid,$this->mUsingClients))
  102. {
  103. $this->mUsingClients[$cid]['ref_count'] -= 1;
  104. $refcount = $this->mUsingClients[$cid]['ref_count'];
  105. if($refcount == 0)
  106. {
  107. $client = $this->mUsingClients[$cid]['client'];
  108. unset($this->mUsingClients[$cid]);
  109. $this->mFreeClients[] = $client;
  110. $suspend_cid = $this->getSuspend();
  111. if($suspend_cid > 0) {
  112. Co::resume($suspend_cid);
  113. }
  114. }
  115. }
  116. else {
  117. $msg = __METHOD__ . " mysqli cannot find mysqli cid={$cid} client";
  118. Log::record($msg,Log::DEBUG);
  119. throw new Exception($msg);
  120. }
  121. }
  122. private function getSuspend()
  123. {
  124. if (empty($this->mSuspendCIDS)) {
  125. return 0;
  126. } else {
  127. $cid = array_shift($this->mSuspendCIDS);
  128. return $cid;
  129. }
  130. }
  131. public function stoped() {
  132. return $this->mStop;
  133. }
  134. public function stop() {
  135. $this->mStop = true;
  136. }
  137. abstract public function create_client();
  138. }