Skip to content

Commit

Permalink
reorder query region hosts (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 authored Jul 25, 2023
1 parent f367c41 commit 59f7b38
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
45 changes: 30 additions & 15 deletions src/Qiniu/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Qiniu/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 25 additions & 1 deletion tests/Qiniu/Tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
}
}
}

0 comments on commit 59f7b38

Please sign in to comment.