Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
695a35c2
Commit
695a35c2
authored
Oct 29, 2018
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement grpc timeout
parent
a6ccc96a
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
94 additions
and
22 deletions
+94
-22
channel.cpp
src/brpc/channel.cpp
+7
-7
controller.cpp
src/brpc/controller.cpp
+6
-6
controller.h
src/brpc/controller.h
+6
-2
controller_private_accessor.h
src/brpc/details/controller_private_accessor.h
+4
-0
parallel_channel.cpp
src/brpc/parallel_channel.cpp
+3
-3
http_rpc_protocol.cpp
src/brpc/policy/http_rpc_protocol.cpp
+36
-1
http_rpc_protocol.h
src/brpc/policy/http_rpc_protocol.h
+1
-0
brpc_grpc_protocol_unittest.cpp
test/brpc_grpc_protocol_unittest.cpp
+30
-1
grpc.proto
test/grpc.proto
+1
-2
No files found.
src/brpc/channel.cpp
View file @
695a35c2
...
...
@@ -493,12 +493,12 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
// Setup timer for backup request. When it occurs, we'll setup a
// timer of timeout_ms before sending backup request.
// _abstime_
u
s is for truncating _connect_timeout_ms and resetting
// _abstime_
n
s is for truncating _connect_timeout_ms and resetting
// timer when EBACKUPREQUEST occurs.
if
(
cntl
->
timeout_ms
()
<
0
)
{
cntl
->
_abstime_
u
s
=
-
1
;
cntl
->
_abstime_
n
s
=
-
1
;
}
else
{
cntl
->
_abstime_
us
=
cntl
->
timeout_ms
()
*
1000L
+
start_send_real_us
;
cntl
->
_abstime_
ns
=
cntl
->
timeout_ms
()
*
1000000L
+
start_send_real_us
*
1000L
;
}
const
int
rc
=
bthread_timer_add
(
&
cntl
->
_timeout_id
,
...
...
@@ -512,18 +512,18 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
}
else
if
(
cntl
->
timeout_ms
()
>=
0
)
{
// Setup timer for RPC timetout
// _abstime_
u
s is for truncating _connect_timeout_ms
cntl
->
_abstime_
us
=
cntl
->
timeout_ms
()
*
1000L
+
start_send_real_us
;
// _abstime_
n
s is for truncating _connect_timeout_ms
cntl
->
_abstime_
ns
=
cntl
->
timeout_ms
()
*
1000000L
+
start_send_real_us
*
1000L
;
const
int
rc
=
bthread_timer_add
(
&
cntl
->
_timeout_id
,
butil
::
microseconds_to_timespec
(
cntl
->
_abstime_u
s
),
butil
::
nanoseconds_to_timespec
(
cntl
->
_abstime_n
s
),
HandleTimeout
,
(
void
*
)
correlation_id
.
value
);
if
(
BAIDU_UNLIKELY
(
rc
!=
0
))
{
cntl
->
SetFailed
(
rc
,
"Fail to add timer for timeout"
);
return
cntl
->
HandleSendFailed
();
}
}
else
{
cntl
->
_abstime_
u
s
=
-
1
;
cntl
->
_abstime_
n
s
=
-
1
;
}
cntl
->
IssueRPC
(
start_send_real_us
);
...
...
src/brpc/controller.cpp
View file @
695a35c2
...
...
@@ -222,7 +222,7 @@ void Controller::ResetPods() {
_timeout_ms
=
UNSET_MAGIC_NUM
;
_backup_request_ms
=
UNSET_MAGIC_NUM
;
_connect_timeout_ms
=
UNSET_MAGIC_NUM
;
_abstime_
u
s
=
-
1
;
_abstime_
n
s
=
-
1
;
_timeout_id
=
0
;
_begin_time_us
=
0
;
_end_time_us
=
0
;
...
...
@@ -568,7 +568,7 @@ void Controller::OnVersionedRPCReturned(const CompletionInfo& info,
if
(
timeout_ms
()
>=
0
)
{
rc
=
bthread_timer_add
(
&
_timeout_id
,
butil
::
microseconds_to_timespec
(
_abstime_u
s
),
butil
::
nanoseconds_to_timespec
(
_abstime_n
s
),
HandleTimeout
,
(
void
*
)
_correlation_id
.
value
);
}
if
(
rc
!=
0
)
{
...
...
@@ -1111,10 +1111,10 @@ void Controller::IssueRPC(int64_t start_realtime_us) {
timespec
connect_abstime
;
timespec
*
pabstime
=
NULL
;
if
(
_connect_timeout_ms
>
0
)
{
if
(
_abstime_
u
s
>=
0
)
{
connect_abstime
=
butil
::
micr
oseconds_to_timespec
(
std
::
min
(
_connect_timeout_ms
*
1000
L
+
start_realtime_us
,
_abstime_
u
s
));
if
(
_abstime_
n
s
>=
0
)
{
connect_abstime
=
butil
::
nan
oseconds_to_timespec
(
std
::
min
(
_connect_timeout_ms
*
1000
000L
+
start_realtime_us
*
1000L
,
_abstime_
n
s
));
}
else
{
connect_abstime
=
butil
::
microseconds_to_timespec
(
_connect_timeout_ms
*
1000L
+
start_realtime_us
);
...
...
src/brpc/controller.h
View file @
695a35c2
...
...
@@ -480,6 +480,10 @@ public:
// Get sock option. .e.g get vip info through ttm kernel module hook,
int
GetSockOption
(
int
level
,
int
optname
,
void
*
optval
,
socklen_t
*
optlen
);
// Get deadline of this RPC (since the Epoch in nanoseconds).
// -1 means no deadline.
int64_t
deadline_ns
()
const
{
return
_abstime_ns
;
}
private
:
struct
CompletionInfo
{
CallId
id
;
// call_id of the corresponding request
...
...
@@ -662,8 +666,8 @@ private:
int32_t
_timeout_ms
;
int32_t
_connect_timeout_ms
;
int32_t
_backup_request_ms
;
// Deadline of this RPC (since the Epoch in
micr
oseconds).
int64_t
_abstime_
u
s
;
// Deadline of this RPC (since the Epoch in
nan
oseconds).
int64_t
_abstime_
n
s
;
// Timer registered to trigger RPC timeout event
bthread_timer_t
_timeout_id
;
...
...
src/brpc/details/controller_private_accessor.h
View file @
695a35c2
...
...
@@ -128,6 +128,10 @@ public:
std
::
string
&
protocol_param
()
{
return
_cntl
->
protocol_param
();
}
const
std
::
string
&
protocol_param
()
const
{
return
_cntl
->
protocol_param
();
}
void
set_deadline_ns
(
int64_t
timeout_ns
)
{
_cntl
->
_abstime_ns
=
butil
::
gettimeofday_us
()
*
1000L
+
timeout_ns
;
}
private
:
Controller
*
_cntl
;
};
...
...
src/brpc/parallel_channel.cpp
View file @
695a35c2
...
...
@@ -658,18 +658,18 @@ void ParallelChannel::CallMethod(
cntl
->
set_timeout_ms
(
_options
.
timeout_ms
);
}
if
(
cntl
->
timeout_ms
()
>=
0
)
{
cntl
->
_abstime_
us
=
cntl
->
timeout_ms
()
*
1000L
+
cntl
->
_begin_time_us
;
cntl
->
_abstime_
ns
=
cntl
->
timeout_ms
()
*
1000000L
+
cntl
->
_begin_time_us
*
1000L
;
// Setup timer for RPC timetout
const
int
rc
=
bthread_timer_add
(
&
cntl
->
_timeout_id
,
butil
::
microseconds_to_timespec
(
cntl
->
_abstime_u
s
),
butil
::
nanoseconds_to_timespec
(
cntl
->
_abstime_n
s
),
HandleTimeout
,
(
void
*
)
cid
.
value
);
if
(
rc
!=
0
)
{
cntl
->
SetFailed
(
rc
,
"Fail to add timer"
);
goto
FAIL
;
}
}
else
{
cntl
->
_abstime_
u
s
=
-
1
;
cntl
->
_abstime_
n
s
=
-
1
;
}
d
->
SaveThreadInfoOfCallsite
();
CHECK_EQ
(
0
,
bthread_id_unlock
(
cid
));
...
...
src/brpc/policy/http_rpc_protocol.cpp
View file @
695a35c2
...
...
@@ -135,6 +135,7 @@ CommonStrings::CommonStrings()
,
GRPC_ACCEPT_ENCODING_VALUE
(
"identity,gzip"
)
,
GRPC_STATUS
(
"grpc-status"
)
,
GRPC_MESSAGE
(
"grpc-message"
)
,
GRPC_TIMEOUT
(
"grpc-timeout"
)
{}
static
CommonStrings
*
common
=
NULL
;
...
...
@@ -1190,6 +1191,31 @@ void EndRunningCallMethodInPool(
::
google
::
protobuf
::
Message
*
response
,
::
google
::
protobuf
::
Closure
*
done
);
static
int64_t
ConvertGrpcTimeoutToNS
(
int64_t
timeout_value
,
const
char
timeout_unit
)
{
switch
(
timeout_unit
)
{
case
'H'
:
timeout_value
*=
(
3600
*
1000000000L
);
break
;
case
'M'
:
timeout_value
*=
(
60
*
1000000000L
);
break
;
case
'S'
:
timeout_value
*=
1000000000L
;
break
;
case
'm'
:
timeout_value
*=
1000000L
;
break
;
case
'u'
:
timeout_value
*=
1000L
;
case
'n'
:
break
;
default:
return
-
1
;
}
return
timeout_value
;
}
void
ProcessHttpRequest
(
InputMessageBase
*
msg
)
{
const
int64_t
start_parse_us
=
butil
::
cpuwide_time_us
();
DestroyingPtr
<
HttpContext
>
imsg_guard
(
static_cast
<
HttpContext
*>
(
msg
));
...
...
@@ -1418,6 +1444,15 @@ void ProcessHttpRequest(InputMessageBase *msg) {
return
;
}
}
const
std
::
string
*
grpc_timeout
=
req_header
.
GetHeader
(
common
->
GRPC_TIMEOUT
);
if
(
grpc_timeout
)
{
const
char
timeout_unit
=
grpc_timeout
->
back
();
int64_t
timeout_value_ns
=
ConvertGrpcTimeoutToNS
((
int64_t
)
strtol
(
grpc_timeout
->
data
(),
NULL
,
10
),
timeout_unit
);
if
(
timeout_value_ns
>=
0
)
{
accessor
.
set_deadline_ns
(
timeout_value_ns
);
}
}
}
}
else
{
encoding
=
req_header
.
GetHeader
(
common
->
CONTENT_ENCODING
);
...
...
@@ -1455,7 +1490,7 @@ void ProcessHttpRequest(InputMessageBase *msg) {
// A http server, just keep content as it is.
cntl
->
request_attachment
().
swap
(
req_body
);
}
google
::
protobuf
::
Closure
*
done
=
new
HttpResponseSenderAsDone
(
&
resp_sender
);
imsg_guard
.
reset
();
// optional, just release resourse ASAP
...
...
src/brpc/policy/http_rpc_protocol.h
View file @
695a35c2
...
...
@@ -71,6 +71,7 @@ struct CommonStrings {
std
::
string
GRPC_ACCEPT_ENCODING_VALUE
;
std
::
string
GRPC_STATUS
;
std
::
string
GRPC_MESSAGE
;
std
::
string
GRPC_TIMEOUT
;
CommonStrings
();
};
...
...
test/brpc_grpc_protocol_unittest.cpp
View file @
695a35c2
...
...
@@ -20,6 +20,7 @@
#include "brpc/server.h"
#include "brpc/channel.h"
#include "brpc/grpc.h"
#include "butil/time.h"
#include "grpc.pb.h"
int
main
(
int
argc
,
char
*
argv
[])
{
...
...
@@ -64,6 +65,10 @@ public:
cntl
->
SetFailed
(
brpc
::
EINTERNAL
,
"%s"
,
g_prefix
.
c_str
());
return
;
}
if
(
req
->
has_timeout_ns
())
{
EXPECT_NEAR
(
cntl
->
deadline_ns
()
/
1000000000L
,
butil
::
gettimeofday_s
()
+
req
->
timeout_ns
()
/
1000000000L
,
1
);
}
}
void
MethodTimeOut
(
::
google
::
protobuf
::
RpcController
*
cntl_base
,
...
...
@@ -77,7 +82,6 @@ public:
}
};
class
GrpcTest
:
public
::
testing
::
Test
{
protected
:
GrpcTest
()
{
...
...
@@ -198,4 +202,29 @@ TEST_F(GrpcTest, MethodNotExist) {
ASSERT_TRUE
(
butil
::
StringPiece
(
cntl
.
ErrorText
()).
ends_with
(
"Method MethodNotExist() not implemented."
));
}
TEST_F
(
GrpcTest
,
GrpcTimeOut
)
{
const
char
*
timeouts
[]
=
{
"2H"
,
"7200000000000"
,
"3M"
,
"180000000000"
,
"+1S"
,
"1000000000"
,
"4m"
,
"4000000"
,
"5u"
,
"5000"
,
"6n"
,
"6"
};
for
(
size_t
i
=
0
;
i
<
arraysize
(
timeouts
);
i
=
i
+
2
)
{
test
::
GrpcRequest
req
;
test
::
GrpcResponse
res
;
brpc
::
Controller
cntl
;
req
.
set_message
(
g_req
);
req
.
set_gzip
(
false
);
req
.
set_return_error
(
false
);
req
.
set_timeout_ns
((
int64_t
)(
strtol
(
timeouts
[
i
+
1
],
NULL
,
10
)));
cntl
.
http_request
().
SetHeader
(
"grpc-timeout"
,
timeouts
[
i
]);
test
::
GrpcService_Stub
stub
(
&
_channel
);
stub
.
Method
(
&
cntl
,
&
req
,
&
res
,
NULL
);
EXPECT_FALSE
(
cntl
.
Failed
());
}
}
}
// namespace
test/grpc.proto
View file @
695a35c2
...
...
@@ -7,6 +7,7 @@ message GrpcRequest {
required
string
message
=
1
;
required
bool
gzip
=
2
;
required
bool
return_error
=
3
;
optional
int64
timeout_ns
=
4
;
};
message
GrpcResponse
{
...
...
@@ -18,5 +19,3 @@ service GrpcService {
rpc
MethodTimeOut
(
GrpcRequest
)
returns
(
GrpcResponse
);
rpc
MethodNotExist
(
GrpcRequest
)
returns
(
GrpcResponse
);
}
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