Go to the documentation of this file.
14 #ifndef GRPC_EVENT_ENGINE_EVENT_ENGINE_H
15 #define GRPC_EVENT_ENGINE_EVENT_ENGINE_H
28 #include <initializer_list>
34 #include "absl/functional/any_invocable.h"
35 #include "absl/status/status.h"
36 #include "absl/status/statusor.h"
37 #include "absl/types/span.h"
42 namespace experimental {
112 class EventEngine :
public std::enable_shared_from_this<EventEngine>,
119 using Duration = std::chrono::duration<int64_t, std::nano>;
136 virtual void Run() = 0;
167 const struct sockaddr*
address()
const;
168 socklen_t
size()
const;
211 int64_t read_hint_bytes_ = 1;
263 virtual bool IsSet(
size_t key)
const = 0;
273 std::initializer_list<WriteEvent> requested_events,
276 on_event_(
std::move(on_event)) {
277 for (
auto event : requested_events) {
278 requested_events_mask_.set(
static_cast<int>(event));
283 return requested_metrics_;
287 return requested_events_mask_.test(
static_cast<int>(event));
291 return requested_events_mask_;
299 std::shared_ptr<MetricsSet> requested_metrics_;
318 : metrics_sink_(std::move(other.metrics_sink_)),
319 google_specific_(other.google_specific_),
320 max_frame_size_(other.max_frame_size_) {
321 other.google_specific_ =
nullptr;
325 if (
this != &other) {
326 metrics_sink_ = std::move(other.metrics_sink_);
327 google_specific_ = other.google_specific_;
328 other.google_specific_ =
nullptr;
329 max_frame_size_ = other.max_frame_size_;
336 auto sink = std::move(metrics_sink_);
337 metrics_sink_.reset();
344 metrics_sink_ = std::move(sink);
352 return google_specific_;
356 return std::exchange(google_specific_,
nullptr);
360 google_specific_ = pointer;
374 std::optional<WriteEventSink> metrics_sink_;
375 void* google_specific_ =
nullptr;
376 int64_t max_frame_size_ = 1024 * 1024;
391 size_t key)
const = 0;
395 absl::string_view name)
const = 0;
398 absl::Span<const size_t> keys)
const = 0;
440 absl::AnyInvocable<void(absl::StatusOr<std::unique_ptr<Endpoint>>)>;
478 std::unique_ptr<MemoryAllocatorFactory> memory_allocator_factory) = 0;
530 absl::AnyInvocable<void(absl::StatusOr<std::vector<ResolvedAddress>>)>;
533 absl::AnyInvocable<void(absl::StatusOr<std::vector<SRVRecord>>)>;
536 absl::AnyInvocable<void(absl::StatusOr<std::vector<std::string>>)>;
551 absl::string_view name,
552 absl::string_view default_port) = 0;
558 absl::string_view name) = 0;
564 absl::string_view name) = 0;
585 virtual absl::StatusOr<std::unique_ptr<DNSResolver>>
GetDNSResolver(
610 virtual void Run(absl::AnyInvocable<
void()> closure) = 0;
638 absl::AnyInvocable<
void()> closure) = 0;
661 absl::AnyInvocable<std::shared_ptr<EventEngine>()> factory);
727 template <
typename Sink>
732 template <
typename Sink>
744 #endif // GRPC_EVENT_ENGINE_EVENT_ENGINE_H
absl::AnyInvocable< void(absl::StatusOr< std::unique_ptr< Endpoint > >)> OnConnectCallback
Called when a new connection is established.
Definition: event_engine.h:440
WriteEvent
Definition: write_event.h:23
virtual ~EventEngine()=default
At time of destruction, the EventEngine must have no active responsibilities.
std::ostream & operator<<(std::ostream &out, const EventEngine::TaskHandle &handle)
void ShutdownDefaultEventEngine()
Resets gRPC to use one of the default internal EventEngines for all new GetDefaultEventEngine request...
virtual std::optional< size_t > GetMetricKey(absl::string_view name) const =0
Returns the key of the write metric with the given name.
An output WriteMetric consists of a key and a value.
Definition: event_engine.h:245
A handle to a cancellable connection attempt.
Definition: event_engine.h:149
std::bitset< static_cast< int >(WriteEvent::kCount)> WriteEventSet
Definition: event_engine.h:256
std::optional< WriteEventSink > TakeMetricsSink()
Definition: event_engine.h:335
std::shared_ptr< EventEngine > GetDefaultEventEngine()
Returns the default EventEngine instance.
size_t key
Definition: event_engine.h:246
static constexpr socklen_t MAX_SIZE_BYTES
Definition: event_engine.h:162
const std::shared_ptr< MetricsSet > & requested_metrics() const
Definition: event_engine.h:282
virtual TaskHandle RunAfter(Duration when, Closure *closure)=0
Synonymous with scheduling an alarm to run after duration when.
WriteEventCallback TakeEventCallback()
Takes the callback.
Definition: event_engine.h:296
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:448
The EventEngine Interface.
Definition: event_engine.h:112
virtual ~Endpoint()=default
Shuts down all connections and invokes all pending read or write callbacks with an error status.
virtual bool IsWorkerThread()=0
virtual ~DNSResolver()=default
virtual void LookupHostname(LookupHostnameCallback on_resolve, absl::string_view name, absl::string_view default_port)=0
Asynchronously resolve an address.
void AbslStringify(Sink &out, const EventEngine::ConnectionHandle &handle)
Definition: event_engine.h:728
void set_metrics_sink(WriteEventSink sink)
Definition: event_engine.h:343
Definition: event_engine.h:270
absl::AnyInvocable< void(absl::StatusOr< std::vector< SRVRecord > >)> LookupSRVCallback
Called with a collection of SRV records.
Definition: event_engine.h:533
absl::AnyInvocable< void(WriteEvent, absl::Time, std::vector< WriteMetric >) const > WriteEventCallback
Definition: event_engine.h:253
void * TakeDeprecatedAndDiscouragedGoogleSpecificPointer()
Definition: event_engine.h:355
int port
Definition: event_engine.h:523
virtual bool Write(absl::AnyInvocable< void(absl::Status)> on_writable, SliceBuffer *data, WriteArgs args)=0
Writes data out on the connection.
virtual absl::StatusOr< int > Bind(const ResolvedAddress &addr)=0
Bind an address/port to this Listener.
virtual bool IsSet(size_t key) const =0
Definition: event_engine.h:259
const grpc_arg_pointer_vtable * grpc_event_engine_arg_vtable(void)
Fetch a vtable for a grpc_channel_arg that points to a grpc_event_engine.
std::string host
Definition: event_engine.h:522
intptr_t keys[2]
Definition: event_engine.h:150
bool has_metrics_sink() const
Definition: event_engine.h:341
void EventEngineFactoryReset()
[DEPRECATED] Reset gRPC's EventEngine factory to the built-in default.
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:530
A struct representing optional arguments that may be provided to an EventEngine Endpoint Write API ca...
Definition: event_engine.h:308
Definition: grpc_types.h:83
virtual bool CancelConnect(ConnectionHandle handle)=0
Request cancellation of a connection attempt.
void SetDeprecatedAndDiscouragedGoogleSpecificPointer(void *pointer)
Definition: event_engine.h:359
virtual std::shared_ptr< MetricsSet > GetMetricsSet(absl::Span< const size_t > keys) const =0
Returns a MetricsSet with all the keys from keys set.
void set_max_frame_size(int64_t max_frame_size)
Definition: event_engine.h:369
bool operator!=(const EventEngine::TaskHandle &lhs, const EventEngine::TaskHandle &rhs)
int64_t value
Definition: event_engine.h:247
WriteArgs & operator=(const WriteArgs &)=delete
Optional configuration for DNSResolvers.
Definition: event_engine.h:515
WriteArgs(WriteArgs &&other) noexcept
Definition: event_engine.h:317
virtual std::shared_ptr< TelemetryInfo > GetTelemetryInfo() const =0
DNS SRV record type.
Definition: event_engine.h:521
virtual void LookupTXT(LookupTXTCallback on_resolve, absl::string_view name)=0
Asynchronously perform a TXT record lookup.
Collection of parameters used to configure client and server endpoints.
Definition: endpoint_config.h:31
Thin wrapper around a platform-specific sockaddr type.
Definition: event_engine.h:160
Listens for incoming connection requests from gRPC clients and initiates request processing once conn...
Definition: event_engine.h:444
A Wrapper around grpc_slice_buffer pointer.
Definition: slice_buffer.h:50
std::string FormatHandleString(uint64_t key1, uint64_t key2)
A custom closure type for EventEngine task execution.
Definition: event_engine.h:127
Definition: event_engine.h:379
ResolvedAddress()=default
WriteEventSink(std::shared_ptr< MetricsSet > requested_metrics, std::initializer_list< WriteEvent > requested_events, WriteEventCallback on_event)
Definition: event_engine.h:272
const struct sockaddr * address() const
virtual absl::Status Start()=0
Closure & operator=(const Closure &)=delete
virtual ~Closure()=default
virtual void Run(Closure *closure)=0
Asynchronously executes a task as soon as possible.
WriteEventSet requested_events_mask() const
Definition: event_engine.h:290
std::string dns_server
If empty, default DNS servers will be used.
Definition: event_engine.h:518
int weight
Definition: event_engine.h:525
void SetEventEngineFactory(absl::AnyInvocable< std::shared_ptr< EventEngine >()> factory)
[DEPRECATED] Replace gRPC's default EventEngine factory.
virtual const ResolvedAddress & GetPeerAddress() const =0
Returns an address in the format described in DNSResolver.
virtual ~TelemetryInfo()=default
Provides asynchronous resolution.
Definition: event_engine.h:512
void * GetDeprecatedAndDiscouragedGoogleSpecificPointer()
Definition: event_engine.h:351
virtual bool Read(absl::AnyInvocable< void(absl::Status)> on_read, SliceBuffer *buffer, ReadArgs args)=0
Reads data from the Endpoint.
Definition: extensible.h:25
Represents a scheduled task.
Definition: event_engine.h:142
virtual bool Cancel(TaskHandle handle)=0
Request cancellation of a task.
void SetDefaultEventEngine(std::shared_ptr< EventEngine > engine)
Set the default EventEngine instance, which will be used throughout gRPC.
std::shared_ptr< EventEngine > CreateEventEngine()
Create a new EventEngine instance.
A struct representing optional arguments that may be provided to an EventEngine Endpoint Read API cal...
Definition: event_engine.h:192
WriteArgs & operator=(WriteArgs &&other) noexcept
Definition: event_engine.h:324
int priority
Definition: event_engine.h:524
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.
void set_read_hint_bytes(int64_t read_hint_bytes)
Definition: event_engine.h:205
Definition: async_unary_call.h:410
static const GRPC_DLL TaskHandle kInvalid
Definition: event_engine.h:144
Definition: endpoint_config.h:24
int64_t max_frame_size() const
Definition: event_engine.h:367
absl::AnyInvocable< void(absl::StatusOr< std::vector< std::string > >)> LookupTXTCallback
Called with the result of a TXT record lookup.
Definition: event_engine.h:536
bool requested_event(WriteEvent event) const
Definition: event_engine.h:286
static const GRPC_DLL ConnectionHandle kInvalid
Definition: event_engine.h:151
int64_t read_hint_bytes() const
Definition: event_engine.h:208
One end of a connection between a gRPC client and server.
Definition: event_engine.h:183
virtual std::shared_ptr< MetricsSet > GetFullMetricsSet() const =0
Returns a MetricsSet with all supported keys set.
Definition: memory_allocator.h:33
virtual ~MetricsSet()=default
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.
virtual std::optional< absl::string_view > GetMetricName(size_t key) const =0
Returns the name of the write metric with the given key.
intptr_t keys[2]
Definition: event_engine.h:143
virtual std::vector< size_t > AllWriteMetrics() const =0
Returns the list of write metrics that the endpoint supports.
virtual ~Listener()=default
::absl::Status Status
Definition: config_protobuf.h:107
virtual void LookupSRV(LookupSRVCallback on_resolve, absl::string_view name)=0
Asynchronously perform an SRV record lookup.
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.
std::chrono::duration< int64_t, std::nano > Duration
A duration between two events.
Definition: event_engine.h:119
virtual const ResolvedAddress & GetLocalAddress() const =0
bool operator==(const EventEngine::TaskHandle &lhs, const EventEngine::TaskHandle &rhs)
ReadArgs & operator=(const ReadArgs &)=default