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_ABUSE_POLICY_H 16 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_PING_ABUSE_POLICY_H 17 18 #include <grpc/support/port_platform.h> 19 20 #include <string> 21 22 #include "src/core/lib/channel/channel_args.h" 23 #include "src/core/lib/gprpp/time.h" 24 25 namespace grpc_core { 26 27 class Chttp2PingAbusePolicy { 28 public: 29 explicit Chttp2PingAbusePolicy(const ChannelArgs& args); 30 31 static void SetDefaults(const ChannelArgs& args); 32 33 // Record one received ping; returns true if the connection should be closed. 34 // If transport_idle is true, we increase the allowed time between pings up to 35 // TCP keep-alive check time. 36 GRPC_MUST_USE_RESULT bool ReceivedOnePing(bool transport_idle); 37 38 // Reset the ping clock, strike count. 39 void ResetPingStrikes(); 40 TestOnlyMaxPingStrikes()41 int TestOnlyMaxPingStrikes() const { return max_ping_strikes_; } TestOnlyMinPingIntervalWithoutData()42 Duration TestOnlyMinPingIntervalWithoutData() const { 43 return min_recv_ping_interval_without_data_; 44 } 45 46 std::string GetDebugString(bool transport_idle) const; 47 48 private: 49 Duration RecvPingIntervalWithoutData(bool transport_idle) const; 50 51 Timestamp last_ping_recv_time_ = Timestamp::InfPast(); 52 const Duration min_recv_ping_interval_without_data_; 53 int ping_strikes_ = 0; 54 const int max_ping_strikes_; 55 }; 56 57 } // namespace grpc_core 58 59 #endif // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_PING_ABUSE_POLICY_H 60