Commit c3824064 authored by tufengqi's avatar tufengqi

curl新增timeout自定义外层配置

parent 4826f72d
...@@ -92,6 +92,9 @@ class TCurlClient extends \Thrift\Transport\TTransport ...@@ -92,6 +92,9 @@ class TCurlClient extends \Thrift\Transport\TTransport
protected $uri_no_func; protected $uri_no_func;
protected $connect_timeout_ms;
protected $timeout_ms;
/** /**
* Make a new HTTP client. * Make a new HTTP client.
* *
...@@ -99,10 +102,10 @@ class TCurlClient extends \Thrift\Transport\TTransport ...@@ -99,10 +102,10 @@ class TCurlClient extends \Thrift\Transport\TTransport
* @param int $port * @param int $port
* @param string $uri_no_func * @param string $uri_no_func
*/ */
public function __construct($host, $port = 80, $uri_no_func = '', $scheme = 'http') public function __construct($host, $port = 80, $uri_no_func = '', $scheme = 'http', $config)
{ {
if ((TStringFuncFactory::create()->strlen($uri_no_func) > 0) && ($uri_no_func{ if ((TStringFuncFactory::create()->strlen($uri_no_func) > 0) && ($uri_no_func {
0} != '/')) { 0} != '/')) {
$uri_no_func = '/' . $uri_no_func; $uri_no_func = '/' . $uri_no_func;
} }
$this->scheme_ = $scheme; $this->scheme_ = $scheme;
...@@ -113,6 +116,8 @@ class TCurlClient extends \Thrift\Transport\TTransport ...@@ -113,6 +116,8 @@ class TCurlClient extends \Thrift\Transport\TTransport
$this->request_ = ''; $this->request_ = '';
$this->response_ = null; $this->response_ = null;
$this->timeout_ = null; $this->timeout_ = null;
$this->connect_timeout_ms = $config['connect_timeout'] ?? 10;
$this->timeout_ms = $config['timeout'] ?? 100;
$this->headers_ = array(); $this->headers_ = array();
} }
...@@ -222,18 +227,13 @@ class TCurlClient extends \Thrift\Transport\TTransport ...@@ -222,18 +227,13 @@ class TCurlClient extends \Thrift\Transport\TTransport
foreach (array_merge($defaultHeaders, $this->headers_) as $key => $value) { foreach (array_merge($defaultHeaders, $this->headers_) as $key => $value) {
$headers[] = "$key: $value"; $headers[] = "$key: $value";
} }
curl_setopt(self::$curlHandle, CURLOPT_HTTPHEADER, $headers); curl_setopt(self::$curlHandle, CURLOPT_HTTPHEADER, $headers);
curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT_MS, $this->timeout_ms);
if ($this->timeout_ > 0) { curl_setopt(self::$curlHandle, CURLOPT_CONNECTTIMEOUT_MS, $this->connect_timeout_ms);
curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $this->timeout_);
}
curl_setopt(self::$curlHandle, CURLOPT_POSTFIELDS, $this->request_); curl_setopt(self::$curlHandle, CURLOPT_POSTFIELDS, $this->request_);
$this->request_ = ''; $this->request_ = '';
curl_setopt(self::$curlHandle, CURLOPT_URL, $fullUrl); curl_setopt(self::$curlHandle, CURLOPT_URL, $fullUrl);
$this->response_ = curl_exec(self::$curlHandle); $this->response_ = curl_exec(self::$curlHandle);
// Connect failed? // Connect failed?
if (!$this->response_) { if (!$this->response_) {
curl_close(self::$curlHandle); curl_close(self::$curlHandle);
......
...@@ -93,7 +93,8 @@ class TGuzzleClient extends \Thrift\Transport\TTransport ...@@ -93,7 +93,8 @@ class TGuzzleClient extends \Thrift\Transport\TTransport
protected $uri_no_func; protected $uri_no_func;
static private $guzzle_client; static private $guzzle_client;
protected $connect_timeout_ms;
protected $timeout_ms;
/** /**
* Make a new HTTP client. * Make a new HTTP client.
* *
...@@ -101,10 +102,10 @@ class TGuzzleClient extends \Thrift\Transport\TTransport ...@@ -101,10 +102,10 @@ class TGuzzleClient extends \Thrift\Transport\TTransport
* @param int $port * @param int $port
* @param string $uri_no_func * @param string $uri_no_func
*/ */
public function __construct($host, $port = 80, $uri_no_func = '', $scheme = 'http') public function __construct($host, $port = 80, $uri_no_func = '', $scheme = 'http', $config)
{ {
if ((TStringFuncFactory::create()->strlen($uri_no_func) > 0) && ($uri_no_func{ if ((TStringFuncFactory::create()->strlen($uri_no_func) > 0) && ($uri_no_func {
0} != '/')) { 0} != '/')) {
$uri_no_func = '/' . $uri_no_func; $uri_no_func = '/' . $uri_no_func;
} }
$this->scheme_ = $scheme; $this->scheme_ = $scheme;
...@@ -115,6 +116,8 @@ class TGuzzleClient extends \Thrift\Transport\TTransport ...@@ -115,6 +116,8 @@ class TGuzzleClient extends \Thrift\Transport\TTransport
$this->request_ = ''; $this->request_ = '';
$this->response_ = null; $this->response_ = null;
$this->timeout_ = null; $this->timeout_ = null;
$this->connect_timeout_ms = $config['connect_timeout'] ?? 10;
$this->timeout_ms = $config['timeout'] ?? 100;
$this->response_body = ''; $this->response_body = '';
$this->headers_ = []; $this->headers_ = [];
if (!self::$guzzle_client) { if (!self::$guzzle_client) {
...@@ -205,7 +208,6 @@ class TGuzzleClient extends \Thrift\Transport\TTransport ...@@ -205,7 +208,6 @@ class TGuzzleClient extends \Thrift\Transport\TTransport
// God, PHP really has some esoteric ways of doing simple things. // God, PHP really has some esoteric ways of doing simple things.
$host = $this->host_ . ($this->port_ != 80 ? ':' . $this->port_ : ''); $host = $this->host_ . ($this->port_ != 80 ? ':' . $this->port_ : '');
$fullUrl = $this->scheme_ . "://" . $host . $this->uri_; $fullUrl = $this->scheme_ . "://" . $host . $this->uri_;
$defaultHeaders = [ $defaultHeaders = [
'Host' => $host, 'Host' => $host,
'User-Agent' => 'PHP/THttpClient', 'User-Agent' => 'PHP/THttpClient',
...@@ -225,7 +227,7 @@ class TGuzzleClient extends \Thrift\Transport\TTransport ...@@ -225,7 +227,7 @@ class TGuzzleClient extends \Thrift\Transport\TTransport
'allow_redirects' => [ 'allow_redirects' => [
'max' => 1, 'max' => 1,
], ],
'timeout' => number_format($this->timeout_ / 1000, 1) // s 'timeout' => number_format($this->timeout_ms / 1000, 1) // s
]; ];
$promise = self::$guzzle_client->sendAsync($request, $options)->then(function ($response) { $promise = self::$guzzle_client->sendAsync($request, $options)->then(function ($response) {
$this->response_ = $response->getBody()->getContents(); $this->response_ = $response->getBody()->getContents();
......
...@@ -17,6 +17,7 @@ class ThriftAsyncServiceFactoryProxy ...@@ -17,6 +17,7 @@ class ThriftAsyncServiceFactoryProxy
private $request_base_params = null; private $request_base_params = null;
private $tem_class_name = ''; private $tem_class_name = '';
private $class_name = ''; private $class_name = '';
private $service_real_config;
const BINARY = 0; const BINARY = 0;
const JSON = 1; const JSON = 1;
...@@ -61,6 +62,7 @@ class ThriftAsyncServiceFactoryProxy ...@@ -61,6 +62,7 @@ class ThriftAsyncServiceFactoryProxy
self::BINARY => 'application/thrift-binary', self::BINARY => 'application/thrift-binary',
self::JSON => 'application/thrift-json', self::JSON => 'application/thrift-json',
]; ];
$this->service_real_config = $service_real;
$this->request_base_params = [ $this->request_base_params = [
'host' => $service_real['host'], 'host' => $service_real['host'],
'port' => $service_real['port'], 'port' => $service_real['port'],
...@@ -87,7 +89,7 @@ class ThriftAsyncServiceFactoryProxy ...@@ -87,7 +89,7 @@ class ThriftAsyncServiceFactoryProxy
goto doEnd; goto doEnd;
} }
doEnd : doEnd :
if ($error) { if ($error) {
return ResponseApi::arrFail($error); return ResponseApi::arrFail($error);
} }
return ResponseApi::arrSuccess([$tem_arr[1], $matches[1]]); return ResponseApi::arrSuccess([$tem_arr[1], $matches[1]]);
...@@ -95,7 +97,13 @@ class ThriftAsyncServiceFactoryProxy ...@@ -95,7 +97,13 @@ class ThriftAsyncServiceFactoryProxy
public function __call($func_name, $arguments) public function __call($func_name, $arguments)
{ {
$socket = new TGuzzleClient($this->request_base_params['host'], $this->request_base_params['port'], $this->request_base_params['uri']); $socket = new TGuzzleClient(
$this->request_base_params['host'],
$this->request_base_params['port'],
$this->request_base_params['uri'],
'http',
$this->service_real_config
);
$socket->addHeaders(['Content-type' => $this->request_base_params['content_type_str']]); $socket->addHeaders(['Content-type' => $this->request_base_params['content_type_str']]);
$transport = new \fpf\thrift\TBufferedTransport($socket, 1024, 1024); $transport = new \fpf\thrift\TBufferedTransport($socket, 1024, 1024);
if (self::BINARY === $this->request_base_params['content_type']) { if (self::BINARY === $this->request_base_params['content_type']) {
......
...@@ -47,13 +47,17 @@ class ThriftServiceFactoryProxy ...@@ -47,13 +47,17 @@ class ThriftServiceFactoryProxy
self::BINARY => 'application/thrift-binary', self::BINARY => 'application/thrift-binary',
self::JSON => 'application/thrift-json', self::JSON => 'application/thrift-json',
]; ];
$this->socket = new TCurlClient($service_real['host'], $service_real['port'], '/' . $service_name_space_str . '/' . $service_str . '/'); $this->socket = new TCurlClient(
$service_real['host'],
$service_real['port'],
'/' . $service_name_space_str . '/' . $service_str . '/',
'http',
$service_real
);
$this->socket->addHeaders(['Content-type' => $content_type_mapping[$content_type]]); $this->socket->addHeaders(['Content-type' => $content_type_mapping[$content_type]]);
$this->transport = new \Thrift\Transport\TBufferedTransport($this->socket, 1024, 1024); $this->transport = new \Thrift\Transport\TBufferedTransport($this->socket, 1024, 1024);
if (self::BINARY === $content_type) { if (self::BINARY === $content_type) {
$strict_read = true; $protocol = new \Thrift\Protocol\TBinaryProtocol($this->transport, $strict_read = true, $strict_write = true);
$strict_write = true;
$protocol = new \Thrift\Protocol\TBinaryProtocol($this->transport, $strict_read, $strict_write);
} elseif (self::JSON === $content_type) { } elseif (self::JSON === $content_type) {
$protocol = new \Thrift\Protocol\TJSONProtocol($this->transport); $protocol = new \Thrift\Protocol\TJSONProtocol($this->transport);
} }
...@@ -77,7 +81,7 @@ class ThriftServiceFactoryProxy ...@@ -77,7 +81,7 @@ class ThriftServiceFactoryProxy
goto doEnd; goto doEnd;
} }
doEnd : doEnd :
if ($error) { if ($error) {
return ResponseApi::arrFail($error); return ResponseApi::arrFail($error);
} }
return ResponseApi::arrSuccess([$tem_arr[1], $matches[1]]); return ResponseApi::arrSuccess([$tem_arr[1], $matches[1]]);
......
...@@ -5,8 +5,11 @@ $service_domain_name = 'service.local.zhaobi.com'; ...@@ -5,8 +5,11 @@ $service_domain_name = 'service.local.zhaobi.com';
$config['service_default_protocol'] = 0; // 0:binary 1:json $config['service_default_protocol'] = 0; // 0:binary 1:json
$config['service_default_hosts'] = [ $config['service_default_hosts'] = [
[ [
'host' => $service_domain_name, 'port' => 80, 'timeout' => 10, 'connect_timeout' => 100, 'host' => $service_domain_name,
'request_timeout' => 200, 'protocol' => 0 'port' => 80,
'timeout' => 10, // ms
'connect_timeout' => 100, // ms
'protocol' => 0
], ],
]; ];
$config['service_default_failure_policy'] = [ $config['service_default_failure_policy'] = [
......
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