Update RetryPolicy logic to use only one context (#2047)

* progress

* missing

* fix comp errors

* remove comments

* Bad merge with my recent changes to master

* Format

Co-authored-by: Ahson Khan <ahson_ahmedk@yahoo.com>
Co-authored-by: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com>
This commit is contained in:
Victor Vazquez 2021-04-05 19:50:17 -07:00 committed by GitHub
parent 3b36c9091a
commit cb39ec302c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 30 deletions

View File

@ -265,7 +265,7 @@ namespace Azure { namespace Core { namespace Http { namespace Policies {
* @param context The context used to call send request.
* @return A positive number indicating the current intent to send the request.
*/
static int32_t GetRetryNumber(Context const& context);
static int32_t GetRetryCount(Context const& context);
protected:
virtual bool ShouldRetryOnTransportFailure(

View File

@ -89,27 +89,9 @@ bool WasLastAttempt(RetryOptions const& retryOptions, int32_t attempt)
}
Context::Key const RetryKey;
/**
* @brief Creates a new #Context node from \p parent with the information about the retrying while
* sending an Http request.
*
* @param parent The parent context for the new created.
* @return Context with information about retry counter.
*/
inline Context CreateRetryContext(Context const& parent)
{
// First try as default
int retryCount = 0;
if (parent.HasKey(RetryKey))
{
retryCount = parent.GetValue<int>(RetryKey) + 1;
}
return parent.WithValue(RetryKey, retryCount);
}
} // namespace
int32_t RetryPolicy::GetRetryNumber(Context const& context)
int32_t RetryPolicy::GetRetryCount(Context const& context)
{
if (!context.HasKey(RetryKey))
{
@ -120,7 +102,7 @@ int32_t RetryPolicy::GetRetryNumber(Context const& context)
// ...
return -1;
}
return context.GetValue<int32_t>(RetryKey);
return *context.GetValue<int32_t*>(RetryKey);
}
std::unique_ptr<RawResponse> RetryPolicy::Send(
@ -130,8 +112,9 @@ std::unique_ptr<RawResponse> RetryPolicy::Send(
{
using Azure::Core::Diagnostics::Logger;
using Azure::Core::Diagnostics::_internal::Log;
auto retryContext = CreateRetryContext(ctx);
// retryCount needs to be apart from RetryNumber attempt.
int32_t retryCount = 0;
auto retryContext = ctx.WithValue(RetryKey, &retryCount);
for (int32_t attempt = 1;; ++attempt)
{
@ -188,7 +171,7 @@ std::unique_ptr<RawResponse> RetryPolicy::Send(
request.GetUrl().SetQueryParameters(std::move(originalQueryParameters));
// Update retry number
retryContext = CreateRetryContext(retryContext);
retryCount += 1;
}
}

View File

@ -39,9 +39,7 @@ struct TestRetryPolicySharedState : public Azure::Core::Http::Policies::HttpPoli
Azure::Core::Context const& ctx) const override
{
EXPECT_EQ(
retryCounterState,
Azure::Core::Http::Policies::_internal::RetryPolicy::GetRetryNumber(ctx));
retryCounterState, Azure::Core::Http::Policies::_internal::RetryPolicy::GetRetryCount(ctx));
retryCounterState += 1;
return nextHttpPolicy.Send(request, ctx);
}
@ -88,7 +86,7 @@ public:
Azure::Core::Http::Policies::NextHttpPolicy,
Azure::Core::Context const& context) const override
{
auto retryNumber = Azure::Core::Http::Policies::_internal::RetryPolicy::GetRetryNumber(context);
auto retryNumber = Azure::Core::Http::Policies::_internal::RetryPolicy::GetRetryCount(context);
if (retryNumber == m_successAfter)
{
auto response = std::make_unique<Azure::Core::Http::RawResponse>(
@ -163,7 +161,7 @@ TEST(Policy, RetryPolicyCounter)
// Check when there's no info about retry on the context
auto initialContext = Context::GetApplicationContext();
EXPECT_EQ(-1, RetryPolicy::GetRetryNumber(initialContext));
EXPECT_EQ(-1, RetryPolicy::GetRetryCount(initialContext));
// Pipeline with retry test
std::vector<std::unique_ptr<HttpPolicy>> policies;

View File

@ -23,7 +23,7 @@ namespace Azure { namespace Storage { namespace _internal {
&& !m_secondaryHost.empty() && replicaStatus && *replicaStatus;
if (considerSecondary
&& Azure::Core::Http::Policies::_internal::RetryPolicy::GetRetryNumber(ctx) > 0)
&& Azure::Core::Http::Policies::_internal::RetryPolicy::GetRetryCount(ctx) > 0)
{
// switch host
if (request.GetUrl().GetHost() == m_primaryHost)