Commit bd210511 authored by shajiaiming's avatar shajiaiming

调整请求时间

parent 176cbc54
......@@ -103,11 +103,24 @@ class Curl
*
* @return mixed response
*/
protected $connect_timeout_ms;
protected $timeout_ms;
public function __construct($config = [])
{
if (!empty($config)) {
$this->connect_timeout_ms = isset($config['connect_timeout']) ? $config['connect_timeout'] : 10;
$this->timeout_ms = isset($config['timeout_ms']) ? $config['timeout_ms'] : 100;
}
}
public function get($url, $raw = true)
{
$this->_baseUrl = $url;
return $this->_httpRequest('GET', $raw);
}
/**
* Start performing HEAD-HTTP-Request
*
......@@ -120,6 +133,7 @@ class Curl
$this->_baseUrl = $url;
return $this->_httpRequest('HEAD');
}
/**
* Start performing POST-HTTP-Request
*
......@@ -133,6 +147,7 @@ class Curl
$this->_baseUrl = $url;
return $this->_httpRequest('POST', $raw);
}
/**
* Start performing PUT-HTTP-Request
*
......@@ -146,6 +161,7 @@ class Curl
$this->_baseUrl = $url;
return $this->_httpRequest('PUT', $raw);
}
/**
* Start performing PATCH-HTTP-Request
*
......@@ -160,8 +176,9 @@ class Curl
$this->setHeaders([
'X-HTTP-Method-Override' => 'PATCH'
]);
return $this->_httpRequest('POST',$raw);
return $this->_httpRequest('POST', $raw);
}
/**
* Start performing DELETE-HTTP-Request
*
......@@ -175,6 +192,7 @@ class Curl
$this->_baseUrl = $url;
return $this->_httpRequest('DELETE', $raw);
}
/**
* Set curl option
*
......@@ -194,6 +212,7 @@ class Curl
//return self
return $this;
}
/**
* Set get params
*
......@@ -210,6 +229,7 @@ class Curl
//return self
return $this;
}
/**
* Set get params
*
......@@ -227,6 +247,7 @@ class Curl
//return self
return $this;
}
/**
* Set raw post data allows you to post any data format.
*
......@@ -242,6 +263,7 @@ class Curl
//return self
return $this;
}
/**
* Set get params
*
......@@ -259,6 +281,7 @@ class Curl
//return self
return $this;
}
/**
* Get URL - return URL parsed with given params
*
......@@ -267,11 +290,12 @@ class Curl
public function getUrl()
{
if (Count($this->_getParams) > 0) {
return $this->_baseUrl.'?'.http_build_query($this->_getParams);
return $this->_baseUrl . '?' . http_build_query($this->_getParams);
} else {
return $this->_baseUrl;
}
}
/**
* Set curl options
*
......@@ -284,6 +308,7 @@ class Curl
$this->_options = $options + $this->_options;
return $this;
}
/**
* Set multiple headers for request.
*
......@@ -298,11 +323,11 @@ class Curl
$parsedHeader = [];
//collect currently set headers
foreach ($this->getRequestHeaders() as $header => $value) {
array_push($parsedHeader, $header.':'.$value);
array_push($parsedHeader, $header . ':' . $value);
}
//parse header into right format key:value
foreach ($headers as $header => $value) {
array_push($parsedHeader, $header.':'.$value);
array_push($parsedHeader, $header . ':' . $value);
}
//set headers
$this->setOption(
......@@ -312,6 +337,7 @@ class Curl
}
return $this;
}
/**
* Set a single header for request.
*
......@@ -326,11 +352,11 @@ class Curl
$parsedHeader = [];
//collect currently set headers
foreach ($this->getRequestHeaders() as $headerToSet => $valueToSet) {
array_push($parsedHeader, $headerToSet.':'.$valueToSet);
array_push($parsedHeader, $headerToSet . ':' . $valueToSet);
}
//add override new header
if (strlen($header) > 0) {
array_push($parsedHeader, $header.':'.$value);
array_push($parsedHeader, $header . ':' . $value);
}
//set headers
$this->setOption(
......@@ -339,6 +365,7 @@ class Curl
);
return $this;
}
/**
* Unset a single header.
*
......@@ -353,7 +380,7 @@ class Curl
//collect currently set headers and filter "unset" header param.
foreach ($this->getRequestHeaders() as $headerToSet => $valueToSet) {
if ($header !== $headerToSet) {
array_push($parsedHeader, $headerToSet.':'.$valueToSet);
array_push($parsedHeader, $headerToSet . ':' . $valueToSet);
}
}
//set headers
......@@ -363,6 +390,7 @@ class Curl
);
return $this;
}
/**
* Get all request headers as key:value array
*
......@@ -381,6 +409,7 @@ class Curl
}
return $parsedRequestHeaders;
}
/**
* Get specific request header as key:value array
*
......@@ -394,6 +423,7 @@ class Curl
$parsedRequestHeaders = $this->getRequestHeaders();
return isset($parsedRequestHeaders[$headerKey]) ? $parsedRequestHeaders[$headerKey] : null;
}
/**
* Unset a single curl option
*
......@@ -409,6 +439,7 @@ class Curl
}
return $this;
}
/**
* Unset all curl option, excluding default options.
*
......@@ -422,6 +453,7 @@ class Curl
}
return $this;
}
/**
* Total reset of options, responses, etc.
*
......@@ -449,6 +481,7 @@ class Curl
$this->_getParams = [];
return $this;
}
/**
* Return a single option
*
......@@ -462,6 +495,7 @@ class Curl
//return value or false if key is not set.
return isset($mergesOptions[$key]) ? $mergesOptions[$key] : false;
}
/**
* Return merged curl options and keep keys!
*
......@@ -471,6 +505,7 @@ class Curl
{
return $this->_options + $this->_defaultOptions;
}
/**
* Get curl info according to http://php.net/manual/de/function.curl-getinfo.php
*
......@@ -487,6 +522,7 @@ class Curl
return [];
}
}
/**
* Performs HTTP request
*
......@@ -508,8 +544,8 @@ class Curl
}
//setup error reporting and profiling
if (YII_DEBUG) {
Yii::trace('Start sending cURL-Request: '.$this->getUrl().'\n', __METHOD__);
Yii::beginProfile($method.' '.$this->_baseUrl.'#'.md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
Yii::trace('Start sending cURL-Request: ' . $this->getUrl() . '\n', __METHOD__);
Yii::beginProfile($method . ' ' . $this->_baseUrl . '#' . md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
}
/**
* proceed curl
......@@ -517,6 +553,11 @@ class Curl
$curlOptions = $this->getOptions();
$this->curl = curl_init($this->getUrl());
curl_setopt_array($this->curl, $curlOptions);
if (true == $this->timeout_ms && true == $this->connect_timeout_ms) {
curl_setopt($this->curl, CURLOPT_TIMEOUT_MS, $this->timeout_ms);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout_ms);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT_MS, $this->connect_timeout_ms);
}
//curl_setopt($this->curl, CURLOPT_TIMEOUT,40);
$response = curl_exec($this->curl);
//check if curl was successful
......@@ -547,7 +588,7 @@ class Curl
$this->_extractAdditionalCurlParameter();
//end yii debug profile
if (YII_DEBUG) {
Yii::endProfile($method.' '.$this->getUrl().'#'.md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
Yii::endProfile($method . ' ' . $this->getUrl() . '#' . md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
}
//check responseCode and return data/status
if ($this->getOption(CURLOPT_CUSTOMREQUEST) === 'HEAD') {
......@@ -557,10 +598,11 @@ class Curl
return $this->response;
}
}
/**
* Extract additional curl params protected class helper
*/
protected function _extractAdditionalCurlParameter ()
protected function _extractAdditionalCurlParameter()
{
/**
* retrieve response code
......@@ -581,27 +623,29 @@ class Curl
* try extract response length
*/
$this->responseLength = curl_getinfo($this->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
if((int)$this->responseLength == -1) {
if ((int)$this->responseLength == -1) {
$this->responseLength = strlen($this->response);
}
}
/**
* Extract body curl data from response
*
* @param string $response
* @return string
*/
protected function _extractCurlBody ($response)
protected function _extractCurlBody($response)
{
return substr($response, $this->getInfo(CURLINFO_HEADER_SIZE));
}
/**
* Extract header curl data from response
*
* @param string $response
* @return array
*/
protected function _extractCurlHeaders ($response)
protected function _extractCurlHeaders($response)
{
//Init
$headers = [];
......
......@@ -20,7 +20,7 @@ class Chain33Service
public function __construct($parameter = [])
{
if(empty($parameter)){
if (empty($parameter)) {
$this->node_params = Yii::$app->params['chain33'];
} else {
$this->node_params = $parameter;
......@@ -56,7 +56,11 @@ class Chain33Service
public function send($params = [], $method = 'Chain33.Query')
{
$ch = new Curl();
$timeout = [
'connect_timeout' => 50000,
'timeout_ms' => 40000
];
$ch = new Curl($timeout);
$jsonrpc = self::jsonRpcBuild($params, $method);
$ch->setHeader('Content-Type', 'application/json');
$ch->setRawPostData($jsonrpc);
......@@ -203,7 +207,7 @@ class Chain33Service
return $this->send($params, 'Chain33.CreateRawTransaction');
}
public function createRawTransaction($to, $amount, $fee, $note,$execer)
public function createRawTransaction($to, $amount, $fee, $note, $execer)
{
$params = [
"to" => $to,
......
......@@ -51,7 +51,6 @@ class CrossChainController extends Controller
'scheme' => $transfer_url[0],
'host' => str_replace('//', '', $transfer_url[1]),
'port' => isset($transfer_url[2]) ? $transfer_url[2] : '',
'timeout' => 100
];
$service = new Chain33Service($node_params);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment