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 #include "src/core/ext/transport/chttp2/transport/ping_rate_policy.h"
16
17 #include <grpc/impl/channel_arg_names.h>
18 #include <grpc/support/port_platform.h>
19
20 #include <algorithm>
21 #include <ostream>
22
23 #include "absl/strings/str_cat.h"
24 #include "absl/types/optional.h"
25 #include "src/core/lib/experiments/experiments.h"
26 #include "src/core/util/match.h"
27
28 // How many pings do we allow to be inflight at any given time?
29 // In older versions of gRPC this was implicitly 1.
30 // With the multiping experiment we allow this to rise to 100 by default.
31 // TODO(ctiller): consider making this public API
32 #define GRPC_ARG_HTTP2_MAX_INFLIGHT_PINGS "grpc.http2.max_inflight_pings"
33
34 namespace grpc_core {
35
36 namespace {
37 int g_default_max_pings_without_data_sent = 2;
38 constexpr Duration kThrottleIntervalWithoutDataSent = Duration::Minutes(1);
39 absl::optional<int> g_default_max_inflight_pings;
40 } // namespace
41
Chttp2PingRatePolicy(const ChannelArgs & args,bool is_client)42 Chttp2PingRatePolicy::Chttp2PingRatePolicy(const ChannelArgs& args,
43 bool is_client)
44 : max_pings_without_data_sent_(
45 is_client
46 ? std::max(0,
47 args.GetInt(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA)
48 .value_or(g_default_max_pings_without_data_sent))
49 : 0),
50 // Configuration via channel arg dominates, otherwise if the multiping
51 // experiment is enabled we use 100, otherwise 1.
52 max_inflight_pings_(
53 std::max(0, args.GetInt(GRPC_ARG_HTTP2_MAX_INFLIGHT_PINGS)
54 .value_or(g_default_max_inflight_pings.value_or(
55 IsMultipingEnabled() ? 100 : 1)))) {}
56
SetDefaults(const ChannelArgs & args)57 void Chttp2PingRatePolicy::SetDefaults(const ChannelArgs& args) {
58 g_default_max_pings_without_data_sent =
59 std::max(0, args.GetInt(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA)
60 .value_or(g_default_max_pings_without_data_sent));
61 g_default_max_inflight_pings = args.GetInt(GRPC_ARG_HTTP2_MAX_INFLIGHT_PINGS);
62 }
63
64 Chttp2PingRatePolicy::RequestSendPingResult
RequestSendPing(Duration next_allowed_ping_interval,size_t inflight_pings) const65 Chttp2PingRatePolicy::RequestSendPing(Duration next_allowed_ping_interval,
66 size_t inflight_pings) const {
67 if (max_inflight_pings_ > 0 &&
68 inflight_pings > static_cast<size_t>(max_inflight_pings_)) {
69 return TooManyRecentPings{};
70 }
71 const Timestamp next_allowed_ping =
72 last_ping_sent_time_ + next_allowed_ping_interval;
73 const Timestamp now = Timestamp::Now();
74 if (next_allowed_ping > now) {
75 return TooSoon{next_allowed_ping_interval, last_ping_sent_time_,
76 next_allowed_ping - now};
77 }
78 // Throttle pings to 1 minute if we haven't sent any data recently
79 if (max_pings_without_data_sent_ != 0 &&
80 pings_before_data_sending_required_ == 0) {
81 if (IsMaxPingsWoDataThrottleEnabled()) {
82 const Timestamp next_allowed_ping =
83 last_ping_sent_time_ + kThrottleIntervalWithoutDataSent;
84 if (next_allowed_ping > now) {
85 return TooSoon{kThrottleIntervalWithoutDataSent, last_ping_sent_time_,
86 next_allowed_ping - now};
87 }
88 } else {
89 return TooManyRecentPings{};
90 }
91 }
92
93 return SendGranted{};
94 }
95
SentPing()96 void Chttp2PingRatePolicy::SentPing() {
97 last_ping_sent_time_ = Timestamp::Now();
98 if (pings_before_data_sending_required_ > 0) {
99 --pings_before_data_sending_required_;
100 }
101 }
102
ReceivedDataFrame()103 void Chttp2PingRatePolicy::ReceivedDataFrame() {
104 last_ping_sent_time_ = Timestamp::InfPast();
105 }
106
ResetPingsBeforeDataRequired()107 void Chttp2PingRatePolicy::ResetPingsBeforeDataRequired() {
108 pings_before_data_sending_required_ = max_pings_without_data_sent_;
109 }
110
GetDebugString() const111 std::string Chttp2PingRatePolicy::GetDebugString() const {
112 return absl::StrCat(
113 "max_pings_without_data: ", max_pings_without_data_sent_,
114 ", pings_before_data_required: ", pings_before_data_sending_required_,
115 ", last_ping_sent_time_: ", last_ping_sent_time_.ToString());
116 }
117
operator <<(std::ostream & out,const Chttp2PingRatePolicy::RequestSendPingResult & r)118 std::ostream& operator<<(std::ostream& out,
119 const Chttp2PingRatePolicy::RequestSendPingResult& r) {
120 Match(
121 r, [&out](Chttp2PingRatePolicy::SendGranted) { out << "SendGranted"; },
122 [&out](Chttp2PingRatePolicy::TooManyRecentPings) {
123 out << "TooManyRecentPings";
124 },
125 [&out](Chttp2PingRatePolicy::TooSoon r) {
126 out << "TooSoon: next_allowed="
127 << r.next_allowed_ping_interval.ToString()
128 << " last_ping_sent_time=" << r.last_ping.ToString()
129 << " wait=" << r.wait.ToString();
130 });
131 return out;
132 }
133
134 } // namespace grpc_core
135