xref: /aosp_15_r20/external/grpc-grpc/src/core/ext/transport/chaotic_good/client/chaotic_good_connector.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2024 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_CHAOTIC_GOOD_CLIENT_CHAOTIC_GOOD_CONNECTOR_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHAOTIC_GOOD_CLIENT_CHAOTIC_GOOD_CONNECTOR_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 
24 #include "absl/random/random.h"
25 #include "absl/status/statusor.h"
26 
27 #include <grpc/event_engine/event_engine.h>
28 
29 #include "src/core/client_channel/connector.h"
30 #include "src/core/ext/transport/chttp2/transport/hpack_encoder.h"
31 #include "src/core/ext/transport/chttp2/transport/hpack_parser.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/event_engine/channel_args_endpoint_config.h"
34 #include "src/core/lib/gprpp/notification.h"
35 #include "src/core/lib/gprpp/ref_counted_ptr.h"
36 #include "src/core/lib/gprpp/sync.h"
37 #include "src/core/lib/iomgr/closure.h"
38 #include "src/core/lib/iomgr/endpoint.h"
39 #include "src/core/lib/iomgr/error.h"
40 #include "src/core/lib/iomgr/exec_ctx.h"
41 #include "src/core/lib/promise/activity.h"
42 #include "src/core/lib/promise/inter_activity_latch.h"
43 #include "src/core/lib/promise/wait_for_callback.h"
44 #include "src/core/lib/resource_quota/arena.h"
45 #include "src/core/lib/resource_quota/memory_quota.h"
46 #include "src/core/lib/resource_quota/resource_quota.h"
47 #include "src/core/lib/transport/handshaker.h"
48 #include "src/core/lib/transport/promise_endpoint.h"
49 
50 namespace grpc_core {
51 namespace chaotic_good {
52 class ChaoticGoodConnector : public SubchannelConnector {
53  public:
54   explicit ChaoticGoodConnector(
55       std::shared_ptr<grpc_event_engine::experimental::EventEngine>
56           event_engine);
57   ~ChaoticGoodConnector() override;
58   void Connect(const Args& args, Result* result, grpc_closure* notify) override;
Shutdown(grpc_error_handle error)59   void Shutdown(grpc_error_handle error) override {
60     ActivityPtr connect_activity;
61     MutexLock lock(&mu_);
62     if (is_shutdown_) return;
63     is_shutdown_ = true;
64     if (handshake_mgr_ != nullptr) {
65       handshake_mgr_->Shutdown(error);
66     }
67     connect_activity = std::move(connect_activity_);
68   };
69 
70  private:
71   static auto DataEndpointReadSettingsFrame(
72       RefCountedPtr<ChaoticGoodConnector> self);
73   static auto DataEndpointWriteSettingsFrame(
74       RefCountedPtr<ChaoticGoodConnector> self);
75   static auto ControlEndpointReadSettingsFrame(
76       RefCountedPtr<ChaoticGoodConnector> self);
77   static auto ControlEndpointWriteSettingsFrame(
78       RefCountedPtr<ChaoticGoodConnector> self);
79   static auto WaitForDataEndpointSetup(
80       RefCountedPtr<ChaoticGoodConnector> self);
81   static void OnHandshakeDone(void* arg, grpc_error_handle error);
82 
83   grpc_event_engine::experimental::MemoryAllocator memory_allocator_ =
84       ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
85           "connect_activity");
86   ScopedArenaPtr arena_ = MakeScopedArena(1024, &memory_allocator_);
87   Mutex mu_;
88   Args args_;
89   Result* result_ ABSL_GUARDED_BY(mu_);
90   grpc_closure* notify_ = nullptr;
91   bool is_shutdown_ ABSL_GUARDED_BY(mu_) = false;
92   absl::StatusOr<grpc_event_engine::experimental::EventEngine::ResolvedAddress>
93       resolved_addr_;
94 
95   PromiseEndpoint control_endpoint_;
96   PromiseEndpoint data_endpoint_;
97   ActivityPtr connect_activity_ ABSL_GUARDED_BY(mu_);
98   const std::shared_ptr<grpc_event_engine::experimental::EventEngine>
99       event_engine_;
100   std::shared_ptr<HandshakeManager> handshake_mgr_;
101   HPackCompressor hpack_compressor_;
102   HPackParser hpack_parser_;
103   absl::BitGen bitgen_;
104   InterActivityLatch<void> data_endpoint_ready_;
105   std::string connection_id_;
106 };
107 }  // namespace chaotic_good
108 }  // namespace grpc_core
109 
110 grpc_channel* grpc_chaotic_good_channel_create(const char* target,
111                                                const grpc_channel_args* args);
112 
113 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHAOTIC_GOOD_CLIENT_CHAOTIC_GOOD_CONNECTOR_H
114