ServerSwitch.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * 用于多个域名之间的切换逻辑
  4. * Created by PhpStorm.
  5. * User: zhangdali
  6. * Date: 2016/12/5
  7. * Time: 下午4:29
  8. */
  9. namespace App\Libs\Push\XMPush;
  10. class ServerSwitch {
  11. /**
  12. * 存储message的server
  13. * @var array Server
  14. */
  15. private $servers;
  16. private $apiRegionList;
  17. private $feedbackRegionList;
  18. private $sandbox;
  19. private $specified;
  20. private $emq;
  21. private $messageVip;
  22. private $defaultServer;
  23. private $inited = false;
  24. private $lastRefreshTime;
  25. static $REFRESH_SERVER_HOST_INTERVAL = 300000; // 5 * 60 * 1000
  26. /**
  27. * @var ServerSwitch reference to singleton instance
  28. */
  29. private static $instance;
  30. /**
  31. * 通过延迟加载(用到时才加载)获取实例
  32. *
  33. * @return self
  34. */
  35. public static function getInstance() {
  36. if (!isset(self::$instance)) {
  37. $class = __CLASS__;
  38. self::$instance = new $class;
  39. }
  40. return self::$instance;
  41. }
  42. /**
  43. * 是否需要刷新host列表
  44. * @return bool
  45. */
  46. public function needRefreshHostList() {
  47. return !$this->inited ||
  48. $this->currentTimeMillis() - $this->lastRefreshTime >= self::$REFRESH_SERVER_HOST_INTERVAL;
  49. }
  50. /**
  51. * @param String $serverListStr : host:min:max:step,host:min:max:step,...
  52. */
  53. public function initialize($serverListStr) {
  54. if (!$this->needRefreshHostList()) {
  55. return;
  56. }
  57. $serverStrArr = explode(',', $serverListStr);
  58. $servers = array();
  59. $i = 0;
  60. foreach ($serverStrArr as $serverStr) {
  61. $sp = explode(":", $serverStr);
  62. if (count($sp) < 5) {
  63. $servers[$i] = $this->defaultServer;
  64. continue;
  65. }
  66. $servers[$i] = new Server($sp[0], intval($sp[1]), intval($sp[2]), intval($sp[3]), intval($sp[4]));
  67. if (!empty($this->servers)) {
  68. foreach ($this->servers as $server) {
  69. if (strcmp($server->getHost(), $servers[$i]->getHost())) {
  70. $servers[$i]->setPriority($server->getPriority());
  71. }
  72. }
  73. }
  74. $i++;
  75. }
  76. $this->inited = true;
  77. $this->lastRefreshTime = $this->currentTimeMillis();
  78. $this->servers = $servers;
  79. }
  80. /**
  81. * @param PushRequestPath $requestPath
  82. */
  83. /**
  84. * @param PushRequestPath $requestPath
  85. * @return Server
  86. */
  87. public function &selectServer($requestPath, $region, $isVip) {
  88. if (isset(Constants::$host)) {
  89. $this->specified->setHost(Constants::$host);
  90. return $this->specified;
  91. }
  92. if (Constants::$sandbox) {
  93. return $this->sandbox;
  94. }
  95. switch ($requestPath->getRequestType()) {
  96. case PushRequestType::FEEDBACK:
  97. switch ($region) {
  98. case Region::Other:
  99. return $this->feedbackRegionList[$region];
  100. default:
  101. return $this->feedbackRegionList[Region::China];
  102. }
  103. case PushRequestType::EMQ:
  104. return $this->emq;
  105. default:
  106. switch ($region) {
  107. case Region::Other:
  108. return $this->apiRegionList[$region];
  109. default:
  110. if ($isVip) {
  111. return $this->messageVip;
  112. }
  113. return $this->selectMsgServer();
  114. }
  115. }
  116. }
  117. /**
  118. * @return mixed|Server
  119. */
  120. private function &selectMsgServer() {
  121. if (!Constants::$autoSwitchHost || !$this->inited) {
  122. return $this->defaultServer;
  123. }
  124. $allPriority = 0;
  125. $priorities = array();
  126. foreach ($this->servers as $server) {
  127. $priorities[] = $server->getPriority();
  128. $allPriority += $server->getPriority();
  129. }
  130. $randomPoint = mt_rand(0, $allPriority);
  131. $sum = 0;
  132. for ($i = 0; $i < count($priorities); $i++) {
  133. $sum += $priorities[$i];
  134. if ($randomPoint <= $sum) {
  135. return $this->servers[$i];
  136. }
  137. }
  138. return $this->defaultServer;
  139. }
  140. /**
  141. * 构造函数私有,不允许在外部实例化
  142. */
  143. private function __construct() {
  144. $this->sandbox = new Server(Constants::HOST_SANDBOX, 100, 100, 0, 0);
  145. $this->specified = new Server(Constants::$host, 100, 100, 0, 0);
  146. $this->emq = new Server(Constants::HOST_EMQ, 100, 100, 0, 0);
  147. $this->defaultServer = new Server(Constants::HOST_PRODUCTION, 1, 90, 10, 5);
  148. $this->lastRefreshTime = $this->currentTimeMillis();
  149. $hostList = Region::getFeedbackHostList();
  150. $this->feedbackRegionList = array();
  151. foreach ($hostList as $region => $host) {
  152. $this->feedbackRegionList[$region] = new Server($host, 100, 100, 0, 0);
  153. }
  154. $hostList = Region::getApiHostList();
  155. $this->apiRegionList = array();
  156. foreach ($hostList as $region => $host) {
  157. $this->apiRegionList[$region] = new Server($host, 100, 100, 0, 0);
  158. }
  159. $this->messageVip = new Server(Constants::HOST_VIP, 100, 100, 0, 0);
  160. }
  161. /**
  162. * 防止对象实例被克隆
  163. *
  164. * @return void
  165. */
  166. private function __clone() {
  167. }
  168. /**
  169. * 防止被反序列化
  170. *
  171. * @return void
  172. */
  173. private function __wakeup() {
  174. }
  175. /**
  176. * 获取当前时间(毫秒)
  177. * @return int
  178. */
  179. private function currentTimeMillis() {
  180. return ceil(microtime(true) * 1000);
  181. }
  182. }