Skip to content

Fix async waiting in DynamoDBRetryPolicy. #3842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions generator/.DevConfigs/1cecd29e-66f5-4ac7-be16-2df3ba18f3d2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"services": [
{
"serviceName": "DynamoDBv2",
"type": "patch",
"changeLogMessages": [
"Add DynamoDBRetryPolicy.WaitBeforeRetryAsync override method in order to use the updated retry logic for async retries."
]
}
]
}
37 changes: 21 additions & 16 deletions sdk/src/Services/DynamoDBv2/Custom/Internal/DynamoDBRetryPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
/*
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
*
* http://aws.amazon.com/apache2.0
*
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.Runtime.Internal;

Expand All @@ -38,12 +35,12 @@ public DynamoDBRetryPolicy(IClientConfig config) :
base(config)
{
ThrottlingErrorCodes.Add("TransactionInProgressException");
//When derived from DefaultRetryPolicy, we are in legacy retry

//When derived from DefaultRetryPolicy, we are in legacy retry
//mode. When in legacy retry mode MaxErrorRetry used to be set
//to 10 in the DynamoDB and DynamoDBStreams configs. This
//can no longer be set in the configs because the retry mode
//may not be known at that point where standard and adaptive
//may not be known at that point where standard and adaptive
//retry modes are not to have this default.
if(!config.IsMaxErrorRetrySet)
{
Expand All @@ -57,24 +54,32 @@ public DynamoDBRetryPolicy(IClientConfig config) :
/// <param name="executionContext"></param>
public override void WaitBeforeRetry(IExecutionContext executionContext)
{
pauseExponentially(executionContext.RequestContext.Retries);
Amazon.Util.AWSSDKUtils.Sleep(CalculateRetryDelay(executionContext.RequestContext.Retries));
}

/// <summary>
/// Overriden to cause a pause between retries.
/// </summary>
/// <param name="executionContext"></param>
public override Task WaitBeforeRetryAsync(IExecutionContext executionContext)
{
return Task.Delay(CalculateRetryDelay(executionContext.RequestContext.Retries), executionContext.RequestContext.CancellationToken);
}

/// <summary>
/// Override the pausing function so retries would happen more frequent then the default operation.
/// </summary>
/// <param name="retries">Current number of retries.</param>
private void pauseExponentially(int retries)
private int CalculateRetryDelay(int retries)
{
int delay;

if (retries <= 0) delay = 0;
else if (retries < 20) delay = Convert.ToInt32(Math.Pow(2, retries - 1) * 50.0);
else delay = Int32.MaxValue;

if (retries > 0 && (delay > MaxBackoffInMilliseconds || delay <= 0))
delay = MaxBackoffInMilliseconds;
Amazon.Util.AWSSDKUtils.Sleep(delay);
return delay;
}
}
}