std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel("127.0.0.1:55551", grpc::InsecureCredentials());
auto controllerClient_ = qq::message::Controller::NewStub(channel);
grpc::ClientContext context;
qq::message::ControllerPrivateMsg msg;
msg.set_fromqq(123456);
msg.set_msg("helloworld");
qq::message::Gvoid gvoid;
grpc::Status st = controllerClient_->privateMsg(&context, msg, &gvoid);
LOGD("status " << st.ok());
but when the server is not running, the function privateMsg will block forever. (⊙o⊙)…
how can I set the timeout
thinkyou~
ps: the java client do not block,it will throw an exception.
You can use ClientContext::set_deadline to set an absolute deadline for the
rpc. end2end_test.cc has example usage.
On Sat, Oct 24, 2015 at 11:06 AM, liujingjing5 notifications@github.com
wrote:
I use the synchronous api, like this
std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel("127.0.0.1:55551", grpc::InsecureCredentials());
auto controllerClient_ = qq::message::Controller::NewStub(channel);
grpc::ClientContext context;
qq::message::ControllerPrivateMsg msg;
msg.set_fromqq(123456);
msg.set_msg("helloworld");
qq::message::Gvoid gvoid;
grpc::Status st = controllerClient_->privateMsg(&context, msg, &gvoid);
LOGD("status " << st.ok());
but when the server is not running, the function privateMsg will block
forever. (⊙o⊙)…
how can I set the timeout
thinkyou~
ps: the java client do not block,it will throw an exception.
Reply to this email directly or view it on GitHub
#3954.
I could offer full example:
// Connection timeout in seconds
unsigned int client_connection_timeout = 5;
ClientContext context;
// Set timeout for API
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::seconds(client_connection_timeout);
context.set_deadline(deadline);
I have this code but the client automatically closes. Can you please help?
ClientContext context;
// Connection timeout in seconds
unsigned int client_connection_timeout = 5000;
// Set timeout for API
std::chrono::system_clock::time_point deadline = std::chrono::system_clock::now() + std::chrono::seconds(client_connection_timeout);
context.set_deadline(deadline);
std::unique_ptr reader(stub_->STap(&context, tap_request));
while (reader->Read(&tap_reply)) {
std::cout<<"Response got from server: " << tap_reply.message() << " " << std::endl;
status = reader->Finish();
// Act upon its status.
if (status.ok()) {
std::cout << "rpc succeeded" << std::endl;
} else {
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
std::cout << "RPC failed" << std::endl;