3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RETRY_THROTTLE_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RETRY_THROTTLE_H
22 #include <grpc/support/port_platform.h>
24 #include "src/core/lib/gprpp/memory.h"
25 #include "src/core/lib/gprpp/ref_counted.h"
30 /// Tracks retry throttling data for an individual server name.
31 class ServerRetryThrottleData : public RefCounted<ServerRetryThrottleData> {
33 ServerRetryThrottleData(intptr_t max_milli_tokens, intptr_t milli_token_ratio,
34 ServerRetryThrottleData* old_throttle_data);
36 /// Records a failure. Returns true if it's okay to send a retry.
39 /// Records a success.
42 intptr_t max_milli_tokens() const { return max_milli_tokens_; }
43 intptr_t milli_token_ratio() const { return milli_token_ratio_; }
46 GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
48 ~ServerRetryThrottleData();
50 void GetReplacementThrottleDataIfNeeded(
51 ServerRetryThrottleData** throttle_data);
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;
62 /// Global map of server name to retry throttle data.
63 class ServerRetryThrottleMap {
65 /// Initializes global map of failure data for each server name.
67 /// Shuts down global map of failure data for each server name.
68 static void Shutdown();
70 /// Returns the failure data for \a server_name, creating a new entry if
72 static RefCountedPtr<ServerRetryThrottleData> GetDataForServer(
73 const char* server_name, intptr_t max_milli_tokens,
74 intptr_t milli_token_ratio);
77 } // namespace internal
78 } // namespace grpc_core
80 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RETRY_THROTTLE_H */