Commit 0f9d7878 authored by zhujiashun's avatar zhujiashun

support grpc client setting timeout in cntl or channel

parent 211915d2
......@@ -576,7 +576,10 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
*/
// TODO: do we need this?
hreq.SetHeader(common->TE, common->TRAILERS);
if (cntl->timeout_ms() >= 0) {
hreq.SetHeader(common->GRPC_TIMEOUT,
butil::string_printf("%ldm", cntl->timeout_ms()));
}
// Append compressed and length before body
AddGrpcPrefix(&cntl->request_attachment(), grpc_compressed);
}
......
......@@ -224,6 +224,7 @@ TEST_F(GrpcTest, GrpcTimeOut) {
"", "-1"
};
// test all timeout format
for (size_t i = 0; i < arraysize(timeouts); i = i + 2) {
test::GrpcRequest req;
test::GrpcResponse res;
......@@ -232,11 +233,41 @@ TEST_F(GrpcTest, GrpcTimeOut) {
req.set_gzip(false);
req.set_return_error(false);
req.set_timeout_us((int64_t)(strtol(timeouts[i+1], NULL, 10)));
cntl.set_timeout_ms(-1);
cntl.http_request().SetHeader("grpc-timeout", timeouts[i]);
test::GrpcService_Stub stub(&_channel);
stub.Method(&cntl, &req, &res, NULL);
EXPECT_FALSE(cntl.Failed());
}
// test timeout by using timeout_ms in cntl
{
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_us(9876000);
cntl.set_timeout_ms(9876);
test::GrpcService_Stub stub(&_channel);
stub.Method(&cntl, &req, &res, NULL);
EXPECT_FALSE(cntl.Failed());
}
// test timeout by using timeout_ms in channel
{
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_us(g_timeout_ms * 1000);
test::GrpcService_Stub stub(&_channel);
stub.Method(&cntl, &req, &res, NULL);
EXPECT_FALSE(cntl.Failed());
}
}
} // namespace
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