Skip to content

Commit 9d0e591

Browse files
authored
Merge pull request #387 from qiniu/features/upload-timeout
add timeout per request for upload
2 parents 57033fa + 0c672d8 commit 9d0e591

File tree

9 files changed

+251
-45
lines changed

9 files changed

+251
-45
lines changed

.github/workflows/test-ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ jobs:
2929
3030
- name: Run cases
3131
run: |
32+
nohup php -S localhost:9000 -t ./tests/mock-server/ > phpd.log 2>&1 &
33+
export PHP_SERVER_PID=$!
3234
./vendor/bin/phpcs --standard=PSR2 src
3335
./vendor/bin/phpcs --standard=PSR2 examples
3436
./vendor/bin/phpcs --standard=PSR2 tests
3537
./vendor/bin/phpunit --coverage-clover=coverage.xml
38+
kill $PHP_SERVER_PID
3639
3740
env:
3841
QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}

src/Qiniu/Http/Client.php

+55-13
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,79 @@
22
namespace Qiniu\Http;
33

44
use Qiniu\Config;
5-
use Qiniu\Http\Request;
6-
use Qiniu\Http\Response;
75

86
final class Client
97
{
10-
public static function get($url, array $headers = array())
8+
/**
9+
* @param $url
10+
* @param array $headers
11+
* @param RequestOptions $opt
12+
* @return Response
13+
*/
14+
public static function get($url, array $headers = array(), $opt = null)
1115
{
12-
$request = new Request('GET', $url, $headers);
16+
$request = new Request('GET', $url, $headers, null, $opt);
1317
return self::sendRequest($request);
1418
}
1519

16-
public static function delete($url, array $headers = array())
20+
/**
21+
* @param $url
22+
* @param array $headers
23+
* @param array $opt detail see {@see Request::$opt}
24+
* @return Response
25+
*/
26+
public static function delete($url, array $headers = array(), $opt = null)
1727
{
18-
$request = new Request('DELETE', $url, $headers);
28+
$request = new Request('DELETE', $url, $headers, null, $opt);
1929
return self::sendRequest($request);
2030
}
2131

22-
public static function post($url, $body, array $headers = array())
32+
/**
33+
* @param $url
34+
* @param $body
35+
* @param array $headers
36+
* @param RequestOptions $opt
37+
* @return Response
38+
*/
39+
public static function post($url, $body, array $headers = array(), $opt = null)
2340
{
24-
$request = new Request('POST', $url, $headers, $body);
41+
$request = new Request('POST', $url, $headers, $body, $opt);
2542
return self::sendRequest($request);
2643
}
2744

28-
public static function PUT($url, $body, array $headers = array())
45+
/**
46+
* @param $url
47+
* @param $body
48+
* @param array $headers
49+
* @param RequestOptions $opt
50+
* @return Response
51+
*/
52+
public static function PUT($url, $body, array $headers = array(), $opt = null)
2953
{
30-
$request = new Request('PUT', $url, $headers, $body);
54+
$request = new Request('PUT', $url, $headers, $body, $opt);
3155
return self::sendRequest($request);
3256
}
3357

58+
/**
59+
* @param $url
60+
* @param array $fields
61+
* @param string $name
62+
* @param string $fileName
63+
* @param $fileBody
64+
* @param null $mimeType
65+
* @param array $headers
66+
* @param RequestOptions $opt
67+
* @return Response
68+
*/
3469
public static function multipartPost(
3570
$url,
3671
$fields,
3772
$name,
3873
$fileName,
3974
$fileBody,
4075
$mimeType = null,
41-
array $headers = array()
76+
$headers = array(),
77+
$opt = null
4278
) {
4379
$data = array();
4480
$mimeBoundary = md5(microtime());
@@ -62,10 +98,9 @@ public static function multipartPost(
6298
array_push($data, '');
6399

64100
$body = implode("\r\n", $data);
65-
// var_dump($data);exit;
66101
$contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
67102
$headers['Content-Type'] = $contentType;
68-
$request = new Request('POST', $url, $headers, $body);
103+
$request = new Request('POST', $url, $headers, $body, $opt);
69104
return self::sendRequest($request);
70105
}
71106

@@ -84,6 +119,10 @@ private static function userAgent()
84119
return $ua;
85120
}
86121

122+
/**
123+
* @param Request $request
124+
* @return Response
125+
*/
87126
public static function sendRequest($request)
88127
{
89128
$t1 = microtime(true);
@@ -98,6 +137,9 @@ public static function sendRequest($request)
98137
CURLOPT_CUSTOMREQUEST => $request->method,
99138
CURLOPT_URL => $request->url,
100139
);
140+
foreach ($request->opt->getCurlOpt() as $k => $v) {
141+
$options[$k] = $v;
142+
}
101143
// Handle open_basedir & safe mode
102144
if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
103145
$options[CURLOPT_FOLLOWLOCATION] = true;

src/Qiniu/Http/Request.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ final class Request
77
public $headers;
88
public $body;
99
public $method;
10+
/**
11+
* @var RequestOptions
12+
*/
13+
public $opt;
1014

11-
public function __construct($method, $url, array $headers = array(), $body = null)
15+
public function __construct($method, $url, array $headers = array(), $body = null, $opt = null)
1216
{
1317
$this->method = strtoupper($method);
1418
$this->url = $url;
1519
$this->headers = $headers;
1620
$this->body = $body;
21+
if ($opt === null) {
22+
$opt = new RequestOptions();
23+
}
24+
$this->opt = $opt;
1725
}
1826
}

src/Qiniu/Http/RequestOptions.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Qiniu\Http;
4+
5+
final class RequestOptions
6+
{
7+
8+
/**
9+
* @var int|null
10+
* http 请求的超时时间,单位:秒,默认:0,不超时
11+
*/
12+
public $connection_timeout;
13+
14+
/**
15+
* @var int|null
16+
* http 请求的超时时间,单位:毫秒,默认:0,不超时
17+
*/
18+
public $connection_timeout_ms;
19+
20+
/**
21+
* @var int|null
22+
* http 请求的超时时间,单位:秒,默认:0,不超时
23+
*/
24+
public $timeout;
25+
26+
27+
/**
28+
* @var int|null
29+
* http 请求的超时时间,单位:毫秒,默认:0,不超时
30+
*/
31+
public $timeout_ms;
32+
33+
public function __construct(
34+
$connection_timeout = null,
35+
$connection_timeout_ms = null,
36+
$timeout = null,
37+
$timeout_ms = null
38+
) {
39+
$this->connection_timeout = $connection_timeout;
40+
$this->connection_timeout_ms = $connection_timeout_ms;
41+
$this->timeout = $timeout;
42+
$this->timeout_ms = $timeout_ms;
43+
}
44+
45+
public function getCurlOpt()
46+
{
47+
$result = array();
48+
if ($this->connection_timeout != null) {
49+
$result[CURLOPT_CONNECTTIMEOUT] = $this->connection_timeout;
50+
}
51+
if ($this->connection_timeout_ms != null) {
52+
$result[CURLOPT_CONNECTTIMEOUT_MS] = $this->connection_timeout_ms;
53+
}
54+
if ($this->timeout != null) {
55+
$result[CURLOPT_TIMEOUT] = $this->timeout;
56+
}
57+
if ($this->timeout_ms != null) {
58+
$result[CURLOPT_TIMEOUT_MS] = $this->timeout_ms;
59+
}
60+
return $result;
61+
}
62+
}

src/Qiniu/Storage/FormUploader.php

+25-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Qiniu\Config;
66
use Qiniu\Http\Error;
77
use Qiniu\Http\Client;
8+
use Qiniu\Http\RequestOptions;
89

910
final class FormUploader
1011
{
@@ -17,10 +18,10 @@ final class FormUploader
1718
* @param string $data 上传二进制流
1819
* @param Config $config 上传配置
1920
* @param string $params 自定义变量,规格参考
20-
* https://developer.qiniu.com/kodo/manual/1235/vars#xvar
21+
* {@link https://developer.qiniu.com/kodo/manual/1235/vars#xvar}
2122
* @param string $mime 上传数据的mimeType
22-
*
2323
* @param string $fname
24+
* @param RequestOptions $reqOpt
2425
*
2526
* @return array 包含已上传文件的信息,类似:
2627
* [
@@ -35,8 +36,12 @@ public static function put(
3536
$config,
3637
$params,
3738
$mime,
38-
$fname
39+
$fname,
40+
$reqOpt = null
3941
) {
42+
if ($reqOpt == null) {
43+
$reqOpt = new RequestOptions();
44+
}
4045
$fields = array('token' => $upToken);
4146
if ($key === null) {
4247
} else {
@@ -63,7 +68,17 @@ public static function put(
6368
return array(null, $err);
6469
}
6570

66-
$response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);
71+
72+
$response = Client::multipartPost(
73+
$upHost,
74+
$fields,
75+
'file',
76+
$fname,
77+
$data,
78+
$mime,
79+
array(),
80+
$reqOpt
81+
);
6782
if (!$response->ok()) {
6883
return array(null, new Error($upHost, $response));
6984
}
@@ -93,9 +108,12 @@ public static function putFile(
93108
$filePath,
94109
$config,
95110
$params,
96-
$mime
111+
$mime,
112+
$reqOpt = null
97113
) {
98-
114+
if ($reqOpt == null) {
115+
$reqOpt = new RequestOptions();
116+
}
99117

100118
$fields = array('token' => $upToken, 'file' => self::createFile($filePath, $mime));
101119
if ($key !== null) {
@@ -123,7 +141,7 @@ public static function putFile(
123141
return array(null, $err);
124142
}
125143

126-
$response = Client::post($upHost, $fields, $headers);
144+
$response = Client::post($upHost, $fields, $headers, $reqOpt);
127145
if (!$response->ok()) {
128146
return array(null, new Error($upHost, $response));
129147
}

0 commit comments

Comments
 (0)