[PATCH] D82844: [clangd] Set gRPC deadlines to all remote index requests

Kirill Bobyrev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 30 00:30:13 PDT 2020


kbobyrev created this revision.
kbobyrev added a reviewer: sammccall.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.

"TL;DR: Always set a deadline.", https://grpc.io/blog/deadlines/


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82844

Files:
  clang-tools-extra/clangd/index/remote/Client.cpp


Index: clang-tools-extra/clangd/index/remote/Client.cpp
===================================================================
--- clang-tools-extra/clangd/index/remote/Client.cpp
+++ clang-tools-extra/clangd/index/remote/Client.cpp
@@ -15,6 +15,8 @@
 #include "support/Logger.h"
 #include "support/Trace.h"
 
+#include <chrono>
+
 namespace clang {
 namespace clangd {
 namespace remote {
@@ -25,7 +27,6 @@
   using StreamingCall = std::unique_ptr<grpc::ClientReader<ReplyT>> (
       remote::SymbolIndex::Stub::*)(grpc::ClientContext *, const RequestT &);
 
-  // FIXME(kirillbobyrev): Set deadlines for requests.
   template <typename RequestT, typename ReplyT, typename ClangdRequestT,
             typename CallbackT>
   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<grpc::Channel> Channel)
-      : Stub(remote::SymbolIndex::NewStub(Channel)) {}
+  IndexClient(
+      std::shared_ptr<grpc::Channel> 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<void(const clangd::Symbol &)> Callback) const {
@@ -84,6 +91,8 @@
 
 private:
   std::unique_ptr<remote::SymbolIndex::Stub> Stub;
+  // Each request will be terminated if it takes too long.
+  const std::chrono::milliseconds DeadlineWaitingTime;
 };
 
 } // namespace


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82844.274346.patch
Type: text/x-patch
Size: 1968 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200630/17591170/attachment-0001.bin>


More information about the cfe-commits mailing list