|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_ASYNC_RETRY_POLICY_H |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_ASYNC_RETRY_POLICY_H |
| 17 | + |
| 18 | +#include "google/cloud/storage/version.h" |
| 19 | +#include "google/cloud/internal/backoff_policy.h" |
| 20 | +#include "google/cloud/internal/retry_policy_impl.h" |
| 21 | +#include "google/cloud/status.h" |
| 22 | +#include <memory> |
| 23 | + |
| 24 | +namespace google { |
| 25 | +namespace cloud { |
| 26 | +namespace storage_experimental { |
| 27 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 28 | +namespace internal { |
| 29 | +/// Defines what error codes are permanent errors. |
| 30 | +struct StatusTraits { |
| 31 | + static bool IsPermanentFailure(Status const& status) { |
| 32 | + return status.code() != StatusCode::kDeadlineExceeded && |
| 33 | + status.code() != StatusCode::kInternal && |
| 34 | + status.code() != StatusCode::kResourceExhausted && |
| 35 | + status.code() != StatusCode::kUnavailable && |
| 36 | + status.code() != StatusCode::kAborted; |
| 37 | + } |
| 38 | +}; |
| 39 | +} // namespace internal |
| 40 | + |
| 41 | +/// The base class for the Storage library async retry policies. |
| 42 | +class AsyncRetryPolicy : public google::cloud::RetryPolicy { |
| 43 | + public: |
| 44 | + /// Creates a new instance of the policy, reset to the initial state. |
| 45 | + virtual std::unique_ptr<AsyncRetryPolicy> clone() const = 0; |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * A retry policy based on counting errors. |
| 50 | + * |
| 51 | + * This policy stops retrying if: |
| 52 | + * - An RPC returns a non-transient error. |
| 53 | + * - More than a prescribed number of transient failures is detected. |
| 54 | + * |
| 55 | + * In this class the following status codes are treated as transient errors: |
| 56 | + * - [`kDeadlineExceeded`](@ref google::cloud::StatusCode) |
| 57 | + * - [`kInternal`](@ref google::cloud::StatusCode) |
| 58 | + * - [`kResourceExhausted`](@ref google::cloud::StatusCode) |
| 59 | + * - [`kUnavailable`](@ref google::cloud::StatusCode) |
| 60 | + * - [`kAborted`](@ref google::cloud::StatusCode) |
| 61 | + */ |
| 62 | +class LimitedErrorCountRetryPolicy : public AsyncRetryPolicy { |
| 63 | + public: |
| 64 | + /** |
| 65 | + * Create an instance that tolerates up to @p maximum_failures transient |
| 66 | + * errors. |
| 67 | + * |
| 68 | + * @note Disable the retry loop by providing an instance of this policy with |
| 69 | + * @p maximum_failures == 0. |
| 70 | + */ |
| 71 | + explicit LimitedErrorCountRetryPolicy(int maximum_failures) |
| 72 | + : impl_(maximum_failures) {} |
| 73 | + |
| 74 | + LimitedErrorCountRetryPolicy(LimitedErrorCountRetryPolicy&& rhs) noexcept |
| 75 | + : LimitedErrorCountRetryPolicy(rhs.maximum_failures()) {} |
| 76 | + LimitedErrorCountRetryPolicy(LimitedErrorCountRetryPolicy const& rhs) noexcept |
| 77 | + : LimitedErrorCountRetryPolicy(rhs.maximum_failures()) {} |
| 78 | + |
| 79 | + int maximum_failures() const { return impl_.maximum_failures(); } |
| 80 | + |
| 81 | + bool OnFailure(Status const& s) override { return impl_.OnFailure(s); } |
| 82 | + bool IsExhausted() const override { return impl_.IsExhausted(); } |
| 83 | + bool IsPermanentFailure(Status const& s) const override { |
| 84 | + return impl_.IsPermanentFailure(s); |
| 85 | + } |
| 86 | + std::unique_ptr<AsyncRetryPolicy> clone() const override { |
| 87 | + return std::make_unique<LimitedErrorCountRetryPolicy>( |
| 88 | + impl_.maximum_failures()); |
| 89 | + } |
| 90 | + |
| 91 | + // This is provided only for backwards compatibility. |
| 92 | + using BaseType = AsyncRetryPolicy; |
| 93 | + |
| 94 | + private: |
| 95 | + google::cloud::internal::LimitedErrorCountRetryPolicy<internal::StatusTraits> |
| 96 | + impl_; |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * A retry policy based on elapsed time. |
| 101 | + * |
| 102 | + * This policy stops retrying if: |
| 103 | + * - An RPC returns a non-transient error. |
| 104 | + * - The elapsed time in the retry loop exceeds a prescribed duration. |
| 105 | + * |
| 106 | + * In this class the following status codes are treated as transient errors: |
| 107 | + * - [`kDeadlineExceeded`](@ref google::cloud::StatusCode) |
| 108 | + * - [`kInternal`](@ref google::cloud::StatusCode) |
| 109 | + * - [`kResourceExhausted`](@ref google::cloud::StatusCode) |
| 110 | + * - [`kUnavailable`](@ref google::cloud::StatusCode) |
| 111 | + * - [`kAborted`](@ref google::cloud::StatusCode) |
| 112 | + */ |
| 113 | +class LimitedTimeRetryPolicy : public AsyncRetryPolicy { |
| 114 | + public: |
| 115 | + /** |
| 116 | + * Constructor given a `std::chrono::duration<>` object. |
| 117 | + * |
| 118 | + * @tparam DurationRep a placeholder to match the `Rep` tparam for |
| 119 | + * @p maximum_duration's type. The semantics of this template parameter |
| 120 | + * are documented in `std::chrono::duration<>`. In brief, the underlying |
| 121 | + * arithmetic type used to store the number of ticks. For our purposes it |
| 122 | + * is simply a formal parameter. |
| 123 | + * @tparam DurationPeriod a placeholder to match the `Period` tparam for |
| 124 | + * @p maximum_duration's type. The semantics of this template parameter |
| 125 | + * are documented in `std::chrono::duration<>`. In brief, the length of |
| 126 | + * the tick in seconds, expressed as a `std::ratio<>`. For our purposes it |
| 127 | + * is simply a formal parameter. |
| 128 | + * @param maximum_duration the maximum time allowed before the policy expires, |
| 129 | + * while the application can express this time in any units they desire, |
| 130 | + * the class truncates to milliseconds. |
| 131 | + * |
| 132 | + * @see https://en.cppreference.com/w/cpp/chrono/duration for more details |
| 133 | + * about `std::chrono::duration`. |
| 134 | + */ |
| 135 | + template <typename DurationRep, typename DurationPeriod> |
| 136 | + explicit LimitedTimeRetryPolicy( |
| 137 | + std::chrono::duration<DurationRep, DurationPeriod> maximum_duration) |
| 138 | + : impl_(maximum_duration) {} |
| 139 | + |
| 140 | + LimitedTimeRetryPolicy(LimitedTimeRetryPolicy&& rhs) noexcept |
| 141 | + : LimitedTimeRetryPolicy(rhs.maximum_duration()) {} |
| 142 | + LimitedTimeRetryPolicy(LimitedTimeRetryPolicy const& rhs) noexcept |
| 143 | + : LimitedTimeRetryPolicy(rhs.maximum_duration()) {} |
| 144 | + |
| 145 | + std::chrono::milliseconds maximum_duration() const { |
| 146 | + return impl_.maximum_duration(); |
| 147 | + } |
| 148 | + |
| 149 | + bool OnFailure(Status const& s) override { return impl_.OnFailure(s); } |
| 150 | + bool IsExhausted() const override { return impl_.IsExhausted(); } |
| 151 | + bool IsPermanentFailure(Status const& s) const override { |
| 152 | + return impl_.IsPermanentFailure(s); |
| 153 | + } |
| 154 | + std::unique_ptr<AsyncRetryPolicy> clone() const override { |
| 155 | + return std::make_unique<LimitedTimeRetryPolicy>(impl_.maximum_duration()); |
| 156 | + } |
| 157 | + |
| 158 | + // This is provided only for backwards compatibility. |
| 159 | + using BaseType = RetryPolicy; |
| 160 | + |
| 161 | + private: |
| 162 | + google::cloud::internal::LimitedTimeRetryPolicy<internal::StatusTraits> impl_; |
| 163 | +}; |
| 164 | + |
| 165 | +/// The backoff policy base class. |
| 166 | +using AsyncBackoffPolicy = ::google::cloud::internal::BackoffPolicy; |
| 167 | + |
| 168 | +/// Implement truncated exponential backoff with randomization. |
| 169 | +using ExponentialAsyncBackoffPolicy = |
| 170 | + ::google::cloud::internal::ExponentialBackoffPolicy; |
| 171 | + |
| 172 | +/// Configure the resume policy used in a request, client, or connection. |
| 173 | +struct AsyncRetryPolicyOption { |
| 174 | + using Type = std::shared_ptr<AsyncRetryPolicy>; |
| 175 | +}; |
| 176 | + |
| 177 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 178 | +} // namespace storage_experimental |
| 179 | +} // namespace cloud |
| 180 | +} // namespace google |
| 181 | + |
| 182 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_ASYNC_RETRY_POLICY_H |
0 commit comments