• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2024 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_SRC_CPP_SERVER_ORCA_ORCA_SERVICE_H
18 #define GRPC_SRC_CPP_SERVER_ORCA_ORCA_SERVICE_H
19 
20 #include <grpc/event_engine/event_engine.h>
21 #include <grpcpp/ext/orca_service.h>
22 #include <grpcpp/impl/sync.h>
23 #include <grpcpp/support/byte_buffer.h>
24 #include <grpcpp/support/server_callback.h>
25 #include <grpcpp/support/status.h>
26 
27 #include <atomic>
28 #include <memory>
29 
30 #include "absl/base/thread_annotations.h"
31 #include "absl/strings/string_view.h"
32 #include "absl/types/optional.h"
33 #include "src/core/util/ref_counted.h"
34 
35 namespace grpc {
36 namespace experimental {
37 
38 class OrcaService::Reactor
39     : public ServerWriteReactor<ByteBuffer>,
40       public grpc_core::RefCounted<OrcaService::Reactor> {
41  public:
42   explicit Reactor(OrcaService* service, absl::string_view peer,
43                    const ByteBuffer* request_buffer,
44                    std::shared_ptr<OrcaService::ReactorHook> hook);
45 
46   void OnWriteDone(bool ok) override;
47 
48   void OnCancel() override;
49 
50   void OnDone() override;
51 
52  private:
53   void FinishRpc(grpc::Status status);
54 
55   void SendResponse();
56 
57   bool MaybeScheduleTimer();
58 
59   bool MaybeCancelTimer();
60 
61   void OnTimer();
62 
63   OrcaService* service_;
64 
65   grpc::internal::Mutex timer_mu_;
66   absl::optional<grpc_event_engine::experimental::EventEngine::TaskHandle>
67       timer_handle_ ABSL_GUARDED_BY(&timer_mu_);
68   bool cancelled_ ABSL_GUARDED_BY(&timer_mu_) = false;
69 
70   grpc_event_engine::experimental::EventEngine::Duration report_interval_;
71   ByteBuffer response_;
72   std::shared_ptr<ReactorHook> hook_;
73   std::shared_ptr<grpc_event_engine::experimental::EventEngine> engine_;
74 };
75 
76 }  // namespace experimental
77 }  // namespace grpc
78 
79 #endif  // GRPC_SRC_CPP_SERVER_ORCA_ORCA_SERVICE_H
80