Commit c3824064 authored by tufengqi's avatar tufengqi

curl新增timeout自定义外层配置

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