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/write_size_policy.h" 18 19 #include <algorithm> 20 21 #include <grpc/support/log.h> 22 23 namespace grpc_core { 24 WriteTargetSize()25size_t Chttp2WriteSizePolicy::WriteTargetSize() { return current_target_; } 26 BeginWrite(size_t size)27void Chttp2WriteSizePolicy::BeginWrite(size_t size) { 28 GPR_ASSERT(experiment_start_time_ == Timestamp::InfFuture()); 29 if (size < current_target_ * 7 / 10) { 30 // If we were trending fast but stopped getting enough data to verify, then 31 // reset back to the default state. 32 if (state_ < 0) state_ = 0; 33 return; 34 } 35 experiment_start_time_ = Timestamp::Now(); 36 } 37 EndWrite(bool success)38void Chttp2WriteSizePolicy::EndWrite(bool success) { 39 if (experiment_start_time_ == Timestamp::InfFuture()) return; 40 const auto elapsed = Timestamp::Now() - experiment_start_time_; 41 experiment_start_time_ = Timestamp::InfFuture(); 42 if (!success) return; 43 if (elapsed < FastWrite()) { 44 --state_; 45 if (state_ == -2) { 46 state_ = 0; 47 current_target_ = std::min(current_target_ * 3 / 2, MaxTarget()); 48 } 49 } else if (elapsed > SlowWrite()) { 50 ++state_; 51 if (state_ == 2) { 52 state_ = 0; 53 current_target_ = std::max(current_target_ / 3, MinTarget()); 54 } 55 } else { 56 state_ = 0; 57 } 58 } 59 60 } // namespace grpc_core 61