diff --git a/clang-tools-extra/clangd/index/remote/Client.cpp b/clang-tools-extra/clangd/index/remote/Client.cpp --- a/clang-tools-extra/clangd/index/remote/Client.cpp +++ b/clang-tools-extra/clangd/index/remote/Client.cpp @@ -15,6 +15,8 @@ #include "support/Logger.h" #include "support/Trace.h" +#include + namespace clang { namespace clangd { namespace remote { @@ -25,7 +27,6 @@ using StreamingCall = std::unique_ptr> ( remote::SymbolIndex::Stub::*)(grpc::ClientContext *, const RequestT &); - // FIXME(kirillbobyrev): Set deadlines for requests. template bool streamRPC(ClangdRequestT Request, @@ -35,6 +36,9 @@ trace::Span Tracer(RequestT::descriptor()->name()); const auto RPCRequest = toProtobuf(Request); grpc::ClientContext Context; + std::chrono::system_clock::time_point Deadline = + std::chrono::system_clock::now() + DeadlineWaitingTime; + Context.set_deadline(Deadline); auto Reader = (Stub.get()->*RPCCall)(&Context, RPCRequest); llvm::BumpPtrAllocator Arena; llvm::UniqueStringSaver Strings(Arena); @@ -54,8 +58,11 @@ } public: - IndexClient(std::shared_ptr Channel) - : Stub(remote::SymbolIndex::NewStub(Channel)) {} + IndexClient( + std::shared_ptr Channel, + std::chrono::milliseconds DeadlineTime = std::chrono::milliseconds(500)) + : Stub(remote::SymbolIndex::NewStub(Channel)), + DeadlineWaitingTime(DeadlineTime) {} void lookup(const clangd::LookupRequest &Request, llvm::function_ref Callback) const { @@ -84,6 +91,8 @@ private: std::unique_ptr Stub; + // Each request will be terminated if it takes too long. + const std::chrono::milliseconds DeadlineWaitingTime; }; } // namespace