Skip to content

add timeout per request for upload #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ jobs:

- name: Run cases
run: |
nohup php -S localhost:9000 -t ./tests/mock-server/ > phpd.log 2>&1 &
export PHP_SERVER_PID=$!
./vendor/bin/phpcs --standard=PSR2 src
./vendor/bin/phpcs --standard=PSR2 examples
./vendor/bin/phpcs --standard=PSR2 tests
./vendor/bin/phpunit --coverage-clover=coverage.xml
kill $PHP_SERVER_PID

env:
QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}
Expand Down
68 changes: 55 additions & 13 deletions src/Qiniu/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,79 @@
namespace Qiniu\Http;

use Qiniu\Config;
use Qiniu\Http\Request;
use Qiniu\Http\Response;

final class Client
{
public static function get($url, array $headers = array())
/**
* @param $url
* @param array $headers
* @param RequestOptions $opt
* @return Response
*/
public static function get($url, array $headers = array(), $opt = null)
{
$request = new Request('GET', $url, $headers);
$request = new Request('GET', $url, $headers, null, $opt);
return self::sendRequest($request);
}

public static function delete($url, array $headers = array())
/**
* @param $url
* @param array $headers
* @param array $opt detail see {@see Request::$opt}
* @return Response
*/
public static function delete($url, array $headers = array(), $opt = null)
{
$request = new Request('DELETE', $url, $headers);
$request = new Request('DELETE', $url, $headers, null, $opt);
return self::sendRequest($request);
}

public static function post($url, $body, array $headers = array())
/**
* @param $url
* @param $body
* @param array $headers
* @param RequestOptions $opt
* @return Response
*/
public static function post($url, $body, array $headers = array(), $opt = null)
{
$request = new Request('POST', $url, $headers, $body);
$request = new Request('POST', $url, $headers, $body, $opt);
return self::sendRequest($request);
}

public static function PUT($url, $body, array $headers = array())
/**
* @param $url
* @param $body
* @param array $headers
* @param RequestOptions $opt
* @return Response
*/
public static function PUT($url, $body, array $headers = array(), $opt = null)
{
$request = new Request('PUT', $url, $headers, $body);
$request = new Request('PUT', $url, $headers, $body, $opt);
return self::sendRequest($request);
}

/**
* @param $url
* @param array $fields
* @param string $name
* @param string $fileName
* @param $fileBody
* @param null $mimeType
* @param array $headers
* @param RequestOptions $opt
* @return Response
*/
public static function multipartPost(
$url,
$fields,
$name,
$fileName,
$fileBody,
$mimeType = null,
array $headers = array()
$headers = array(),
$opt = null
) {
$data = array();
$mimeBoundary = md5(microtime());
Expand All @@ -62,10 +98,9 @@ public static function multipartPost(
array_push($data, '');

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

Expand All @@ -84,6 +119,10 @@ private static function userAgent()
return $ua;
}

/**
* @param Request $request
* @return Response
*/
public static function sendRequest($request)
{
$t1 = microtime(true);
Expand All @@ -98,6 +137,9 @@ public static function sendRequest($request)
CURLOPT_CUSTOMREQUEST => $request->method,
CURLOPT_URL => $request->url,
);
foreach ($request->opt->getCurlOpt() as $k => $v) {
$options[$k] = $v;
}
// Handle open_basedir & safe mode
if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
$options[CURLOPT_FOLLOWLOCATION] = true;
Expand Down
10 changes: 9 additions & 1 deletion src/Qiniu/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ final class Request
public $headers;
public $body;
public $method;
/**
* @var RequestOptions
*/
public $opt;

public function __construct($method, $url, array $headers = array(), $body = null)
public function __construct($method, $url, array $headers = array(), $body = null, $opt = null)
{
$this->method = strtoupper($method);
$this->url = $url;
$this->headers = $headers;
$this->body = $body;
if ($opt === null) {
$opt = new RequestOptions();
}
$this->opt = $opt;
}
}
62 changes: 62 additions & 0 deletions src/Qiniu/Http/RequestOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Qiniu\Http;

final class RequestOptions
{

/**
* @var int|null
* http 请求的超时时间,单位:秒,默认:0,不超时
*/
public $connection_timeout;

/**
* @var int|null
* http 请求的超时时间,单位:毫秒,默认:0,不超时
*/
public $connection_timeout_ms;

/**
* @var int|null
* http 请求的超时时间,单位:秒,默认:0,不超时
*/
public $timeout;


/**
* @var int|null
* http 请求的超时时间,单位:毫秒,默认:0,不超时
*/
public $timeout_ms;

public function __construct(
$connection_timeout = null,
$connection_timeout_ms = null,
$timeout = null,
$timeout_ms = null
) {
$this->connection_timeout = $connection_timeout;
$this->connection_timeout_ms = $connection_timeout_ms;
$this->timeout = $timeout;
$this->timeout_ms = $timeout_ms;
}

public function getCurlOpt()
{
$result = array();
if ($this->connection_timeout != null) {
$result[CURLOPT_CONNECTTIMEOUT] = $this->connection_timeout;
}
if ($this->connection_timeout_ms != null) {
$result[CURLOPT_CONNECTTIMEOUT_MS] = $this->connection_timeout_ms;
}
if ($this->timeout != null) {
$result[CURLOPT_TIMEOUT] = $this->timeout;
}
if ($this->timeout_ms != null) {
$result[CURLOPT_TIMEOUT_MS] = $this->timeout_ms;
}
return $result;
}
}
32 changes: 25 additions & 7 deletions src/Qiniu/Storage/FormUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Qiniu\Config;
use Qiniu\Http\Error;
use Qiniu\Http\Client;
use Qiniu\Http\RequestOptions;

final class FormUploader
{
Expand All @@ -17,10 +18,10 @@ final class FormUploader
* @param string $data 上传二进制流
* @param Config $config 上传配置
* @param string $params 自定义变量,规格参考
* https://developer.qiniu.com/kodo/manual/1235/vars#xvar
* {@link https://developer.qiniu.com/kodo/manual/1235/vars#xvar}
* @param string $mime 上传数据的mimeType
*
* @param string $fname
* @param RequestOptions $reqOpt
*
* @return array 包含已上传文件的信息,类似:
* [
Expand All @@ -35,8 +36,12 @@ public static function put(
$config,
$params,
$mime,
$fname
$fname,
$reqOpt = null
) {
if ($reqOpt == null) {
$reqOpt = new RequestOptions();
}
$fields = array('token' => $upToken);
if ($key === null) {
} else {
Expand All @@ -63,7 +68,17 @@ public static function put(
return array(null, $err);
}

$response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);

$response = Client::multipartPost(
$upHost,
$fields,
'file',
$fname,
$data,
$mime,
array(),
$reqOpt
);
if (!$response->ok()) {
return array(null, new Error($upHost, $response));
}
Expand Down Expand Up @@ -93,9 +108,12 @@ public static function putFile(
$filePath,
$config,
$params,
$mime
$mime,
$reqOpt = null
) {

if ($reqOpt == null) {
$reqOpt = new RequestOptions();
}

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

$response = Client::post($upHost, $fields, $headers);
$response = Client::post($upHost, $fields, $headers, $reqOpt);
if (!$response->ok()) {
return array(null, new Error($upHost, $response));
}
Expand Down
Loading