Grpc 客户端C++通道析构函数需要 10 秒


代码

的目的最初是为了在 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 RPCn";
    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 Stubn";
    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 . . .

从时间戳中可以明显看出,通道的析构函数仅花费了 10 多秒的时间。

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

你能检查一下通话的状态吗?在代码中,在grpc::Status st = client.Accumulate(&ctx, amount, &_void)后立即检查st.ok()(有关示例,请参阅此处(。如果st.ok()为 false,请尝试打印st.error_message()st.error_code()以获取更多信息。

在将环境变量设置为 debug 的情况下启动它也GRPC_VERBOSITY可能很有用(有关详细信息,请参阅此文档(。

最新更新