Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
system
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
_site-res
system
Commits
c3824064
Commit
c3824064
authored
May 31, 2018
by
tufengqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
curl新增timeout自定义外层配置
parent
4826f72d
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
42 additions
and
25 deletions
+42
-25
TCurlClient.php
classes/fpf/thrift/TCurlClient.php
+10
-10
TGuzzleClient.php
classes/fpf/thrift/TGuzzleClient.php
+8
-6
ThriftAsyncServiceFactoryProxy.php
classes/fpf/thrift/ThriftAsyncServiceFactoryProxy.php
+10
-2
ThriftServiceFactoryProxy.php
classes/fpf/thrift/ThriftServiceFactoryProxy.php
+9
-5
thrift_service.php
config/thrift_service.php
+5
-2
No files found.
classes/fpf/thrift/TCurlClient.php
View file @
c3824064
...
@@ -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
);
...
...
classes/fpf/thrift/TGuzzleClient.php
View file @
c3824064
...
@@ -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
();
...
...
classes/fpf/thrift/ThriftAsyncServiceFactoryProxy.php
View file @
c3824064
...
@@ -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'
])
{
...
...
classes/fpf/thrift/ThriftServiceFactoryProxy.php
View file @
c3824064
...
@@ -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
]]);
...
...
config/thrift_service.php
View file @
c3824064
...
@@ -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'
]
=
[
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment