Skip to content

Commit 23a094d

Browse files
committed
unit test pass
1 parent 0891c67 commit 23a094d

File tree

5 files changed

+56
-18
lines changed

5 files changed

+56
-18
lines changed

src/Qiniu/Auth.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ public function uploadToken(
9393
if ($zone === null) {
9494
$zone = new Zone();
9595
}
96-
$args['upHosts'] = $zone->getUpHosts($this->accessKey, $bucket);
96+
97+
list($upHosts, $err) = $zone->getUpHosts($this->accessKey, $bucket);
98+
if ($err === null) {
99+
$args['upHosts'] = $upHosts;
100+
}
101+
97102
$b = json_encode($args);
98103
return $this->signWithData($b);
99104
}

src/Qiniu/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ final class Config
1818

1919
public function __construct(Zone $z = null) // 构造函数,默认为zone0
2020
{
21-
if ($z === null) {
21+
// if ($z === null) {
2222
$this->zone = new Zone();
23-
}
23+
// }
2424
}
2525
}

src/Qiniu/Storage/ResumeUploader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(
5454
$this->mime = $mime;
5555
$this->contexts = array();
5656
$this->config = $config;
57-
$this->host = $config->zone::getUpHostByToken($upToken);
57+
$this->host = $config->zone->getUpHostByToken($upToken);
5858
}
5959

6060
/**
@@ -76,7 +76,7 @@ public function upload()
7676
$ret = $response->json();
7777
}
7878
if ($response->statusCode < 0) {
79-
$this->host = $this->config->zone::getBackupUpHostByToken($this->upToken);
79+
$this->host = $this->config->zone->getBackupUpHostByToken($this->upToken);
8080
}
8181
if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
8282
$response = $this->makeBlock($data, $blockSize);

src/Qiniu/Storage/UploadManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function putFile(
109109
$checkCrc
110110
);
111111
}
112+
112113
$up = new ResumeUploader(
113114
$upToken,
114115
$key,

src/Qiniu/Zone.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ final class Zone
1010
public $upHost;
1111
public $upHostBackup;
1212

13-
public $hostCache; //<scheme>:<ak>:<bucket> ==> array('deadline' => 'xxx', 'upHosts' => array(), 'ioHost' => 'xxx.com')
13+
//array(
14+
// <scheme>:<ak>:<bucket> ==>
15+
// array('deadline' => 'xxx', 'upHosts' => array(), 'ioHost' => 'xxx.com')
16+
//)
17+
public $hostCache;
1418
public $scheme = 'http';
1519

1620
public function __construct($scheme = null)
@@ -37,8 +41,13 @@ public function getBackupUpHostByToken($uptoken)
3741

3842
public function getIoHost($ak, $bucket)
3943
{
40-
list($bucketHosts, ) = $this->getBucketHosts($ak, $bucket);
41-
return $bucketHosts['ioHost'][0];
44+
list($bucketHosts,) = $this->getBucketHosts($ak, $bucket);
45+
$ioHosts = $bucketHosts['ioHost'];
46+
if (count($ioHosts) === 0) {
47+
return "";
48+
}
49+
50+
return $ioHosts[0];
4251
}
4352

4453
public function getUpHosts($ak, $bucket)
@@ -73,15 +82,11 @@ private function unmarshalUpToken($uptoken)
7382

7483
public function getBucketHosts($ak, $bucket)
7584
{
76-
$key = $ak . $bucket;
77-
78-
$exist = false;
79-
if (count($this->hostCache) > 0) {
80-
$exist = array_key_exists($key, $this->hostCache) && $this->hostCache[$key]['deadline'] > time();
81-
}
85+
$key = $this->scheme . $ak . $bucket;
8286

83-
if ($exist) {
84-
return $this->hostCache[$key];
87+
$bucketHosts = $this->getBucketHostsFromCache($key);
88+
if (count($bucketHosts) > 0) {
89+
return array($bucketHosts, null);
8590
}
8691

8792
list($hosts, $err) = $this->bucketHosts($ak, $bucket);
@@ -90,12 +95,39 @@ public function getBucketHosts($ak, $bucket)
9095
}
9196

9297
$schemeHosts = $hosts[$this->scheme];
93-
$bucketHosts = array('upHosts' => $schemeHosts['up'], 'ioHost' => $schemeHosts['io'], 'deadline' => time() + $hosts['ttl']);
98+
$bucketHosts = array(
99+
'upHosts' => $schemeHosts['up'],
100+
'ioHost' => $schemeHosts['io'],
101+
'deadline' => time() + $hosts['ttl']
102+
);
94103

95-
$this->hostCache[$key] = $bucketHosts;
104+
$this->setBucketHostsToCache($key, $bucketHosts);
96105
return array($bucketHosts, null);
97106
}
98107

108+
private function getBucketHostsFromCache($key)
109+
{
110+
$ret = array();
111+
if (count($this->hostCache) === 0) {
112+
return $ret;
113+
}
114+
115+
if (!array_key_exists($key, $this->hostCache)) {
116+
return $ret;
117+
}
118+
119+
if ($this->hostCache[$key]['deadline'] > time()) {
120+
$ret = $this->hostCache[$key];
121+
}
122+
123+
return $ret;
124+
}
125+
126+
private function setBucketHostsToCache($key, $val)
127+
{
128+
$this->hostCache[$key] = $val;
129+
return;
130+
}
99131

100132
/* 请求包:
101133
* GET /v1/query?ak=<ak>&&bucket=<bucket>

0 commit comments

Comments
 (0)