• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2017 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_SRC_CORE_CLIENT_CHANNEL_RETRY_THROTTLE_H
20 #define GRPC_SRC_CORE_CLIENT_CHANNEL_RETRY_THROTTLE_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stdint.h>
25 
26 #include <map>
27 #include <string>
28 
29 #include "absl/base/thread_annotations.h"
30 
31 #include <grpc/support/atm.h>
32 
33 #include "src/core/lib/gprpp/ref_counted.h"
34 #include "src/core/lib/gprpp/ref_counted_ptr.h"
35 #include "src/core/lib/gprpp/sync.h"
36 
37 namespace grpc_core {
38 namespace internal {
39 
40 /// Tracks retry throttling data for an individual server name.
41 class ServerRetryThrottleData final
42     : public RefCounted<ServerRetryThrottleData> {
43  public:
44   ServerRetryThrottleData(uintptr_t max_milli_tokens,
45                           uintptr_t milli_token_ratio,
46                           ServerRetryThrottleData* old_throttle_data);
47   ~ServerRetryThrottleData() override;
48 
49   /// Records a failure.  Returns true if it's okay to send a retry.
50   bool RecordFailure();
51 
52   /// Records a success.
53   void RecordSuccess();
54 
max_milli_tokens()55   uintptr_t max_milli_tokens() const { return max_milli_tokens_; }
milli_token_ratio()56   uintptr_t milli_token_ratio() const { return milli_token_ratio_; }
57 
58  private:
59   void GetReplacementThrottleDataIfNeeded(
60       ServerRetryThrottleData** throttle_data);
61 
62   const uintptr_t max_milli_tokens_;
63   const uintptr_t milli_token_ratio_;
64   gpr_atm milli_tokens_;
65   // A pointer to the replacement for this ServerRetryThrottleData entry.
66   // If non-nullptr, then this entry is stale and must not be used.
67   // We hold a reference to the replacement.
68   gpr_atm replacement_ = 0;
69 };
70 
71 /// Global map of server name to retry throttle data.
72 class ServerRetryThrottleMap final {
73  public:
74   static ServerRetryThrottleMap* Get();
75 
76   /// Returns the failure data for \a server_name, creating a new entry if
77   /// needed.
78   RefCountedPtr<ServerRetryThrottleData> GetDataForServer(
79       const std::string& server_name, uintptr_t max_milli_tokens,
80       uintptr_t milli_token_ratio);
81 
82  private:
83   using StringToDataMap =
84       std::map<std::string, RefCountedPtr<ServerRetryThrottleData>>;
85 
86   Mutex mu_;
87   StringToDataMap map_ ABSL_GUARDED_BY(mu_);
88 };
89 
90 }  // namespace internal
91 }  // namespace grpc_core
92 
93 #endif  // GRPC_SRC_CORE_CLIENT_CHANNEL_RETRY_THROTTLE_H
94