GRPC:客户端超时 [英] GRPC: Client side timeout

查看:233
本文介绍了GRPC:客户端超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使客户端可以超时.为此,我修改了async_greeter_server.cpp和async_greeter_client.cpp文件以测试该概念.

I am trying to make the client work with timeouts. For this I modified the async_greeter_server.cpp and async_greeter_client.cpp files to test the concept.

我在客户端(在客户端上下文上)设置了最后期限,如果超时,我要等到收到服务器的实际(延迟)响应为止.以下是更改(在Finish()调用之后).

I am setting a deadline on the client side (on the client context) and if there is a timeout I wait until I receive the actual (delayed)response from the server. Following is the change (after the Finish() call).

类似地,在服务器端,经过一些延迟后发送响应以在客户端产生超时.

Similarly on the server side send the response after some delay to produce a timeout at the client.

由于CallData cq_.Next()循环"GPR_ASSERT(ok)"中的断言,服务器端崩溃.

The server side crashes due to assert in the CallData cq_.Next() loop "GPR_ASSERT(ok)".

客户端将在添加的超时代码中永远等待实际的响应.

The Client keeps waiting for the actual response forever in the added timeout code.

您知道此程序有什么问题吗?

Any idea what's wrong with this program ?

greeter_async_client.cc

greeter_async_client.cc

std::string SayHello(const std::string& user) 
{
    // Data we are sending to the server.
    HelloRequest request;
    request.set_name(user);

    HelloReply reply;
    ClientContext context;
    CompletionQueue cq;
    Status status;

    std::chrono::system_clock::time_point deadline = std::chrono::system_clock::now() + std::chrono::seconds(10);
    context.set_deadline(deadline);

    std::unique_ptr<ClientAsyncResponseReader<HelloReply> > rpc(stub_->AsyncSayHello(&context, request, &cq));

    rpc->Finish(&reply, &status, (void*)1);
    void* got_tag;
    bool ok = false;

    GPR_ASSERT(cq.Next(&got_tag, &ok));
    GPR_ASSERT(got_tag == (void*)1);
    GPR_ASSERT(ok);

    // -------------- HANDLE TIMEOUT ------------------------------------------
    // If timeout error wait for the actual reply
    if (status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED)
    {
      std::cout << "Timeout exceeded .., waiting for the next event "<< status.error_message() << std::endl;

      GPR_ASSERT(cq.Next(&got_tag, &ok));
      GPR_ASSERT(got_tag == (void *) 1);
      GPR_ASSERT(ok);
    }
    // Act upon the status of the actual RPC.
    if (status.ok()) 
    {
      return reply.message();
    } 
    else 
    {
      return "RPC failed";
    }
}

greeter_async_server.cc

greeter_async_server.cc

void Proceed() 
{
  if (status_ == CREATE) 
  {
    status_ = PROCESS;
    service_->RequestSayHello(&ctx_, &request_, &responder_, cq_, cq_,this);
  } 
  else if (status_ == PROCESS) 
  {
    new CallData(service_, cq_);

    std::string prefix("Hello ");
    reply_.set_message(prefix + request_.name());

    std::cout << "Sleeping for 20 seconds .... \n";
    std::this_thread::sleep_for(std::chrono::seconds(20));
    std::cout << "Out of sleep ..... \n";

    status_ = FINISH;
    responder_.Finish(reply_, Status::OK, this);
    std::cout <<" Reply sent - Finish ....... \n";
  } 
  else 
  {
    GPR_ASSERT(status_ == FINISH);
    // Once in the FINISH state, deallocate ourselves (CallData).
    delete this;
  }
}

推荐答案

在您的客户端,当您遇到截止截止时间超出错误之后,就不要再尝试获取标签1了.标签只会从cq中出来一次

At your client side, after you get deadline_exceeded error, you should not try to get tag 1 again... A tag will only come out of the cq once.

这篇关于GRPC:客户端超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆