【问题标题】:Grpc Client-side C++ Channel Destructor taking 10 secondsGrpc Client-side C++ Channel Destructor 耗时 10 秒
【发布时间】:2017-05-29 18:45:17
【问题描述】:

代码的目的最初是在 grpc 服务不可用时测试 grpc 存根的操作。但是,我看到的行为表明发生了一些我不明白的事情——因此提出了这个问题。

在这段代码中:

#define IN_MILLISECONDS(x) (std::chrono::system_clock::now() + std::chrono::milliseconds(x))

string NowString()
{
    char buf[128];
    SYSTEMTIME timeBuf;
    ::GetLocalTime(&timeBuf);
    sprintf(buf, "%02d:%02d:%02d.%03d - ", timeBuf.wHour, timeBuf.wMinute, timeBuf.wSecond, timeBuf.wMilliseconds);
    return string(buf);
}

void testStub(std::shared_ptr<grpc::Channel> chan)
{
    MessageProcessor::Stub client(chan);

    Void _void;
    AccumulateAmount amount;
    amount.set_amount(42);

    grpc::ClientContext ctx;
    ctx.set_deadline(IN_MILLISECONDS(100));

    cout << NowString() << "    Making RPC\n";
    grpc::Status st = client.Accumulate(&ctx, amount, &_void);
    cout << NowString() << "    Leaving testStub()\n";
}

void test()
{
    auto chan = grpc::CreateChannel("localhost:54321", grpc::InsecureChannelCredentials());

    cout << NowString() << "  Channel Up- Testing Stub\n";
    testStub(chan);
    cout << NowString() << "  Leaving test()\n";
}

int main()
{
    cout << NowString() << "Calling test()\n";
    test();
    cout << NowString() << "Exiting 'main'\n";
    return 1;
}

输出是

11:42:05.400 - Calling test()
11:42:05.403 -   Channel Up- Testing Stub
11:42:05.404 -     Making RPC
11:42:05.506 -     Leaving testStub()
11:42:05.507 -   Leaving test()
11:42:15.545 - Exiting 'main'
Press any key to continue . . .

从时间戳可以看出,Channel 的析构函数只用了 10 多秒。

我的问题是这样的:我可以做些什么来显着减少销毁 grpc 通道所需的时间?

【问题讨论】:

    标签: c++ grpc


    【解决方案1】:

    你能检查一下通话的状态吗?在您的代码中,在grpc::Status st = client.Accumulate(&amp;ctx, amount, &amp;_void) 之后检查st.ok()(参见this 示例)。如果st.ok() 为假,请尝试打印st.error_message()st.error_code() 以获取更多信息。

    将环境变量GRPC_VERBOSITY 设置为debug 来启动它也可能很有用(有关更多信息,请参阅this document)。

    【讨论】:

      猜你喜欢
      • 2016-07-13
      • 2019-10-01
      • 2018-03-30
      • 2019-01-22
      • 1970-01-01
      • 2022-12-27
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      相关资源
      最近更新 更多