• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WRITE_SIZE_POLICY_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_WRITE_SIZE_POLICY_H
17 
18 #include <grpc/support/port_platform.h>
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 #include "src/core/util/time.h"
23 
24 namespace grpc_core {
25 
26 class Chttp2WriteSizePolicy {
27  public:
28   // Smallest possible WriteTargetSize
MinTarget()29   static constexpr size_t MinTarget() { return 32 * 1024; }
30   // Largest possible WriteTargetSize
MaxTarget()31   static constexpr size_t MaxTarget() { return 16 * 1024 * 1024; }
32   // How long should a write take to be considered "fast"
FastWrite()33   static constexpr Duration FastWrite() { return Duration::Milliseconds(100); }
34   // How long should a write take to be considered "slow"
SlowWrite()35   static constexpr Duration SlowWrite() { return Duration::Seconds(1); }
36   // If a read is slow, what target time should we use to try and adjust back
37   // to?
TargetWriteTime()38   static constexpr Duration TargetWriteTime() {
39     return Duration::Milliseconds(300);
40   }
41 
42   // What size should be targeted for the next write.
43   size_t WriteTargetSize();
44   // Notify the policy that a write of some size has begun.
45   // EndWrite must be called when the write completes.
46   void BeginWrite(size_t size);
47   // Notify the policy that a write of some size has ended.
48   void EndWrite(bool success);
49 
50  private:
51   size_t current_target_ = 128 * 1024;
52   Timestamp experiment_start_time_ = Timestamp::InfFuture();
53   // State varies from -2...2
54   // Every time we do a write faster than kFastWrite, we decrement
55   // Every time we do a write slower than kSlowWrite, we increment
56   // If we hit -2, we increase the target size and reset state to 0
57   // If we hit 2, we decrease the target size and reset state to 0
58   // In this way, we need two consecutive fast/slow operations to adjust,
59   // denoising the signal significantly
60   int8_t state_ = 0;
61 };
62 
63 }  // namespace grpc_core
64 
65 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_WRITE_SIZE_POLICY_H
66