From 59f7b387d95ff1187f15bf6bf1639817ab4b07a0 Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Tue, 25 Jul 2023 09:46:35 +0800 Subject: [PATCH] reorder query region hosts (#416) --- src/Qiniu/Config.php | 45 +++++++++++++++++++++----------- src/Qiniu/Region.php | 2 +- tests/Qiniu/Tests/ConfigTest.php | 26 +++++++++++++++++- 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/src/Qiniu/Config.php b/src/Qiniu/Config.php index a4590be7..0bc20567 100644 --- a/src/Qiniu/Config.php +++ b/src/Qiniu/Config.php @@ -11,6 +11,7 @@ final class Config const API_HOST = 'api.qiniuapi.com'; const RS_HOST = 'rs.qiniuapi.com'; //RS Host const UC_HOST = 'uc.qbox.me'; //UC Host + const QUERY_REGION_HOST = 'kodo-config.qiniuapi.com'; const RTCAPI_HOST = 'http://rtc.qiniuapi.com'; const ARGUS_HOST = 'ai.qiniuapi.com'; const CASTER_HOST = 'pili-caster.qiniuapi.com'; @@ -32,8 +33,9 @@ final class Config private $regionCache; // UC Host private $ucHost; + private $queryRegionHost; // backup UC Hosts - private $backupUcHosts; + private $backupQueryRegionHosts; // backup UC Hosts max retry time public $backupUcHostsRetryTimes; @@ -45,17 +47,18 @@ public function __construct(Region $z = null) $this->useCdnDomains = false; $this->regionCache = array(); $this->ucHost = Config::UC_HOST; - $this->backupUcHosts = array( - "kodo-config.qiniuapi.com", + $this->queryRegionHost = Config::QUERY_REGION_HOST; + $this->backupQueryRegionHosts = array( + "uc.qbox.me", "api.qiniu.com" ); $this->backupUcHostsRetryTimes = 2; } - public function setUcHost($ucHost, $backupUcHosts = array()) + public function setUcHost($ucHost) { $this->ucHost = $ucHost; - $this->backupUcHosts = $backupUcHosts; + $this->setQueryRegionHost($ucHost); } public function getUcHost() @@ -69,19 +72,31 @@ public function getUcHost() return $scheme . $this->ucHost; } - public function appendBackupUcHosts($hosts) + public function setQueryRegionHost($host, $backupHosts = array()) { - $this->backupUcHosts = array_merge($this->backupUcHosts, $hosts); + $this->queryRegionHost = $host; + $this->backupQueryRegionHosts = $backupHosts; } - public function prependBackupUcHosts($hosts) + public function getQueryRegionHost() { - $this->backupUcHosts = array_merge($hosts, $this->backupUcHosts); + if ($this->useHTTPS === true) { + $scheme = "https://"; + } else { + $scheme = "http://"; + } + + return $scheme . $this->queryRegionHost; + } + + public function setBackupQueryRegionHosts($hosts = array()) + { + $this->backupQueryRegionHosts = $hosts; } - public function getBackupUcHosts() + public function getBackupQueryRegionHosts() { - return $this->backupUcHosts; + return $this->backupQueryRegionHosts; } public function getUpHost($accessKey, $bucket) @@ -336,8 +351,8 @@ private function getRegion($accessKey, $bucket) $region = Zone::queryZone( $accessKey, $bucket, - $this->getUcHost(), - $this->getBackupUcHosts(), + $this->getQueryRegionHost(), + $this->getBackupQueryRegionHosts(), $this->backupUcHostsRetryTimes ); if (is_array($region)) { @@ -366,8 +381,8 @@ private function getRegionV2($accessKey, $bucket) $region = Zone::queryZone( $accessKey, $bucket, - $this->getUcHost(), - $this->getBackupUcHosts(), + $this->getQueryRegionHost(), + $this->getBackupQueryRegionHosts(), $this->backupUcHostsRetryTimes ); if (is_array($region)) { diff --git a/src/Qiniu/Region.php b/src/Qiniu/Region.php index 46237dff..d5f9620a 100644 --- a/src/Qiniu/Region.php +++ b/src/Qiniu/Region.php @@ -163,7 +163,7 @@ public static function queryRegion($ak, $bucket, $ucHost = null, $backupUcHosts { $region = new Region(); if (!$ucHost) { - $ucHost = "https://" . Config::UC_HOST; + $ucHost = "https://" . Config::QUERY_REGION_HOST; } $url = $ucHost . '/v4/query' . "?ak=$ak&bucket=$bucket"; $reqOpt = new RequestOptions(); diff --git a/tests/Qiniu/Tests/ConfigTest.php b/tests/Qiniu/Tests/ConfigTest.php index 7ebc0aad..9910c9b5 100644 --- a/tests/Qiniu/Tests/ConfigTest.php +++ b/tests/Qiniu/Tests/ConfigTest.php @@ -77,10 +77,20 @@ public function testSetUcHost() $this->assertEquals("https://uc.example.com", $conf->getUcHost()); } + public function testGetRegionWithCustomDomain() + { + $conf = new Config(); + $conf->setQueryRegionHost( + "uc.qbox.me" + ); + list(, $err) = $conf->getRsHostV2($this->accessKey, $this->bucketName); + $this->assertNull($err); + } + public function testGetRegionWithBackupDomains() { $conf = new Config(); - $conf->setUcHost( + $conf->setQueryRegionHost( "fake-uc.phpsdk.qiniu.com", array( "unavailable-uc.phpsdk.qiniu.com", @@ -90,5 +100,19 @@ public function testGetRegionWithBackupDomains() list(, $err) = $conf->getRsHostV2($this->accessKey, $this->bucketName); $this->assertNull($err); } + + public function testGetRegionWithUcAndBackupDomains() + { + $conf = new Config(); + $conf->setUcHost("fake-uc.phpsdk.qiniu.com"); + $conf->setBackupQueryRegionHosts( + array( + "unavailable-uc.phpsdk.qiniu.com", + "uc.qbox.me" // real uc + ) + ); + list(, $err) = $conf->getRsHostV2($this->accessKey, $this->bucketName); + $this->assertNull($err); + } } }