Alex-PLACET commented on code in PR #50407:
URL: https://github.com/apache/arrow/pull/50407#discussion_r3912632811


##########
cpp/src/arrow/flight/transport/grpc/grpc_server_internal.h:
##########
@@ -0,0 +1,213 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <memory>
+#include <string>
+#include <unordered_map>
+#include <utility>
+#include <vector>
+
+#include <grpcpp/grpcpp.h>
+
+#include "arrow/flight/server.h"
+#include "arrow/flight/server_middleware.h"
+#include "arrow/flight/transport/grpc/util_internal.h"
+#include "arrow/util/uri.h"
+
+namespace arrow::flight::transport::grpc {
+
+using MiddlewareFactoryList =
+    std::vector<std::pair<std::string, 
std::shared_ptr<ServerMiddlewareFactory>>>;
+
+ARROW_FLIGHT_EXPORT
+::grpc::Status FinishGrpcServerStatus(const Status& arrow_status,
+                                      ::grpc::ServerContext* context);
+
+ARROW_FLIGHT_EXPORT
+::grpc::Status FinishGrpcServerStatus(const Status& arrow_status,
+                                      ::grpc::CallbackServerContext* context);
+
+template <typename GrpcContext>
+class GrpcAddServerHeaders : public AddCallHeaders {
+ public:
+  explicit GrpcAddServerHeaders(GrpcContext* context) : context_(context) {}
+  ~GrpcAddServerHeaders() override = default;
+
+  void AddHeader(const std::string& key, const std::string& value) override {
+    context_->AddInitialMetadata(key, value);
+  }
+
+ private:
+  GrpcContext* context_;
+};
+
+template <typename GrpcContext>
+class GrpcServerCallContext : public ServerCallContext {
+ public:
+  explicit GrpcServerCallContext(GrpcContext* context)
+      : context_(context), peer_(context->peer()) {
+    for (const auto& entry : context->client_metadata()) {
+      incoming_headers_.insert(
+          {std::string_view(entry.first.data(), entry.first.length()),
+           std::string_view(entry.second.data(), entry.second.length())});
+    }
+  }
+
+  const std::string& peer_identity() const override { return peer_identity_; }
+  const std::string& peer() const override { return peer_; }
+  bool is_cancelled() const override { return context_->IsCancelled(); }
+  const CallHeaders& incoming_headers() const override { return 
incoming_headers_; }
+
+  // Helper method that runs interceptors given the result of an RPC,
+  // then returns the final gRPC status to send to the client
+  ::grpc::Status FinishRequest(const ::grpc::Status& status) {
+    // Don't double-convert status - return the original one here
+    FinishRequest(FromGrpcStatus(status));
+    return status;
+  }
+
+  ::grpc::Status FinishRequest(const Status& status) {
+    for (const auto& instance : middleware_) {
+      instance->CallCompleted(status);
+    }
+    return FinishGrpcServerStatus(status, context_);
+  }
+
+  void AddHeader(const std::string& key, const std::string& value) const 
override {
+    context_->AddInitialMetadata(key, value);
+  }
+
+  void AddTrailer(const std::string& key, const std::string& value) const 
override {
+    context_->AddTrailingMetadata(key, value);
+  }
+
+  ServerMiddleware* GetMiddleware(const std::string& key) const override {
+    const auto& instance = middleware_map_.find(key);
+    if (instance == middleware_map_.end()) {
+      return nullptr;
+    }
+    return instance->second.get();
+  }
+
+ private:
+  template <typename>
+  friend class GrpcServerCallContextHelper;
+
+  GrpcContext* context_;
+  std::string peer_;
+  std::string peer_identity_;
+  std::vector<std::shared_ptr<ServerMiddleware>> middleware_;
+  std::unordered_map<std::string, std::shared_ptr<ServerMiddleware>> 
middleware_map_;
+  CallHeaders incoming_headers_;
+};

Review Comment:
   Moved from 
https://github.com/apache/arrow/pull/50407/changes/BASE..20c2325e0b8481adf97aeac53b815f2509b9f301#diff-b3f03a3354d6ebe8a7212c9fd37b8c4333357552947460a941b39777baa28b17L115



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to