説明なし

ServerSwitch.php 4.6KB

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