• 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_CORE_EXT_FILTERS_CLIENT_CHANNEL_RETRY_THROTTLE_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RETRY_THROTTLE_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include "src/core/lib/gprpp/ref_counted.h"
25 
26 namespace grpc_core {
27 namespace internal {
28 
29 /// Tracks retry throttling data for an individual server name.
30 class ServerRetryThrottleData : public RefCounted<ServerRetryThrottleData> {
31  public:
32   ServerRetryThrottleData(intptr_t max_milli_tokens, intptr_t milli_token_ratio,
33                           ServerRetryThrottleData* old_throttle_data);
34 
35   /// Records a failure.  Returns true if it's okay to send a retry.
36   bool RecordFailure();
37 
38   /// Records a success.
39   void RecordSuccess();
40 
max_milli_tokens()41   intptr_t max_milli_tokens() const { return max_milli_tokens_; }
milli_token_ratio()42   intptr_t milli_token_ratio() const { return milli_token_ratio_; }
43 
44  private:
45   // So Delete() can call our private dtor.
46   template <typename T>
47   friend void grpc_core::Delete(T*);
48 
49   ~ServerRetryThrottleData();
50 
51   void GetReplacementThrottleDataIfNeeded(
52       ServerRetryThrottleData** throttle_data);
53 
54   const intptr_t max_milli_tokens_;
55   const intptr_t milli_token_ratio_;
56   gpr_atm milli_tokens_;
57   // A pointer to the replacement for this ServerRetryThrottleData entry.
58   // If non-nullptr, then this entry is stale and must not be used.
59   // We hold a reference to the replacement.
60   gpr_atm replacement_ = 0;
61 };
62 
63 /// Global map of server name to retry throttle data.
64 class ServerRetryThrottleMap {
65  public:
66   /// Initializes global map of failure data for each server name.
67   static void Init();
68   /// Shuts down global map of failure data for each server name.
69   static void Shutdown();
70 
71   /// Returns the failure data for \a server_name, creating a new entry if
72   /// needed.
73   static RefCountedPtr<ServerRetryThrottleData> GetDataForServer(
74       const char* server_name, intptr_t max_milli_tokens,
75       intptr_t milli_token_ratio);
76 };
77 
78 }  // namespace internal
79 }  // namespace grpc_core
80 
81 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RETRY_THROTTLE_H */
82