GRPC C++  1.64.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 
17 #include <vector>
18 
19 #include "absl/functional/any_invocable.h"
20 #include "absl/status/status.h"
21 #include "absl/status/statusor.h"
22 
26 #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 
102 class EventEngine : public std::enable_shared_from_this<EventEngine>,
103  public Extensible {
104  public:
109  using Duration = std::chrono::duration<int64_t, std::nano>;
116 
117  class Closure {
118  public:
119  Closure() = default;
120  // Closure's are an interface, and thus non-copyable.
121  Closure(const Closure&) = delete;
122  Closure& operator=(const Closure&) = delete;
123  // Polymorphic type => virtual destructor
124  virtual ~Closure() = default;
125  // Run the contained code.
126  virtual void Run() = 0;
127  };
132  struct TaskHandle {
133  intptr_t keys[2];
135  friend bool operator==(const TaskHandle& lhs, const TaskHandle& rhs);
136  friend bool operator!=(const TaskHandle& lhs, const TaskHandle& rhs);
137  };
142  intptr_t keys[2];
144  friend bool operator==(const ConnectionHandle& lhs,
145  const ConnectionHandle& rhs);
146  friend bool operator!=(const ConnectionHandle& lhs,
147  const ConnectionHandle& rhs);
148  };
157  public:
158  static constexpr socklen_t MAX_SIZE_BYTES = 128;
159 
160  ResolvedAddress(const sockaddr* address, socklen_t size);
161  ResolvedAddress() = default;
162  ResolvedAddress(const ResolvedAddress&) = default;
163  const struct sockaddr* address() const;
164  socklen_t size() const;
165 
166  private:
167  char address_[MAX_SIZE_BYTES] = {};
168  socklen_t size_ = 0;
169  };
170 
179  class Endpoint : public Extensible {
180  public:
183  virtual ~Endpoint() = default;
188  struct ReadArgs {
189  // A suggestion to the endpoint implementation to read at-least the
190  // specified number of bytes over the network connection before marking
191  // the endpoint read operation as complete. gRPC may use this argument
192  // to minimize the number of endpoint read API calls over the lifetime
193  // of a connection.
195  };
216  virtual bool Read(absl::AnyInvocable<void(absl::Status)> on_read,
217  SliceBuffer* buffer, const ReadArgs* args) = 0;
222  struct WriteArgs {
223  // Represents private information that may be passed by gRPC for
224  // select endpoints expected to be used only within google.
225  void* google_specific = nullptr;
226  // A suggestion to the endpoint implementation to group data to be written
227  // into frames of the specified max_frame_size. gRPC may use this
228  // argument to dynamically control the max sizes of frames sent to a
229  // receiver in response to high receiver memory pressure.
230  int64_t max_frame_size;
231  };
252  virtual bool Write(absl::AnyInvocable<void(absl::Status)> on_writable,
253  SliceBuffer* data, const WriteArgs* args) = 0;
256  virtual const ResolvedAddress& GetPeerAddress() const = 0;
257  virtual const ResolvedAddress& GetLocalAddress() const = 0;
258  };
259 
266  using OnConnectCallback =
267  absl::AnyInvocable<void(absl::StatusOr<std::unique_ptr<Endpoint>>)>;
268 
271  class Listener : public Extensible {
272  public:
274  using AcceptCallback = absl::AnyInvocable<void(
275  std::unique_ptr<Endpoint>, MemoryAllocator memory_allocator)>;
276  virtual ~Listener() = default;
282  virtual absl::StatusOr<int> Bind(const ResolvedAddress& addr) = 0;
283  virtual absl::Status Start() = 0;
284  };
285 
301  virtual absl::StatusOr<std::unique_ptr<Listener>> CreateListener(
302  Listener::AcceptCallback on_accept,
303  absl::AnyInvocable<void(absl::Status)> on_shutdown,
304  const EndpointConfig& config,
305  std::unique_ptr<MemoryAllocatorFactory> memory_allocator_factory) = 0;
317  virtual ConnectionHandle Connect(OnConnectCallback on_connect,
318  const ResolvedAddress& addr,
319  const EndpointConfig& args,
320  MemoryAllocator memory_allocator,
321  Duration timeout) = 0;
322 
331  virtual bool CancelConnect(ConnectionHandle handle) = 0;
339  class DNSResolver {
340  public:
345  std::string dns_server;
346  };
348  struct SRVRecord {
349  std::string host;
350  int port = 0;
351  int priority = 0;
352  int weight = 0;
353  };
356  using LookupHostnameCallback =
357  absl::AnyInvocable<void(absl::StatusOr<std::vector<ResolvedAddress>>)>;
359  using LookupSRVCallback =
360  absl::AnyInvocable<void(absl::StatusOr<std::vector<SRVRecord>>)>;
362  using LookupTXTCallback =
363  absl::AnyInvocable<void(absl::StatusOr<std::vector<std::string>>)>;
364 
365  virtual ~DNSResolver() = default;
366 
377  virtual void LookupHostname(LookupHostnameCallback on_resolve,
378  absl::string_view name,
379  absl::string_view default_port) = 0;
384  virtual void LookupSRV(LookupSRVCallback on_resolve,
385  absl::string_view name) = 0;
390  virtual void LookupTXT(LookupTXTCallback on_resolve,
391  absl::string_view name) = 0;
392  };
393 
401  virtual ~EventEngine() = default;
402 
403  // TODO(nnoble): consider whether we can remove this method before we
404  // de-experimentalize this API.
405  virtual bool IsWorkerThread() = 0;
406 
412  virtual absl::StatusOr<std::unique_ptr<DNSResolver>> GetDNSResolver(
413  const DNSResolver::ResolverOptions& options) = 0;
414 
424  virtual void Run(Closure* closure) = 0;
437  virtual void Run(absl::AnyInvocable<void()> closure) = 0;
449  virtual TaskHandle RunAfter(Duration when, Closure* closure) = 0;
464  virtual TaskHandle RunAfter(Duration when,
465  absl::AnyInvocable<void()> closure) = 0;
474  virtual bool Cancel(TaskHandle handle) = 0;
475 };
476 
487  absl::AnyInvocable<std::unique_ptr<EventEngine>()> factory);
488 
497 std::unique_ptr<EventEngine> CreateEventEngine();
498 
499 } // namespace experimental
500 } // namespace grpc_event_engine
501 
502 #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:267
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:141
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:222
grpc_event_engine::experimental::EventEngine::ResolvedAddress::MAX_SIZE_BYTES
static constexpr socklen_t MAX_SIZE_BYTES
Definition: event_engine.h:158
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:275
grpc_event_engine::experimental::EventEngine
The EventEngine Interface.
Definition: event_engine.h:102
grpc_event_engine::experimental::EventEngine::Endpoint::ReadArgs::read_hint_bytes
int64_t read_hint_bytes
Definition: event_engine.h:194
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:360
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::port
int port
Definition: event_engine.h:350
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:188
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:349
grpc_event_engine::experimental::EventEngine::ConnectionHandle::keys
intptr_t keys[2]
Definition: event_engine.h:142
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:357
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:342
grpc_event_engine::experimental::EventEngine::Endpoint::WriteArgs::max_frame_size
int64_t max_frame_size
Definition: event_engine.h:230
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord
DNS SRV record type.
Definition: event_engine.h:348
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:156
grpc_event_engine::experimental::EventEngine::Listener
Listens for incoming connection requests from gRPC clients and initiates request processing once conn...
Definition: event_engine.h:271
grpc_event_engine::experimental::SliceBuffer
A Wrapper around grpc_slice_buffer pointer.
Definition: slice_buffer.h:52
grpc_event_engine::experimental::EventEngine::Closure
A custom closure type for EventEngine task execution.
Definition: event_engine.h:117
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:225
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:345
grpc_event_engine::experimental::EventEngine::DNSResolver::SRVRecord::weight
int weight
Definition: event_engine.h:352
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:339
grpc_event_engine::experimental::Extensible
Definition: extensible.h:25
grpc_event_engine::experimental::EventEngine::TaskHandle
Represents a scheduled task.
Definition: event_engine.h:132
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:351
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:134
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:363
grpc_event_engine::experimental::EventEngine::ConnectionHandle::kInvalid
static const GRPC_DLL ConnectionHandle kInvalid
Definition: event_engine.h:143
grpc_event_engine::experimental::EventEngine::Endpoint
One end of a connection between a gRPC client and server.
Definition: event_engine.h:179
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:34
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:133
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:106
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:109
grpc_event_engine::experimental::EventEngine::Endpoint::GetLocalAddress
virtual const ResolvedAddress & GetLocalAddress() const =0
port_platform.h