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 <grpc/support/port_platform.h>
16
17 #include "src/core/ext/transport/chttp2/transport/ping_rate_policy.h"
18
19 #include <algorithm>
20 #include <ostream>
21
22 #include "absl/strings/str_cat.h"
23 #include "absl/types/optional.h"
24
25 #include <grpc/impl/channel_arg_names.h>
26
27 #include "src/core/lib/experiments/experiments.h"
28 #include "src/core/lib/gprpp/match.h"
29
30 // How many pings do we allow to be inflight at any given time?
31 // In older versions of gRPC this was implicitly 1.
32 // With the multiping experiment we allow this to rise to 100 by default.
33 // TODO(ctiller): consider making this public API
34 #define GRPC_ARG_HTTP2_MAX_INFLIGHT_PINGS "grpc.http2.max_inflight_pings"
35
36 namespace grpc_core {
37
38 namespace {
39 int g_default_max_pings_without_data = 2;
40 absl::optional<int> g_default_max_inflight_pings;
41 } // namespace
42
Chttp2PingRatePolicy(const ChannelArgs & args,bool is_client)43 Chttp2PingRatePolicy::Chttp2PingRatePolicy(const ChannelArgs& args,
44 bool is_client)
45 : max_pings_without_data_(
46 is_client
47 ? std::max(0, args.GetInt(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA)
48 .value_or(g_default_max_pings_without_data))
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 =
59 std::max(0, args.GetInt(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA)
60 .value_or(g_default_max_pings_without_data));
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_pings_without_data_ != 0 && pings_before_data_required_ == 0) {
68 return TooManyRecentPings{};
69 }
70 if (max_inflight_pings_ > 0 &&
71 inflight_pings > static_cast<size_t>(max_inflight_pings_)) {
72 return TooManyRecentPings{};
73 }
74 const Timestamp next_allowed_ping =
75 last_ping_sent_time_ + next_allowed_ping_interval;
76 const Timestamp now = Timestamp::Now();
77 if (next_allowed_ping > now) {
78 return TooSoon{next_allowed_ping_interval, last_ping_sent_time_,
79 next_allowed_ping - now};
80 }
81 return SendGranted{};
82 }
83
SentPing()84 void Chttp2PingRatePolicy::SentPing() {
85 last_ping_sent_time_ = Timestamp::Now();
86 if (pings_before_data_required_) --pings_before_data_required_;
87 }
88
ReceivedDataFrame()89 void Chttp2PingRatePolicy::ReceivedDataFrame() {
90 last_ping_sent_time_ = Timestamp::InfPast();
91 }
92
ResetPingsBeforeDataRequired()93 void Chttp2PingRatePolicy::ResetPingsBeforeDataRequired() {
94 pings_before_data_required_ = max_pings_without_data_;
95 }
96
GetDebugString() const97 std::string Chttp2PingRatePolicy::GetDebugString() const {
98 return absl::StrCat(
99 "max_pings_without_data: ", max_pings_without_data_,
100 ", pings_before_data_required: ", pings_before_data_required_,
101 ", last_ping_sent_time_: ", last_ping_sent_time_.ToString());
102 }
103
operator <<(std::ostream & out,const Chttp2PingRatePolicy::RequestSendPingResult & r)104 std::ostream& operator<<(std::ostream& out,
105 const Chttp2PingRatePolicy::RequestSendPingResult& r) {
106 Match(
107 r, [&out](Chttp2PingRatePolicy::SendGranted) { out << "SendGranted"; },
108 [&out](Chttp2PingRatePolicy::TooManyRecentPings) {
109 out << "TooManyRecentPings";
110 },
111 [&out](Chttp2PingRatePolicy::TooSoon r) {
112 out << "TooSoon: next_allowed="
113 << r.next_allowed_ping_interval.ToString()
114 << " last_ping_sent_time=" << r.last_ping.ToString()
115 << " wait=" << r.wait.ToString();
116 });
117 return out;
118 }
119
120 } // namespace grpc_core
121