xref: /aosp_15_r20/external/grpc-grpc/src/core/ext/transport/chttp2/transport/ping_rate_policy.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_RATE_POLICY_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_PING_RATE_POLICY_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <stddef.h>
21 
22 #include <iosfwd>
23 #include <string>
24 
25 #include "absl/types/variant.h"
26 
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/gprpp/time.h"
29 
30 namespace grpc_core {
31 
32 class Chttp2PingRatePolicy {
33  public:
34   explicit Chttp2PingRatePolicy(const ChannelArgs& args, bool is_client);
35 
36   static void SetDefaults(const ChannelArgs& args);
37 
38   struct SendGranted {
39     bool operator==(const SendGranted&) const { return true; }
40   };
41   struct TooManyRecentPings {
42     bool operator==(const TooManyRecentPings&) const { return true; }
43   };
44   struct TooSoon {
45     Duration next_allowed_ping_interval;
46     Timestamp last_ping;
47     Duration wait;
48     bool operator==(const TooSoon& other) const {
49       return next_allowed_ping_interval == other.next_allowed_ping_interval &&
50              last_ping == other.last_ping && wait == other.wait;
51     }
52   };
53   using RequestSendPingResult =
54       absl::variant<SendGranted, TooManyRecentPings, TooSoon>;
55 
56   // Request that one ping be sent.
57   // Returns:
58   //  - SendGranted if a ping can be sent.
59   //  - TooManyRecentPings if too many pings have been sent recently and we
60   //    should wait for some future write.
61   //  - TooSoon if we should wait for some time before sending the ping.
62   RequestSendPingResult RequestSendPing(Duration next_allowed_ping_interval,
63                                         size_t inflight_pings) const;
64   // Notify the policy that one ping has been sent.
65   void SentPing();
66   // Notify the policy that some data has been sent and so we should no longer
67   // block pings on that basis.
68   void ResetPingsBeforeDataRequired();
69   // Notify the policy that we've received some data.
70   void ReceivedDataFrame();
71   std::string GetDebugString() const;
72 
TestOnlyMaxPingsWithoutData()73   int TestOnlyMaxPingsWithoutData() const { return max_pings_without_data_; }
74 
75  private:
76   const int max_pings_without_data_;
77   const int max_inflight_pings_;
78   // No pings allowed before receiving a header or data frame.
79   int pings_before_data_required_ = 0;
80   Timestamp last_ping_sent_time_ = Timestamp::InfPast();
81 };
82 
83 std::ostream& operator<<(std::ostream& out,
84                          const Chttp2PingRatePolicy::RequestSendPingResult& r);
85 
86 }  // namespace grpc_core
87 
88 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_PING_RATE_POLICY_H
89