GRPC C++  1.62.0
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 <vector>
20 
21 #include "absl/functional/any_invocable.h"
22 #include "absl/status/status.h"
23 #include "absl/status/statusor.h"
24 
28 #include <grpc/event_engine/port.h>
30 
31 // TODO(vigneshbabu): Define the Endpoint::Write metrics collection system
32 namespace grpc_event_engine {
33 namespace experimental {
34 
103 class EventEngine : public std::enable_shared_from_this<EventEngine>,
104  public Extensible {
105  public:
110  using Duration = std::chrono::duration<int64_t, std::nano>;
117 
118  class Closure {
119  public:
120  Closure() = default;
121  // Closure's are an interface, and thus non-copyable.
122  Closure(const Closure&) = delete;
123  Closure& operator=(const Closure&) = delete;
124  // Polymorphic type => virtual destructor
125  virtual ~Closure() = default;
126  // Run the contained code.
127  virtual void Run() = 0;
128  };
133  struct TaskHandle {
134  intptr_t keys[2];
136  friend bool operator==(const TaskHandle& lhs, const TaskHandle& rhs);
137  friend bool operator!=(const TaskHandle& lhs, const TaskHandle& rhs);
138  };
143  intptr_t keys[2];
145  friend bool operator==(const ConnectionHandle& lhs,
146  const ConnectionHandle& rhs);
147  friend bool operator!=(const ConnectionHandle& lhs,
148  const ConnectionHandle& rhs);
149  };
158  public:
159  static constexpr socklen_t MAX_SIZE_BYTES = 128;
160 
161  ResolvedAddress(const sockaddr* address, socklen_t size);
162  ResolvedAddress() = default;
163  ResolvedAddress(const ResolvedAddress&) = default;
164  const struct sockaddr* address() const;
165  socklen_t size() const;
166 
167  private:
168  char address_[MAX_SIZE_BYTES] = {};
169  socklen_t size_ = 0;
170  };
171 
180  class Endpoint : public Extensible {
181  public:
184  virtual ~Endpoint() = default;
189  struct ReadArgs {
190  // A suggestion to the endpoint implementation to read at-least the
191  // specified number of bytes over the network connection before marking
192  // the endpoint read operation as complete. gRPC may use this argument
193  // to minimize the number of endpoint read API calls over the lifetime
194  // of a connection.
196  };
217  virtual bool Read(absl::AnyInvocable<void(absl::Status)> on_read,
218  SliceBuffer* buffer, const ReadArgs* args) = 0;
223  struct WriteArgs {
224  // Represents private information that may be passed by gRPC for
225  // select endpoints expected to be used only within google.
226  void* google_specific = nullptr;
227  // A suggestion to the endpoint implementation to group data to be written
228  // into frames of the specified max_frame_size. gRPC may use this
229  // argument to dynamically control the max sizes of frames sent to a
230  // receiver in response to high receiver memory pressure.
231  int64_t max_frame_size;
232  };
253  virtual bool Write(absl::AnyInvocable<void(absl::Status)> on_writable,
254  SliceBuffer* data, const WriteArgs* args) = 0;
257  virtual const ResolvedAddress& GetPeerAddress() const = 0;
258  virtual const ResolvedAddress& GetLocalAddress() const = 0;
259  };
260 
267  using OnConnectCallback =
268  absl::AnyInvocable<void(absl::StatusOr<std::unique_ptr<Endpoint>>)>;
269 
272  class Listener : public Extensible {
273  public:
275  using AcceptCallback = absl::AnyInvocable<void(
276  std::unique_ptr<Endpoint>, MemoryAllocator memory_allocator)>;
277  virtual ~Listener() = default;
283  virtual absl::StatusOr<int> Bind(const ResolvedAddress& addr) = 0;
284  virtual absl::Status Start() = 0;
285  };
286 
302  virtual absl::StatusOr<std::unique_ptr<Listener>> CreateListener(
303  Listener::AcceptCallback on_accept,
304  absl::AnyInvocable<void(absl::Status)> on_shutdown,
305  const EndpointConfig& config,
306  std::unique_ptr<MemoryAllocatorFactory> memory_allocator_factory) = 0;
318  virtual ConnectionHandle Connect(OnConnectCallback on_connect,
319  const ResolvedAddress& addr,
320  const EndpointConfig& args,
321  MemoryAllocator memory_allocator,
322  Duration timeout) = 0;
323 
332  virtual bool CancelConnect(ConnectionHandle handle) = 0;
340  class DNSResolver {
341  public:
346  std::string dns_server;
347  };
349  struct SRVRecord {
350  std::string host;
351  int port = 0;
352  int priority = 0;
353  int weight = 0;
354  };
357  using LookupHostnameCallback =
358  absl::AnyInvocable<void(absl::StatusOr<std::vector<ResolvedAddress>>)>;
360  using LookupSRVCallback =
361  absl::AnyInvocable<void(absl::StatusOr<std::vector<SRVRecord>>)>;
363  using LookupTXTCallback =
364  absl::AnyInvocable<void(absl::StatusOr<std::vector<std::string>>)>;
365 
366  virtual ~DNSResolver() = default;
367 
378  virtual void LookupHostname(LookupHostnameCallback on_resolve,
379  absl::string_view name,
380  absl::string_view default_port) = 0;
385  virtual void LookupSRV(LookupSRVCallback on_resolve,
386  absl::string_view name) = 0;
391  virtual void LookupTXT(LookupTXTCallback on_resolve,
392  absl::string_view name) = 0;
393  };
394 
402  virtual ~EventEngine() = default;
403 
404  // TODO(nnoble): consider whether we can remove this method before we
405  // de-experimentalize this API.
406  virtual bool IsWorkerThread() = 0;
407 
413  virtual absl::StatusOr<std::unique_ptr<DNSResolver>> GetDNSResolver(
414  const DNSResolver::ResolverOptions& options) = 0;
415 
425  virtual void Run(Closure* closure) = 0;
438  virtual void Run(absl::AnyInvocable<void()> closure) = 0;
447  virtual TaskHandle RunAfter(Duration when, Closure* closure) = 0;
462  virtual TaskHandle RunAfter(Duration when,
463  absl::AnyInvocable<void()> closure) = 0;
472  virtual bool Cancel(TaskHandle handle) = 0;
473 };
474 
485  absl::AnyInvocable<std::unique_ptr<EventEngine>()> factory);
486 
495 std::unique_ptr<EventEngine> CreateEventEngine();
496 
497 } // namespace experimental
498 } // namespace grpc_event_engine
499 
500 #endif // GRPC_EVENT_ENGINE_EVENT_ENGINE_H
grpc_event_engine::experimental::EventEngine::OnConnectCallback
absl::AnyInvocable< void(absl::StatusOr< std::unique_ptr< Endpoint > >)> OnConnectCallback
Called when a new connection is established.
Definition: event_engine.h:268
grpc_event_engine::experimental::EventEngine::~EventEngine
virtual ~EventEngine()=default
At time of destruction, the EventEngine must have no active responsibilities.
grpc_event_engine::experimental::EventEngine::ResolvedAddress::size
socklen_t size() const
grpc_event_engine::experimental::EventEngine::ConnectionHandle
A handle to a cancellable connection attempt.
Definition: event_engine.h:142
port.h
grpc_event_engine::experimental::EventEngine::Endpoint::WriteArgs
A struct representing optional arguments that may be provided to an EventEngine Endpoint Write API ca...
Definition: event_engine.h:223
grpc_event_engine::experimental::EventEngine::ResolvedAddress::MAX_SIZE_BYTES
static constexpr socklen_t MAX_SIZE_BYTES
Definition: event_engine.h:159
extensible.h
grpc_event_engine::experimental::EventEngine::RunAfter
virtual TaskHandle RunAfter(Duration when, Closure *closure)=0
Synonymous with scheduling an alarm to run after duration when.
grpc_event_engine::experimental::EventEngine::TaskHandle::operator==
friend bool operator==(const TaskHandle &lhs, const TaskHandle &rhs)
grpc_event_engine::experimental::EventEngine::Listener::AcceptCallback
absl::AnyInvocable< void(std::unique_ptr< Endpoint >, MemoryAllocator memory_allocator)> AcceptCallback
Called when the listener has accepted a new client connection.
Definition: event_engine.h:276
grpc_event_engine::experimental::EventEngine
The EventEngine Interface.
Definition: event_engine.h:103
grpc_event_engine::experimental::EventEngine::Endpoint::ReadArgs::read_hint_bytes
int64_t read_hint_bytes
Definition: event_engine.h:195
grpc_event_engine::experimental::EventEngine::Endpoint::~Endpoint
virtual ~Endpoint()=default
Shuts down all connections and invokes all pending read or write callbacks with an error status.
grpc_event_engine::experimental::EventEngine::IsWorkerThread
virtual bool IsWorkerThread()=0
grpc_event_engine::experimental::EventEngine::DNSResolver::~DNSResolver
virtual ~DNSResolver()=default
grpc_event_engine::experimental::EventEngine::Endpoint::Read
virtual bool Read(absl::AnyInvocable< void(absl::Status)> on_read, SliceBuffer *buffer, const ReadArgs *args)=0
Reads data from the Endpoint.
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupHostname
virtual void LookupHostname(LookupHostnameCallback on_resolve, absl::string_view name, absl::string_view default_port)=0
Asynchronously resolve an address.
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupSRVCallback
absl::AnyInvocable< void(absl::StatusOr< std::vector< SRVRecord > >)> LookupSRVCallback
Called with a collection of SRV records.
Definition: event_engine.h:361
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::port
int port
Definition: event_engine.h:351
grpc_event_engine::experimental::EventEngine::Endpoint::ReadArgs
A struct representing optional arguments that may be provided to an EventEngine Endpoint Read API cal...
Definition: event_engine.h:189
grpc_event_engine::experimental::EventEngine::Listener::Bind
virtual absl::StatusOr< int > Bind(const ResolvedAddress &addr)=0
Bind an address/port to this Listener.
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::host
std::string host
Definition: event_engine.h:350
grpc_event_engine::experimental::EventEngine::ConnectionHandle::keys
intptr_t keys[2]
Definition: event_engine.h:143
grpc_event_engine::experimental::EventEngine::ConnectionHandle::operator==
friend bool operator==(const ConnectionHandle &lhs, const ConnectionHandle &rhs)
grpc_event_engine::experimental::EventEngineFactoryReset
void EventEngineFactoryReset()
Reset gRPC's EventEngine factory to the built-in default.
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupHostnameCallback
absl::AnyInvocable< void(absl::StatusOr< std::vector< ResolvedAddress > >)> LookupHostnameCallback
Called with the collection of sockaddrs that were resolved from a given target address.
Definition: event_engine.h:358
grpc_event_engine::experimental::EventEngine::Endpoint::Write
virtual bool Write(absl::AnyInvocable< void(absl::Status)> on_writable, SliceBuffer *data, const WriteArgs *args)=0
Writes data out on the connection.
grpc_event_engine::experimental::EventEngine::CancelConnect
virtual bool CancelConnect(ConnectionHandle handle)=0
Request cancellation of a connection attempt.
grpc_event_engine::experimental::EventEngine::DNSResolver::ResolverOptions
Optional configuration for DNSResolvers.
Definition: event_engine.h:343
grpc_event_engine::experimental::EventEngine::Endpoint::WriteArgs::max_frame_size
int64_t max_frame_size
Definition: event_engine.h:231
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord
DNS SRV record type.
Definition: event_engine.h:349
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupTXT
virtual void LookupTXT(LookupTXTCallback on_resolve, absl::string_view name)=0
Asynchronously perform a TXT record lookup.
grpc_event_engine::experimental::EndpointConfig
Collection of parameters used to configure client and server endpoints.
Definition: endpoint_config.h:31
grpc_event_engine::experimental::EventEngine::ResolvedAddress
Thin wrapper around a platform-specific sockaddr type.
Definition: event_engine.h:157
grpc_event_engine::experimental::EventEngine::Listener
Listens for incoming connection requests from gRPC clients and initiates request processing once conn...
Definition: event_engine.h:272
grpc_event_engine::experimental::SliceBuffer
A Wrapper around grpc_slice_buffer pointer.
Definition: slice_buffer.h:53
grpc_event_engine::experimental::EventEngine::Closure
A custom closure type for EventEngine task execution.
Definition: event_engine.h:118
grpc_event_engine::experimental::EventEngine::Closure::Closure
Closure()=default
grpc_event_engine::experimental::EventEngine::ResolvedAddress::ResolvedAddress
ResolvedAddress()=default
grpc_event_engine::experimental::EventEngine::ResolvedAddress::address
const struct sockaddr * address() const
grpc_event_engine::experimental::EventEngine::Listener::Start
virtual absl::Status Start()=0
grpc_event_engine::experimental::EventEngine::Closure::operator=
Closure & operator=(const Closure &)=delete
grpc_event_engine::experimental::EventEngine::Closure::~Closure
virtual ~Closure()=default
grpc_event_engine::experimental::EventEngine::Run
virtual void Run(Closure *closure)=0
Asynchronously executes a task as soon as possible.
slice_buffer.h
grpc_event_engine::experimental::EventEngine::Endpoint::WriteArgs::google_specific
void * google_specific
Definition: event_engine.h:226
grpc_event_engine::experimental::EventEngine::DNSResolver::ResolverOptions::dns_server
std::string dns_server
If empty, default DNS servers will be used.
Definition: event_engine.h:346
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::weight
int weight
Definition: event_engine.h:353
grpc_event_engine::experimental::EventEngine::Endpoint::GetPeerAddress
virtual const ResolvedAddress & GetPeerAddress() const =0
Returns an address in the format described in DNSResolver.
grpc_event_engine::experimental::CreateEventEngine
std::unique_ptr< EventEngine > CreateEventEngine()
Create an EventEngine using the default factory.
grpc_event_engine::experimental::EventEngine::Closure::Run
virtual void Run()=0
grpc_event_engine::experimental::EventEngine::DNSResolver
Provides asynchronous resolution.
Definition: event_engine.h:340
grpc_event_engine::experimental::Extensible
Definition: extensible.h:25
grpc_event_engine::experimental::EventEngine::TaskHandle
Represents a scheduled task.
Definition: event_engine.h:133
grpc_event_engine::experimental::EventEngine::Cancel
virtual bool Cancel(TaskHandle handle)=0
Request cancellation of a task.
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::priority
int priority
Definition: event_engine.h:352
grpc_event_engine::experimental::EventEngine::GetDNSResolver
virtual absl::StatusOr< std::unique_ptr< DNSResolver > > GetDNSResolver(const DNSResolver::ResolverOptions &options)=0
Creates and returns an instance of a DNSResolver, optionally configured by the options struct.
grpc_event_engine::experimental::EventEngine::TaskHandle::kInvalid
static const GRPC_DLL TaskHandle kInvalid
Definition: event_engine.h:135
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupTXTCallback
absl::AnyInvocable< void(absl::StatusOr< std::vector< std::string > >)> LookupTXTCallback
Called with the result of a TXT record lookup.
Definition: event_engine.h:364
grpc_event_engine::experimental::EventEngine::ConnectionHandle::kInvalid
static const GRPC_DLL ConnectionHandle kInvalid
Definition: event_engine.h:144
grpc_event_engine::experimental::EventEngine::Endpoint
One end of a connection between a gRPC client and server.
Definition: event_engine.h:180
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
grpc_event_engine::experimental::EventEngine::Connect
virtual ConnectionHandle Connect(OnConnectCallback on_connect, const ResolvedAddress &addr, const EndpointConfig &args, MemoryAllocator memory_allocator, Duration timeout)=0
Creates a client network connection to a remote network listener.
grpc_event_engine::experimental::SetEventEngineFactory
void SetEventEngineFactory(absl::AnyInvocable< std::unique_ptr< EventEngine >()> factory)
Replace gRPC's default EventEngine factory.
memory_allocator.h
GRPC_DLL
#define GRPC_DLL
Definition: port_platform.h:127
grpc_event_engine::experimental::EventEngine::TaskHandle::keys
intptr_t keys[2]
Definition: event_engine.h:134
grpc_event_engine::experimental::EventEngine::ConnectionHandle::operator!=
friend bool operator!=(const ConnectionHandle &lhs, const ConnectionHandle &rhs)
endpoint_config.h
grpc_event_engine::experimental::EventEngine::Listener::~Listener
virtual ~Listener()=default
grpc::protobuf::util::Status
::absl::Status Status
Definition: config_protobuf.h:97
grpc_event_engine::experimental::EventEngine::TaskHandle::operator!=
friend bool operator!=(const TaskHandle &lhs, const TaskHandle &rhs)
grpc_event_engine::experimental::EventEngine::DNSResolver::LookupSRV
virtual void LookupSRV(LookupSRVCallback on_resolve, absl::string_view name)=0
Asynchronously perform an SRV record lookup.
grpc_event_engine::experimental::EventEngine::CreateListener
virtual absl::StatusOr< std::unique_ptr< Listener > > CreateListener(Listener::AcceptCallback on_accept, absl::AnyInvocable< void(absl::Status)> on_shutdown, const EndpointConfig &config, std::unique_ptr< MemoryAllocatorFactory > memory_allocator_factory)=0
Factory method to create a network listener / server.
grpc_event_engine::experimental::EventEngine::Duration
std::chrono::duration< int64_t, std::nano > Duration
A duration between two events.
Definition: event_engine.h:110
grpc_event_engine::experimental::EventEngine::Endpoint::GetLocalAddress
virtual const ResolvedAddress & GetLocalAddress() const =0
port_platform.h