15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_RPC_RETRY_POLICY_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_RPC_RETRY_POLICY_H
18 #include "google/cloud/bigtable/internal/rpc_policy_parameters.h"
19 #include "google/cloud/bigtable/version.h"
20 #include "google/cloud/grpc_error_delegate.h"
21 #include "google/cloud/internal/retry_policy.h"
22 #include "google/cloud/status.h"
23 #include "absl/memory/memory.h"
24 #include <grpcpp/grpcpp.h>
29 namespace bigtable_internal {
33 class CommonRetryPolicy;
41 struct SafeGrpcRetry {
42 static inline bool IsOk(
Status const& status) {
return status
.ok(); }
43 static inline bool IsTransientFailure(
Status const& status) {
44 auto const code = status
.code();
46 google::
cloud::internal::IsTransientInternalError(status);
48 static inline bool IsPermanentFailure(
Status const& status) {
49 return !IsOk(status) && !IsTransientFailure(status);
53 static inline bool IsOk(grpc::Status
const& status) {
return status.ok(); }
54 static inline bool IsTransientFailure(grpc::Status
const& status) {
57 static inline bool IsPermanentFailure(grpc::Status
const& status) {
58 return !IsOk(status) && !IsTransientFailure(status);
81 using RetryableTraits = internal::SafeGrpcRetry;
98 virtual void Setup(grpc::ClientContext& context)
const = 0;
110 return internal::SafeGrpcRetry::IsPermanentFailure(status);
114 return internal::SafeGrpcRetry::IsPermanentFailure(status);
120 template <
typename T>
121 friend class bigtable_internal::CommonRetryPolicy;
123 bool exhausted_ =
false;
128 internal::RPCPolicyParameters defaults);
136 : impl_(maximum_failures) {}
139 void Setup(grpc::ClientContext& context)
const override;
146 using Impl = ::
google::
cloud::internal::LimitedErrorCountRetryPolicy<
147 internal::SafeGrpcRetry>;
157 template <
typename DurationT>
159 : impl_(maximum_duration) {}
162 void Setup(grpc::ClientContext& context)
const override;
169 using Impl = ::
google::
cloud::internal::LimitedTimeRetryPolicy<
170 internal::SafeGrpcRetry>;
176 namespace bigtable_internal {
179 template <
typename ReturnType>
180 class CommonRetryPolicy :
public ReturnType {
183 : impl_(std::move(impl)) {}
184 ~CommonRetryPolicy()
override =
default;
186 std::unique_ptr<ReturnType> clone()
const override {
187 return absl::make_unique<CommonRetryPolicy>(impl_->
clone());
189 bool OnFailure(
Status const& s)
override {
191 if (!retry && !IsPermanentFailure(s)) impl_->exhausted_ =
true;
194 bool IsExhausted()
const override {
return impl_->
IsExhausted(); }
195 bool IsPermanentFailure(
Status const& s)
const override {
198 void OnFailureImpl()
override {}
204 template <
typename ReturnType>
205 std::unique_ptr<ReturnType> MakeCommonRetryPolicy(
207 return absl::make_unique<CommonRetryPolicy<ReturnType>>(std::move(policy));