kbobyrev updated this revision to Diff 310642.
kbobyrev marked 5 inline comments as done.
kbobyrev added a comment.
Resolve review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92198/new/

https://reviews.llvm.org/D92198

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
@@ -6,8 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <grpc++/grpc++.h>
-
 #include "Client.h"
 #include "Service.grpc.pb.h"
 #include "index/Index.h"
@@ -18,15 +16,42 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Error.h"
+#include <atomic>
+#include <grpc++/grpc++.h>
 
 #include <chrono>
+#include <memory>
 
 namespace clang {
 namespace clangd {
 namespace remote {
 namespace {
 
+llvm::StringRef toString(const grpc_connectivity_state &State) {
+  switch (State) {
+  case GRPC_CHANNEL_IDLE:
+    return "idle";
+  case GRPC_CHANNEL_CONNECTING:
+    return "connecting";
+  case GRPC_CHANNEL_READY:
+    return "ready";
+  case GRPC_CHANNEL_TRANSIENT_FAILURE:
+    return "transient failure";
+  case GRPC_CHANNEL_SHUTDOWN:
+    return "shutdown";
+  }
+  llvm_unreachable("Not a valid grpc_connectivity_state.");
+}
+
 class IndexClient : public clangd::SymbolIndex {
+  void updateConnectionStatus() const {
+    auto NewStatus = Channel->GetState(/*try_to_connect=*/false);
+    if (ConnectionStatus != NewStatus)
+      vlog("Remote index connection [{0}]: {1} => {2}", ServerAddress,
+           toString(ConnectionStatus), toString(NewStatus));
+    ConnectionStatus.exchange(NewStatus);
+  }
+
   template <typename RequestT, typename ReplyT>
   using StreamingCall = std::unique_ptr<grpc::ClientReader<ReplyT>> (
       remote::v1::SymbolIndex::Stub::*)(grpc::ClientContext *,
@@ -37,6 +62,7 @@
   bool streamRPC(ClangdRequestT Request,
                  StreamingCall<RequestT, ReplyT> RPCCall,
                  CallbackT Callback) const {
+    updateConnectionStatus();
     bool FinalResult = false;
     trace::Span Tracer(RequestT::descriptor()->name());
     const auto RPCRequest = ProtobufMarshaller->toProtobuf(Request);
@@ -77,18 +103,21 @@
     SPAN_ATTACH(Tracer, "Status", Reader->Finish().ok());
     SPAN_ATTACH(Tracer, "Successful", Successful);
     SPAN_ATTACH(Tracer, "Failed to parse", FailedToParse);
+    updateConnectionStatus();
     return FinalResult;
   }
 
 public:
   IndexClient(
-      std::shared_ptr<grpc::Channel> Channel, llvm::StringRef ProjectRoot,
-      llvm::StringRef Address,
+      std::shared_ptr<grpc::Channel> Channel, llvm::StringRef Address,
+      llvm::StringRef ProjectRoot,
       std::chrono::milliseconds DeadlineTime = std::chrono::milliseconds(1000))
-      : Stub(remote::v1::SymbolIndex::NewStub(Channel)),
+      : Stub(remote::v1::SymbolIndex::NewStub(Channel)), Channel(Channel),
+        ServerAddress(Address),
+        ConnectionStatus(Channel->GetState(/*try_to_connect=*/true)),
         ProtobufMarshaller(new Marshaller(/*RemoteIndexRoot=*/"",
                                           /*LocalIndexRoot=*/ProjectRoot)),
-        ServerAddress(Address), DeadlineWaitingTime(DeadlineTime) {
+        DeadlineWaitingTime(DeadlineTime) {
     assert(!ProjectRoot.empty());
   }
 
@@ -128,7 +157,9 @@
 
 private:
   std::unique_ptr<remote::v1::SymbolIndex::Stub> Stub;
+  std::shared_ptr<grpc::Channel> Channel;
   llvm::SmallString<256> ServerAddress;
+  mutable std::atomic<grpc_connectivity_state> ConnectionStatus;
   std::unique_ptr<Marshaller> ProtobufMarshaller;
   // Each request will be terminated if it takes too long.
   std::chrono::milliseconds DeadlineWaitingTime;
@@ -140,9 +171,8 @@
                                                llvm::StringRef ProjectRoot) {
   const auto Channel =
       grpc::CreateChannel(Address.str(), grpc::InsecureChannelCredentials());
-  Channel->GetState(true);
   return std::unique_ptr<clangd::SymbolIndex>(
-      new IndexClient(Channel, ProjectRoot, Address));
+      new IndexClient(Channel, Address, ProjectRoot));
 }
 
 } // namespace remote
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to