event_engine.h
Go to the documentation of this file.
1 // Copyright 2021 The 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 #ifndef GRPC_EVENT_ENGINE_EVENT_ENGINE_H
15 #define GRPC_EVENT_ENGINE_EVENT_ENGINE_H
16 
18 
19 #include <functional>
20 #include <vector>
21 
22 #include "absl/status/status.h"
23 #include "absl/status/statusor.h"
24 
27 #include <grpc/event_engine/port.h>
29 
30 // TODO(vigneshbabu): Define the Endpoint::Write metrics collection system
31 namespace grpc_event_engine {
32 namespace experimental {
33 
74 class EventEngine {
75  public:
80  using Duration = std::chrono::duration<int64_t, std::nano>;
87  class Closure {
88  public:
89  Closure() = default;
90  // Closure's are an interface, and thus non-copyable.
91  Closure(const Closure&) = delete;
92  Closure& operator=(const Closure&) = delete;
93  // Polymorphic type => virtual destructor
94  virtual ~Closure() = default;
95  // Run the contained code.
96  virtual void Run() = 0;
97  };
102  struct TaskHandle {
104  };
110  };
119  public:
120  static constexpr socklen_t MAX_SIZE_BYTES = 128;
121 
122  ResolvedAddress(const sockaddr* address, socklen_t size);
123  ResolvedAddress() = default;
124  ResolvedAddress(const ResolvedAddress&) = default;
125  const struct sockaddr* address() const;
126  socklen_t size() const;
127 
128  private:
130  socklen_t size_ = 0;
131  };
132 
141  class Endpoint {
142  public:
145  virtual ~Endpoint() = default;
150  struct ReadArgs {
151  // A suggestion to the endpoint implementation to read at-least the
152  // specified number of bytes over the network connection before marking
153  // the endpoint read operation as complete. gRPC may use this argument
154  // to minimize the number of endpoint read API calls over the lifetime
155  // of a connection.
157  };
175  virtual void Read(std::function<void(absl::Status)> on_read,
176  SliceBuffer* buffer, const ReadArgs* args) = 0;
181  struct WriteArgs {
182  // Represents private information that may be passed by gRPC for
183  // select endpoints expected to be used only within google.
184  void* google_specific = nullptr;
185  // A suggestion to the endpoint implementation to group data to be written
186  // into frames of the specified max_frame_size. gRPC may use this
187  // argument to dynamically control the max sizes of frames sent to a
188  // receiver in response to high receiver memory pressure.
190  };
209  virtual void Write(std::function<void(absl::Status)> on_writable,
210  SliceBuffer* data, const WriteArgs* args) = 0;
213  virtual const ResolvedAddress& GetPeerAddress() const = 0;
214  virtual const ResolvedAddress& GetLocalAddress() const = 0;
215  };
216 
223  using OnConnectCallback =
224  std::function<void(absl::StatusOr<std::unique_ptr<Endpoint>>)>;
225 
228  class Listener {
229  public:
231  using AcceptCallback = std::function<void(
232  std::unique_ptr<Endpoint>, MemoryAllocator memory_allocator)>;
233  virtual ~Listener() = default;
239  virtual absl::StatusOr<int> Bind(const ResolvedAddress& addr) = 0;
240  virtual absl::Status Start() = 0;
241  };
242 
259  std::function<void(absl::Status)> on_shutdown,
260  const EndpointConfig& config,
261  std::unique_ptr<MemoryAllocatorFactory> memory_allocator_factory) = 0;
274  const ResolvedAddress& addr,
275  const EndpointConfig& args,
276  MemoryAllocator memory_allocator,
277  Duration timeout) = 0;
278 
287  virtual bool CancelConnect(ConnectionHandle handle) = 0;
289  class DNSResolver {
290  public:
294  };
300  };
302  struct SRVRecord {
304  int port = 0;
305  int priority = 0;
306  int weight = 0;
307  };
310  using LookupHostnameCallback =
311  std::function<void(absl::StatusOr<std::vector<ResolvedAddress>>)>;
313  using LookupSRVCallback =
314  std::function<void(absl::StatusOr<std::vector<SRVRecord>>)>;
317 
318  virtual ~DNSResolver() = default;
319 
334  absl::string_view default_port,
335  Duration timeout) = 0;
340  virtual LookupTaskHandle LookupSRV(LookupSRVCallback on_resolve,
342  Duration timeout) = 0;
347  virtual LookupTaskHandle LookupTXT(LookupTXTCallback on_resolve,
349  Duration timeout) = 0;
355  virtual bool CancelLookup(LookupTaskHandle handle) = 0;
356  };
357 
365  virtual ~EventEngine() = default;
366 
367  // TODO(nnoble): consider whether we can remove this method before we
368  // de-experimentalize this API.
369  virtual bool IsWorkerThread() = 0;
370 
373  virtual std::unique_ptr<DNSResolver> GetDNSResolver(
375 
380  virtual void Run(Closure* closure) = 0;
390  virtual void Run(std::function<void()> closure) = 0;
396  virtual TaskHandle RunAfter(Duration when, Closure* closure) = 0;
408  virtual TaskHandle RunAfter(Duration when, std::function<void()> closure) = 0;
422  virtual bool Cancel(TaskHandle handle) = 0;
423 };
424 
435  std::function<std::unique_ptr<EventEngine>()> factory);
436 
438 std::unique_ptr<EventEngine> CreateEventEngine();
439 
440 } // namespace experimental
441 } // namespace grpc_event_engine
442 
443 #endif // GRPC_EVENT_ENGINE_EVENT_ENGINE_H
grpc_event_engine::experimental::EventEngine::Endpoint::WriteArgs::max_frame_size
int64_t max_frame_size
Definition: event_engine.h:189
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupTXT
virtual LookupTaskHandle LookupTXT(LookupTXTCallback on_resolve, absl::string_view name, Duration timeout)=0
grpc_event_engine::experimental::EventEngine::Listener::Start
virtual absl::Status Start()=0
grpc_event_engine::experimental::EventEngine::Closure::Closure
Closure()=default
grpc_event_engine::experimental::EventEngine::DNSResolver::ResolverOptions::dns_server
std::string dns_server
Definition: event_engine.h:299
grpc_event_engine::experimental::EventEngine::Listener::AcceptCallback
std::function< void(std::unique_ptr< Endpoint >, MemoryAllocator memory_allocator)> AcceptCallback
Called when the listener has accepted a new client connection.
Definition: event_engine.h:232
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
grpc_event_engine::experimental::SetDefaultEventEngineFactory
void SetDefaultEventEngineFactory(std::function< std::unique_ptr< EventEngine >()> factory)
Definition: event_engine.cc:34
grpc_event_engine::experimental::EventEngine::~EventEngine
virtual ~EventEngine()=default
grpc_event_engine::experimental::EventEngine::Run
virtual void Run(Closure *closure)=0
options
double_dict options[]
Definition: capstone_test.c:55
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupHostname
virtual LookupTaskHandle LookupHostname(LookupHostnameCallback on_resolve, absl::string_view name, absl::string_view default_port, Duration timeout)=0
grpc_event_engine::experimental::EventEngine
Definition: event_engine.h:74
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupSRVCallback
std::function< void(absl::StatusOr< std::vector< SRVRecord > >)> LookupSRVCallback
Called with a collection of SRV records.
Definition: event_engine.h:314
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::host
std::string host
Definition: event_engine.h:303
grpc_event_engine::experimental::EventEngine::ResolvedAddress::size
socklen_t size() const
Definition: resolved_address.cc:38
grpc_event_engine::experimental::EventEngine::ResolvedAddress::size_
socklen_t size_
Definition: event_engine.h:130
grpc_event_engine::experimental::EventEngine::Endpoint::GetLocalAddress
virtual const ResolvedAddress & GetLocalAddress() const =0
grpc_event_engine::experimental::EventEngine::Cancel
virtual bool Cancel(TaskHandle handle)=0
grpc_event_engine::experimental::EventEngine::Endpoint
Definition: event_engine.h:141
grpc_event_engine::experimental::EventEngine::ResolvedAddress::ResolvedAddress
ResolvedAddress()=default
setup.name
name
Definition: setup.py:542
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::weight
int weight
Definition: event_engine.h:306
grpc_event_engine::experimental::EventEngine::RunAfter
virtual TaskHandle RunAfter(Duration when, Closure *closure)=0
grpc_event_engine::experimental::EventEngine::TaskHandle
Definition: event_engine.h:102
grpc_event_engine::experimental::EventEngine::Closure::operator=
Closure & operator=(const Closure &)=delete
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupTaskHandle::keys
intptr_t keys[2]
Definition: event_engine.h:293
grpc_event_engine::experimental::EndpointConfig
Definition: endpoint_config.h:31
grpc_event_engine::experimental::EventEngine::Endpoint::Write
virtual void Write(std::function< void(absl::Status)> on_writable, SliceBuffer *data, const WriteArgs *args)=0
grpc_event_engine::experimental::EventEngine::ResolvedAddress
Definition: event_engine.h:118
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupHostnameCallback
std::function< void(absl::StatusOr< std::vector< ResolvedAddress > >)> LookupHostnameCallback
Definition: event_engine.h:311
port.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
on_read
static grpc_closure on_read
Definition: bad_server_response_test.cc:88
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::priority
int priority
Definition: event_engine.h:305
grpc_event_engine::experimental::EventEngine::Endpoint::WriteArgs
Definition: event_engine.h:181
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupTXTCallback
std::function< void(absl::StatusOr< std::string >)> LookupTXTCallback
Called with the result of a TXT record lookup.
Definition: event_engine.h:316
grpc_event_engine::experimental::EventEngine::ConnectionHandle
Definition: event_engine.h:108
grpc_event_engine::experimental::EventEngine::Listener::Bind
virtual absl::StatusOr< int > Bind(const ResolvedAddress &addr)=0
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
grpc_event_engine::experimental::EventEngine::DNSResolver
Provides asynchronous resolution.
Definition: event_engine.h:289
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
grpc_event_engine::experimental::EventEngine::Endpoint::ReadArgs
Definition: event_engine.h:150
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupTaskHandle
Task handle for DNS Resolution requests.
Definition: event_engine.h:292
grpc_event_engine::experimental::EventEngine::Endpoint::~Endpoint
virtual ~Endpoint()=default
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord
DNS SRV record type.
Definition: event_engine.h:302
grpc_event_engine::experimental::EventEngine::Listener
Definition: event_engine.h:228
on_connect
void on_connect(uv_connect_t *req, int status)
Definition: libuv/docs/code/dns/main.c:32
grpc_event_engine::experimental::EventEngine::Closure::~Closure
virtual ~Closure()=default
grpc_event_engine::experimental::EventEngine::Closure::Run
virtual void Run()=0
grpc_event_engine::experimental::EventEngine::Endpoint::GetPeerAddress
virtual const ResolvedAddress & GetPeerAddress() const =0
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::port
int port
Definition: event_engine.h:304
grpc_event_engine::experimental::EventEngine::OnConnectCallback
std::function< void(absl::StatusOr< std::unique_ptr< Endpoint > >)> OnConnectCallback
Definition: event_engine.h:224
grpc_event_engine::experimental::EventEngine::Closure
Definition: event_engine.h:87
grpc_event_engine::experimental::EventEngine::GetDNSResolver
virtual std::unique_ptr< DNSResolver > GetDNSResolver(const DNSResolver::ResolverOptions &options)=0
slice_buffer.h
grpc_event_engine::experimental::EventEngine::Endpoint::WriteArgs::google_specific
void * google_specific
Definition: event_engine.h:184
grpc_event_engine::experimental::EventEngine::IsWorkerThread
virtual bool IsWorkerThread()=0
grpc_event_engine::experimental::EventEngine::ConnectionHandle::keys
intptr_t keys[2]
Definition: event_engine.h:109
grpc_event_engine::experimental::EventEngine::ResolvedAddress::address
const struct sockaddr * address() const
Definition: resolved_address.cc:34
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
grpc_event_engine::experimental::EventEngine::ResolvedAddress::address_
char address_[MAX_SIZE_BYTES]
Definition: event_engine.h:129
on_accept
static void on_accept(void *arg, grpc_endpoint *endpoint, grpc_pollset *, grpc_tcp_server_acceptor *acceptor)
Definition: http_proxy_fixture.cc:567
grpc_event_engine::experimental::SliceBuffer
Definition: include/grpc/event_engine/slice_buffer.h:51
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::experimental::EventEngine::DNSResolver::CancelLookup
virtual bool CancelLookup(LookupTaskHandle handle)=0
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupSRV
virtual LookupTaskHandle LookupSRV(LookupSRVCallback on_resolve, absl::string_view name, Duration timeout)=0
grpc_event_engine::experimental::EventEngine::DNSResolver::~DNSResolver
virtual ~DNSResolver()=default
grpc_event_engine::experimental::EventEngine::Connect
virtual ConnectionHandle Connect(OnConnectCallback on_connect, const ResolvedAddress &addr, const EndpointConfig &args, MemoryAllocator memory_allocator, Duration timeout)=0
closure
Definition: proxy.cc:59
config_s
Definition: bloaty/third_party/zlib/deflate.c:120
grpc_event_engine::experimental::EventEngine::ResolvedAddress::MAX_SIZE_BYTES
static constexpr socklen_t MAX_SIZE_BYTES
Definition: event_engine.h:120
handle
static csh handle
Definition: test_arm_regression.c:16
memory_allocator.h
grpc_event_engine::experimental::EventEngine::Endpoint::ReadArgs::read_hint_bytes
int64_t read_hint_bytes
Definition: event_engine.h:156
grpc_event_engine::experimental::EventEngine::TaskHandle::keys
intptr_t keys[2]
Definition: event_engine.h:103
Duration
Definition: bloaty/third_party/protobuf/src/google/protobuf/duration.pb.h:69
grpc_event_engine::experimental::CreateEventEngine
std::unique_ptr< EventEngine > CreateEventEngine()
Create an EventEngine using the default factory.
Definition: event_engine.cc:40
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
endpoint_config.h
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
grpc_event_engine::experimental::EventEngine::CancelConnect
virtual bool CancelConnect(ConnectionHandle handle)=0
grpc_event_engine::experimental::EventEngine::Listener::~Listener
virtual ~Listener()=default
grpc_event_engine::experimental::EventEngine::DNSResolver::ResolverOptions
Optional configuration for DNSResolvers.
Definition: event_engine.h:296
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
grpc_event_engine::experimental::EventEngine::Endpoint::Read
virtual void Read(std::function< void(absl::Status)> on_read, SliceBuffer *buffer, const ReadArgs *args)=0
grpc_event_engine::experimental::EventEngine::CreateListener
virtual absl::StatusOr< std::unique_ptr< Listener > > CreateListener(Listener::AcceptCallback on_accept, std::function< void(absl::Status)> on_shutdown, const EndpointConfig &config, std::unique_ptr< MemoryAllocatorFactory > memory_allocator_factory)=0
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:16