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 #include <stdint.h> 24 25 #include <atomic> 26 #include <map> 27 #include <string> 28 29 #include "absl/base/thread_annotations.h" 30 #include "src/core/util/ref_counted.h" 31 #include "src/core/util/ref_counted_ptr.h" 32 #include "src/core/util/sync.h" 33 34 namespace grpc_core { 35 namespace internal { 36 37 /// Tracks retry throttling data for an individual server name. 38 class ServerRetryThrottleData final 39 : public RefCounted<ServerRetryThrottleData> { 40 public: 41 ServerRetryThrottleData(uintptr_t max_milli_tokens, 42 uintptr_t milli_token_ratio, 43 ServerRetryThrottleData* old_throttle_data); 44 ~ServerRetryThrottleData() override; 45 46 /// Records a failure. Returns true if it's okay to send a retry. 47 bool RecordFailure(); 48 49 /// Records a success. 50 void RecordSuccess(); 51 max_milli_tokens()52 uintptr_t max_milli_tokens() const { return max_milli_tokens_; } milli_token_ratio()53 uintptr_t milli_token_ratio() const { return milli_token_ratio_; } 54 55 private: 56 void GetReplacementThrottleDataIfNeeded( 57 ServerRetryThrottleData** throttle_data); 58 59 const uintptr_t max_milli_tokens_; 60 const uintptr_t milli_token_ratio_; 61 std::atomic<intptr_t> milli_tokens_; 62 // A pointer to the replacement for this ServerRetryThrottleData entry. 63 // If non-nullptr, then this entry is stale and must not be used. 64 // We hold a reference to the replacement. 65 std::atomic<ServerRetryThrottleData*> replacement_{nullptr}; 66 }; 67 68 /// Global map of server name to retry throttle data. 69 class ServerRetryThrottleMap final { 70 public: 71 static ServerRetryThrottleMap* Get(); 72 73 /// Returns the failure data for \a server_name, creating a new entry if 74 /// needed. 75 RefCountedPtr<ServerRetryThrottleData> GetDataForServer( 76 const std::string& server_name, uintptr_t max_milli_tokens, 77 uintptr_t milli_token_ratio); 78 79 private: 80 using StringToDataMap = 81 std::map<std::string, RefCountedPtr<ServerRetryThrottleData>>; 82 83 Mutex mu_; 84 StringToDataMap map_ ABSL_GUARDED_BY(mu_); 85 }; 86 87 } // namespace internal 88 } // namespace grpc_core 89 90 #endif // GRPC_SRC_CORE_CLIENT_CHANNEL_RETRY_THROTTLE_H 91