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