2017-05-29 136 views
0

代碼的目的是最初在grpc服務不可用時測試grpc存根的操作。然而,我所看到的行爲表明,有些事情我不明白 - 因此是一個問題。Grpc客戶端C++通道析構函數花費10秒

在此代碼:

#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通道所需的時間?

回答

0

您能檢查通話狀態嗎?在您的代碼中,在grpc::Status st = client.Accumulate(&ctx, amount, &_void)之後檢查st.ok()(例如,參見this)。如果st.ok()爲false,請嘗試打印st.error_message()st.error_code()以獲取更多信息。

將環境變量GRPC_VERBOSITY設置爲debug(有關詳細信息,請參閱this document),啓動它也可能很有用。