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