Grpc 客户端C++通道析构函数需要 10 秒
Grpc Client-side C++ Channel Destructor taking 10 seconds
代码
的目的最初是为了在 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
可能很有用(有关详细信息,请参阅此文档(。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 什么时候调用析构函数
- 优先顺序:智能指针和类析构函数
- C++-明确何时以及如何调用析构函数
- 使用基类指针创建对象时,缺少派生类析构函数
- 在c++中使用向量时,如何调用构造函数和析构函数
- 重载运算符new[]的行为取决于析构函数
- 我需要知道编译器如何在cpp中使用析构函数
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 析构函数调用
- 通过引用传递-为什么要调用这个析构函数
- 对具有动态分配的内存和析构函数的类对象的引用
- 重载 -> shared_ptr 个实例中的箭头运算符<interface>,接口中没有纯虚拟析构函数
- C++成员的析构函数顺序与shared_ptr
- C++ 防止在映射中放置()时调用析构函数
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- 调用析构函数以释放动态分配的内存
- Grpc 客户端C++通道析构函数需要 10 秒