xref: /aosp_15_r20/external/grpc-grpc/src/core/ext/transport/chttp2/transport/ping_callbacks.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2023 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 
15 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_PING_CALLBACKS_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_PING_CALLBACKS_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include <algorithm>
24 #include <vector>
25 
26 #include "absl/container/flat_hash_map.h"
27 #include "absl/functional/any_invocable.h"
28 #include "absl/hash/hash.h"
29 #include "absl/random/bit_gen_ref.h"
30 #include "absl/types/optional.h"
31 
32 #include <grpc/event_engine/event_engine.h>
33 
34 #include "src/core/lib/debug/trace.h"
35 #include "src/core/lib/gprpp/time.h"
36 
37 extern grpc_core::TraceFlag grpc_ping_trace;
38 
39 namespace grpc_core {
40 
41 class Chttp2PingCallbacks {
42  public:
43   // One callback from OnPing/OnPingAck or the timeout.
44   using Callback = absl::AnyInvocable<void()>;
45 
46   // Request a ping (but one we don't need any notification for when it begins
47   // or ends).
RequestPing()48   void RequestPing() { ping_requested_ = true; }
49 
50   // Request a ping, and specify callbacks for when it begins and ends.
51   // on_start is invoked during the call to StartPing.
52   // on_ack is invoked during the call to AckPing.
53   void OnPing(Callback on_start, Callback on_ack);
54 
55   // Request a notification when *some* ping is acked:
56   // If there is no ping in flight, one will be scheduled and the callback
57   // will be invoked when it is acked. (ie as per OnPing([]{}, on_ack)).
58   // If there is a ping in flight, the callback will be invoked when the most
59   // recently sent ping is acked.
60   // on_ack is invoked during the call to AckPing.
61   void OnPingAck(Callback on_ack);
62 
63   // Write path: begin a ping.
64   // Uses bitgen to generate a randomized id for the ping.
65   // Sets started_new_ping_without_setting_timeout.
66   GRPC_MUST_USE_RESULT uint64_t StartPing(absl::BitGenRef bitgen);
67   bool AckPing(uint64_t id,
68                grpc_event_engine::experimental::EventEngine* event_engine);
69 
70   // Cancel all the ping callbacks.
71   // Sufficient state is maintained such that AckPing will still return true
72   // if a ping is acked after this call.
73   // No timeouts or start or ack callbacks previously scheduled will be invoked.
74   void CancelAll(grpc_event_engine::experimental::EventEngine* event_engine);
75 
76   // Return true if a ping needs to be started due to
77   // RequestPing/OnPing/OnPingAck.
ping_requested()78   bool ping_requested() const { return ping_requested_; }
79 
80   // Returns the number of pings currently in flight.
pings_inflight()81   size_t pings_inflight() const { return inflight_.size(); }
82 
83   // Returns true if a ping was started without setting a timeout yet.
started_new_ping_without_setting_timeout()84   bool started_new_ping_without_setting_timeout() const {
85     return started_new_ping_without_setting_timeout_;
86   }
87 
88   // Add a ping timeout for the most recently started ping.
89   // started_new_ping_without_setting_timeout must be set.
90   // Clears started_new_ping_without_setting_timeout.
91   // Returns the ping id of the ping the timeout was attached to if a timer was
92   // started, or nullopt otherwise.
93   absl::optional<uint64_t> OnPingTimeout(
94       Duration ping_timeout,
95       grpc_event_engine::experimental::EventEngine* event_engine,
96       Callback callback);
97 
98  private:
99   using CallbackVec = std::vector<Callback>;
100   struct InflightPing {
101     grpc_event_engine::experimental::EventEngine::TaskHandle on_timeout =
102         grpc_event_engine::experimental::EventEngine::TaskHandle::kInvalid;
103     CallbackVec on_ack;
104   };
105   absl::flat_hash_map<uint64_t, InflightPing> inflight_;
106   uint64_t most_recent_inflight_ = 0;
107   bool ping_requested_ = false;
108   bool started_new_ping_without_setting_timeout_ = false;
109   CallbackVec on_start_;
110   CallbackVec on_ack_;
111 };
112 
113 }  // namespace grpc_core
114 
115 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_PING_CALLBACKS_H
116