src/core/lib/surface/channel.h
Go to the documentation of this file.
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_CORE_LIB_SURFACE_CHANNEL_H
20 #define GRPC_CORE_LIB_SURFACE_CHANNEL_H
21 
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #include <atomic>
28 #include <map>
29 #include <string>
30 #include <utility>
31 
32 #include "absl/base/thread_annotations.h"
33 #include "absl/status/statusor.h"
34 #include "absl/strings/string_view.h"
35 #include "absl/types/optional.h"
36 
40 #include <grpc/slice.h>
41 
44 #include "src/core/lib/channel/channel_stack.h" // IWYU pragma: keep
58 
62 
72  grpc_channel* channel, grpc_call* parent_call, uint32_t propagation_mask,
73  grpc_pollset_set* pollset_set, const grpc_slice& method,
74  const grpc_slice* host, grpc_core::Timestamp deadline, void* reserved);
75 
78 
81 
84 
85 namespace grpc_core {
86 
90 
91  explicit RegisteredCall(const char* method_arg, const char* host_arg);
92  RegisteredCall(const RegisteredCall& other);
93  RegisteredCall& operator=(const RegisteredCall&) = delete;
94 
96 };
97 
100  // The map key should be owned strings rather than unowned char*'s to
101  // guarantee that it outlives calls on the core channel (which may outlast the
102  // C++ or other wrapped language Channel that registered these calls).
103  std::map<std::pair<std::string, std::string>, RegisteredCall> map
105  int method_registration_attempts ABSL_GUARDED_BY(mu) = 0;
106 };
107 
108 class Channel : public RefCounted<Channel>,
109  public CppImplOf<Channel, grpc_channel> {
110  public:
112  const char* target, ChannelArgs args,
113  grpc_channel_stack_type channel_stack_type,
114  grpc_transport* optional_transport);
115 
118 
120 
122  return compression_options_;
123  }
124 
126 
127  size_t CallSizeEstimate() {
128  // We round up our current estimate to the NEXT value of kRoundUpSize.
129  // This ensures:
130  // 1. a consistent size allocation when our estimate is drifting slowly
131  // (which is common) - which tends to help most allocators reuse memory
132  // 2. a small amount of allowed growth over the estimate without hitting
133  // the arena size doubling case, reducing overall memory usage
134  static constexpr size_t kRoundUpSize = 256;
135  return (call_size_estimate_.load(std::memory_order_relaxed) +
136  2 * kRoundUpSize) &
137  ~(kRoundUpSize - 1);
138  }
139 
140  void UpdateCallSizeEstimate(size_t size);
141  absl::string_view target() const { return target_; }
143  bool is_client() const { return is_client_; }
144  RegisteredCall* RegisterCall(const char* method, const char* host);
145 
148  return registration_table_.map.size();
149  }
150 
153  return registration_table_.method_registration_attempts;
154  }
155 
156  private:
157  Channel(bool is_client, std::string target, ChannelArgs channel_args,
160 
161  const bool is_client_;
163  std::atomic<size_t> call_size_estimate_;
169 };
170 
171 } // namespace grpc_core
172 
174  const grpc_channel* channel) {
176 }
177 
181 }
182 
186 }
187 
189  const char* reason) {
191 }
193  const char* reason) {
195 }
196 
197 // Return the channel's compression options.
199  const grpc_channel* channel);
200 
201 // Ping the channels peer (load balanced channels will select one sub-channel to
202 // ping); if the channel is not connected, posts a failed.
204  void* tag, void* reserved);
205 
206 #endif /* GRPC_CORE_LIB_SURFACE_CHANNEL_H */
grpc_core::RegisteredCall::authority
absl::optional< Slice > authority
Definition: src/core/lib/surface/channel.h:89
grpc_core::Channel
Definition: src/core/lib/surface/channel.h:108
slice.h
grpc_core::Channel::is_client_
const bool is_client_
Definition: src/core/lib/surface/channel.h:161
grpc_core::Channel::is_client
bool is_client() const
Definition: src/core/lib/surface/channel.h:143
grpc_channel_stack
Definition: channel_stack.h:202
channel_fwd.h
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
grpc_core::Channel::TestOnlyRegisteredCalls
int TestOnlyRegisteredCalls()
Definition: src/core/lib/surface/channel.h:146
grpc_core::RefCountedPtr::get
T * get() const
Definition: ref_counted_ptr.h:146
slice.h
grpc_core::Channel::channel_stack_
const RefCountedPtr< grpc_channel_stack > channel_stack_
Definition: src/core/lib/surface/channel.h:168
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Channel::allocator_
MemoryAllocator allocator_
Definition: src/core/lib/surface/channel.h:166
grpc_core::Slice
Definition: src/core/lib/slice/slice.h:282
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
grpc_core::Channel::registration_table_
CallRegistrationTable registration_table_
Definition: src/core/lib/surface/channel.h:164
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
grpc_core::RegisteredCall::operator=
RegisteredCall & operator=(const RegisteredCall &)=delete
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
grpc_channel_ping
void grpc_channel_ping(grpc_channel *channel, grpc_completion_queue *cq, void *tag, void *reserved)
Definition: channel_ping.cc:52
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::Channel::channel_stack
grpc_channel_stack * channel_stack() const
Definition: src/core/lib/surface/channel.h:119
grpc_channel_get_channel_stack
grpc_channel_stack * grpc_channel_get_channel_stack(grpc_channel *channel)
Definition: src/core/lib/surface/channel.h:178
grpc_core::Channel::target_
std::string target_
Definition: src/core/lib/surface/channel.h:167
grpc_compression_options
Definition: compression_types.h:80
channelz.h
grpc_core::Channel::channelz_node_
RefCountedPtr< channelz::ChannelNode > channelz_node_
Definition: src/core/lib/surface/channel.h:165
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
grpc_channel_get_call_size_estimate
size_t grpc_channel_get_call_size_estimate(grpc_channel *channel)
grpc_core::ChannelStackBuilder
Definition: channel_stack_builder.h:41
grpc_types.h
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
grpc_core::RefCounted::Unref
void Unref()
Definition: ref_counted.h:302
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
grpc_core::Channel::compression_options
grpc_compression_options compression_options() const
Definition: src/core/lib/surface/channel.h:121
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::RefCountedPtr< grpc_channel_stack >
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
grpc_channel_internal_ref
void grpc_channel_internal_ref(grpc_channel *channel, const char *reason)
Definition: src/core/lib/surface/channel.h:188
channel_stack.h
grpc_completion_queue
Definition: completion_queue.cc:347
grpc_channel_get_channelz_node
grpc_core::channelz::ChannelNode * grpc_channel_get_channelz_node(grpc_channel *channel)
Definition: src/core/lib/surface/channel.h:183
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
absl::optional
Definition: abseil-cpp/absl/types/internal/optional.h:61
cpp_impl_of.h
time.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_core::Channel::RegisterCall
RegisteredCall * RegisterCall(const char *method, const char *host)
Definition: channel.cc:378
grpc_core::Channel::CallSizeEstimate
size_t CallSizeEstimate()
Definition: src/core/lib/surface/channel.h:127
grpc_channel_create_pollset_set_call
grpc_call * grpc_channel_create_pollset_set_call(grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask, grpc_pollset_set *pollset_set, const grpc_slice &method, const grpc_slice *host, grpc_core::Timestamp deadline, void *reserved)
Definition: channel.cc:331
grpc_core::Channel::TestOnlyRegistrationAttempts
int TestOnlyRegistrationAttempts()
Definition: src/core/lib/surface/channel.h:151
grpc_core::RefCounted
Definition: ref_counted.h:280
grpc_channel_internal_unref
void grpc_channel_internal_unref(grpc_channel *channel, const char *reason)
Definition: src/core/lib/surface/channel.h:192
channel_stack_type.h
stdint.h
transport_fwd.h
grpc_channel_destroy_internal
void grpc_channel_destroy_internal(grpc_channel *channel)
Definition: channel.cc:425
grpc_core::channelz::ChannelNode
Definition: channelz.h:178
compression_types.h
grpc_core::CallRegistrationTable::ABSL_GUARDED_BY
std::map< std::pair< std::string, std::string >, RegisteredCall > map ABSL_GUARDED_BY(mu)
grpc_core::Channel::call_size_estimate_
std::atomic< size_t > call_size_estimate_
Definition: src/core/lib/surface/channel.h:163
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
debug_location.h
grpc_core::Channel::Channel
Channel(bool is_client, std::string target, ChannelArgs channel_args, grpc_compression_options compression_options, RefCountedPtr< grpc_channel_stack > channel_stack)
Definition: channel.cc:65
ref_counted.h
grpc_core::Channel::channelz_node
channelz::ChannelNode * channelz_node() const
Definition: src/core/lib/surface/channel.h:125
grpc_core::CallRegistrationTable
Definition: src/core/lib/surface/channel.h:98
grpc_channel_stack_type
grpc_channel_stack_type
Definition: channel_stack_type.h:24
grpc_core::ChannelArgs
Definition: channel_args.h:111
ref_counted_ptr.h
grpc_channel
struct grpc_channel grpc_channel
Definition: grpc_types.h:62
grpc_core::RegisteredCall::path
Slice path
Definition: src/core/lib/surface/channel.h:88
grpc_transport
Definition: transport_impl.h:89
memory_quota.h
grpc_core::Channel::target
absl::string_view target() const
Definition: src/core/lib/surface/channel.h:141
channel_args.h
grpc_core::Channel::UpdateCallSizeEstimate
void UpdateCallSizeEstimate(size_t size)
Definition: channel.cc:236
memory_allocator.h
grpc_channel_compression_options
grpc_compression_options grpc_channel_compression_options(const grpc_channel *channel)
Definition: src/core/lib/surface/channel.h:173
grpc_core::Channel::compression_options_
const grpc_compression_options compression_options_
Definition: src/core/lib/surface/channel.h:162
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
iomgr_fwd.h
grpc_core::RegisteredCall::RegisteredCall
RegisteredCall(const char *method_arg, const char *host_arg)
Definition: channel.cc:347
grpc_core::Channel::CreateWithBuilder
static absl::StatusOr< RefCountedPtr< Channel > > CreateWithBuilder(ChannelStackBuilder *builder)
Definition: channel.cc:106
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
method
NSString * method
Definition: ProtoMethod.h:28
grpc_core::CppImplOf< Channel, grpc_channel >::FromC
static Channel * FromC(grpc_channel *c_type)
Definition: cpp_impl_of.h:30
grpc_channel_update_call_size_estimate
void grpc_channel_update_call_size_estimate(grpc_channel *channel, size_t size)
channel_stack_builder.h
grpc_core::RegisteredCall::~RegisteredCall
~RegisteredCall()
Definition: channel.cc:361
grpc_core::RegisteredCall
Definition: src/core/lib/surface/channel.h:87
grpc_core::CallRegistrationTable::mu
Mutex mu
Definition: src/core/lib/surface/channel.h:99
grpc_core::CppImplOf
Definition: cpp_impl_of.h:27
grpc_core::Channel::Create
static absl::StatusOr< RefCountedPtr< Channel > > Create(const char *target, ChannelArgs args, grpc_channel_stack_type channel_stack_type, grpc_transport *optional_transport)
Definition: channel.cc:202
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
grpc_core::Channel::allocator
MemoryAllocator * allocator()
Definition: src/core/lib/surface/channel.h:142
sync.h
grpc_core::RefCounted::Ref
RefCountedPtr< Child > Ref() GRPC_MUST_USE_RESULT
Definition: ref_counted.h:287
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52