1 // 2 // Copyright 2018 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef GRPC_SRC_CORE_CLIENT_CHANNEL_RETRY_SERVICE_CONFIG_H 18 #define GRPC_SRC_CORE_CLIENT_CHANNEL_RETRY_SERVICE_CONFIG_H 19 20 #include <grpc/support/port_platform.h> 21 #include <stddef.h> 22 #include <stdint.h> 23 24 #include <memory> 25 26 #include "absl/strings/string_view.h" 27 #include "absl/types/optional.h" 28 #include "src/core/config/core_configuration.h" 29 #include "src/core/lib/channel/channel_args.h" 30 #include "src/core/lib/channel/status_util.h" 31 #include "src/core/service_config/service_config_parser.h" 32 #include "src/core/util/json/json.h" 33 #include "src/core/util/json/json_args.h" 34 #include "src/core/util/json/json_object_loader.h" 35 #include "src/core/util/time.h" 36 #include "src/core/util/validation_errors.h" 37 38 namespace grpc_core { 39 namespace internal { 40 41 class RetryGlobalConfig final : public ServiceConfigParser::ParsedConfig { 42 public: max_milli_tokens()43 uintptr_t max_milli_tokens() const { return max_milli_tokens_; } milli_token_ratio()44 uintptr_t milli_token_ratio() const { return milli_token_ratio_; } 45 46 static const JsonLoaderInterface* JsonLoader(const JsonArgs&); 47 void JsonPostLoad(const Json& json, const JsonArgs& args, 48 ValidationErrors* errors); 49 50 private: 51 uintptr_t max_milli_tokens_ = 0; 52 uintptr_t milli_token_ratio_ = 0; 53 }; 54 55 class RetryMethodConfig final : public ServiceConfigParser::ParsedConfig { 56 public: max_attempts()57 int max_attempts() const { return max_attempts_; } initial_backoff()58 Duration initial_backoff() const { return initial_backoff_; } max_backoff()59 Duration max_backoff() const { return max_backoff_; } backoff_multiplier()60 float backoff_multiplier() const { return backoff_multiplier_; } retryable_status_codes()61 StatusCodeSet retryable_status_codes() const { 62 return retryable_status_codes_; 63 } per_attempt_recv_timeout()64 absl::optional<Duration> per_attempt_recv_timeout() const { 65 return per_attempt_recv_timeout_; 66 } 67 68 static const JsonLoaderInterface* JsonLoader(const JsonArgs&); 69 void JsonPostLoad(const Json& json, const JsonArgs& args, 70 ValidationErrors* errors); 71 72 template <typename Sink> AbslStringify(Sink & sink,const RetryMethodConfig & config)73 friend void AbslStringify(Sink& sink, const RetryMethodConfig& config) { 74 sink.Append(absl::StrCat( 75 "max_attempts:", config.max_attempts_, " initial_backoff:", 76 config.initial_backoff_, " max_backoff:", config.max_backoff_, 77 " backoff_multiplier:", config.backoff_multiplier_, 78 " retryable_status_codes:", config.retryable_status_codes_.ToString(), 79 " per_attempt_recv_timeout:", 80 config.per_attempt_recv_timeout_.has_value() 81 ? absl::StrCat(*config.per_attempt_recv_timeout_) 82 : "none")); 83 } 84 85 private: 86 int max_attempts_ = 0; 87 Duration initial_backoff_; 88 Duration max_backoff_; 89 float backoff_multiplier_ = 0; 90 StatusCodeSet retryable_status_codes_; 91 absl::optional<Duration> per_attempt_recv_timeout_; 92 }; 93 94 class RetryServiceConfigParser final : public ServiceConfigParser::Parser { 95 public: name()96 absl::string_view name() const override { return parser_name(); } 97 98 std::unique_ptr<ServiceConfigParser::ParsedConfig> ParseGlobalParams( 99 const ChannelArgs& /*args*/, const Json& json, 100 ValidationErrors* errors) override; 101 102 std::unique_ptr<ServiceConfigParser::ParsedConfig> ParsePerMethodParams( 103 const ChannelArgs& args, const Json& json, 104 ValidationErrors* errors) override; 105 106 static size_t ParserIndex(); 107 static void Register(CoreConfiguration::Builder* builder); 108 109 private: parser_name()110 static absl::string_view parser_name() { return "retry"; } 111 }; 112 113 } // namespace internal 114 } // namespace grpc_core 115 116 #endif // GRPC_SRC_CORE_CLIENT_CHANNEL_RETRY_SERVICE_CONFIG_H 117