xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/surface/legacy_channel.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_SRC_CORE_LIB_SURFACE_LEGACY_CHANNEL_H
20 #define GRPC_SRC_CORE_LIB_SURFACE_LEGACY_CHANNEL_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <string>
25 
26 #include "absl/status/statusor.h"
27 #include "absl/types/optional.h"
28 
29 #include <grpc/event_engine/event_engine.h>
30 #include <grpc/grpc.h>
31 
32 #include "src/core/client_channel/client_channel_filter.h"
33 #include "src/core/lib/channel/channel_args.h"
34 #include "src/core/lib/channel/channel_fwd.h"
35 #include "src/core/lib/channel/channel_stack.h"  // IWYU pragma: keep
36 #include "src/core/lib/debug/stats.h"
37 #include "src/core/lib/gprpp/ref_counted_ptr.h"
38 #include "src/core/lib/gprpp/time.h"
39 #include "src/core/lib/iomgr/iomgr_fwd.h"
40 #include "src/core/lib/slice/slice.h"
41 #include "src/core/lib/surface/channel.h"
42 #include "src/core/lib/surface/channel_stack_type.h"
43 #include "src/core/lib/transport/call_size_estimator.h"
44 
45 namespace grpc_core {
46 
47 class LegacyChannel final : public Channel {
48  public:
49   static absl::StatusOr<OrphanablePtr<Channel>> Create(
50       std::string target, ChannelArgs args,
51       grpc_channel_stack_type channel_stack_type);
52 
53   // Do not instantiate directly -- use Create() instead.
54   LegacyChannel(bool is_client, bool is_promising, std::string target,
55                 const ChannelArgs& channel_args,
56                 RefCountedPtr<grpc_channel_stack> channel_stack);
57 
58   void Orphan() override;
59 
CreateArena()60   Arena* CreateArena() override {
61     const size_t initial_size = call_size_estimator_.CallSizeEstimate();
62     global_stats().IncrementCallInitialSize(initial_size);
63     return Arena::Create(initial_size, &allocator_);
64   }
DestroyArena(Arena * arena)65   void DestroyArena(Arena* arena) override {
66     call_size_estimator_.UpdateCallSizeEstimate(arena->TotalUsedBytes());
67     arena->Destroy();
68   }
69 
70   bool IsLame() const override;
71 
72   grpc_call* CreateCall(grpc_call* parent_call, uint32_t propagation_mask,
73                         grpc_completion_queue* cq,
74                         grpc_pollset_set* pollset_set_alternative, Slice path,
75                         absl::optional<Slice> authority, Timestamp deadline,
76                         bool registered_method) override;
77 
event_engine()78   grpc_event_engine::experimental::EventEngine* event_engine() const override {
79     return channel_stack_->EventEngine();
80   }
81 
82   bool SupportsConnectivityWatcher() const override;
83 
84   grpc_connectivity_state CheckConnectivityState(bool try_to_connect) override;
85 
86   void WatchConnectivityState(grpc_connectivity_state last_observed_state,
87                               Timestamp deadline, grpc_completion_queue* cq,
88                               void* tag) override;
89 
90   void AddConnectivityWatcher(
91       grpc_connectivity_state initial_state,
92       OrphanablePtr<AsyncConnectivityStateWatcherInterface> watcher) override;
93   void RemoveConnectivityWatcher(
94       AsyncConnectivityStateWatcherInterface* watcher) override;
95 
96   void GetInfo(const grpc_channel_info* channel_info) override;
97 
98   void ResetConnectionBackoff() override;
99 
100   void Ping(grpc_completion_queue* cq, void* tag) override;
101 
is_client()102   bool is_client() const override { return is_client_; }
is_promising()103   bool is_promising() const override { return is_promising_; }
channel_stack()104   grpc_channel_stack* channel_stack() const override {
105     return channel_stack_.get();
106   }
107 
108  private:
109   class StateWatcher;
110 
111   // Returns the client channel filter if this is a client channel,
112   // otherwise null.
113   ClientChannelFilter* GetClientChannelFilter() const;
114 
115   const bool is_client_;
116   const bool is_promising_;
117   RefCountedPtr<grpc_channel_stack> channel_stack_;
118   CallSizeEstimator call_size_estimator_{1024};
119   grpc_event_engine::experimental::MemoryAllocator allocator_;
120 };
121 
122 }  // namespace grpc_core
123 
124 #endif  // GRPC_SRC_CORE_LIB_SURFACE_LEGACY_CHANNEL_H
125