xref: /aosp_15_r20/external/grpc-grpc/src/core/ext/transport/chttp2/transport/max_concurrent_streams_policy.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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_MAX_CONCURRENT_STREAMS_POLICY_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_MAX_CONCURRENT_STREAMS_POLICY_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <cstdint>
21 #include <limits>
22 
23 namespace grpc_core {
24 
25 class Chttp2MaxConcurrentStreamsPolicy {
26  public:
27   // Set the target number of concurrent streams.
28   // If everything is idle we should advertise this number.
SetTarget(uint32_t target)29   void SetTarget(uint32_t target) { target_ = target; }
30 
31   // Add one demerit to the current target.
32   // We need to do one full settings round trip after this to clear this
33   // demerit.
34   // It will reduce our advertised max concurrent streams by one.
35   void AddDemerit();
36 
37   // Notify the policy that we've sent a settings frame.
38   // Newly added demerits since the last settings frame was sent will be cleared
39   // once that settings frame is acknowledged.
40   void FlushedSettings();
41 
42   // Notify the policy that we've received an acknowledgement for the last
43   // settings frame we sent.
44   void AckLastSend();
45 
46   // Returns what we should advertise as max concurrent streams.
47   uint32_t AdvertiseValue() const;
48 
49  private:
50   uint32_t target_ = std::numeric_limits<int32_t>::max();
51   // Demerit flow:
52   // When we add a demerit, we add to both new & unacked.
53   // When we flush settings, we move new to sent.
54   // When we ack settings, we remove what we sent from unacked.
55   // eg:
56   // we add 10 demerits - now new=10, sent=0, unacked=10
57   // we send settings - now new=0, sent=10, unacked=10
58   // we add 5 demerits - now new=5, sent=10, unacked=15
59   // we get the settings ack - now new=5, sent=0, unacked=5
60   uint32_t new_demerits_ = 0;
61   uint32_t sent_demerits_ = 0;
62   uint32_t unacked_demerits_ = 0;
63 };
64 
65 }  // namespace grpc_core
66 
67 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_MAX_CONCURRENT_STREAMS_POLICY_H
68