Skip to content

Commit b4853a7

Browse files
committed
Fix sync-over-async on DynamoDBRetryPolicy.
1 parent d81a40f commit b4853a7

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

sdk/src/Services/DynamoDBv2/Custom/Internal/DynamoDBRetryPolicy.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
/*
1+
/*
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
66
* A copy of the License is located at
7-
*
7+
*
88
* http://aws.amazon.com/apache2.0
9-
*
9+
*
1010
* or in the "license" file accompanying this file. This file is distributed
1111
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
1515

1616
using System;
17-
using System.Collections.Generic;
18-
using System.Linq;
19-
using System.Text;
20-
17+
using System.Threading;
18+
using System.Threading.Tasks;
2119
using Amazon.Runtime;
2220
using Amazon.Runtime.Internal;
2321

@@ -38,12 +36,12 @@ public DynamoDBRetryPolicy(IClientConfig config) :
3836
base(config)
3937
{
4038
ThrottlingErrorCodes.Add("TransactionInProgressException");
41-
42-
//When derived from DefaultRetryPolicy, we are in legacy retry
39+
40+
//When derived from DefaultRetryPolicy, we are in legacy retry
4341
//mode. When in legacy retry mode MaxErrorRetry used to be set
4442
//to 10 in the DynamoDB and DynamoDBStreams configs. This
4543
//can no longer be set in the configs because the retry mode
46-
//may not be known at that point where standard and adaptive
44+
//may not be known at that point where standard and adaptive
4745
//retry modes are not to have this default.
4846
if(!config.IsMaxErrorRetrySet)
4947
{
@@ -57,24 +55,32 @@ public DynamoDBRetryPolicy(IClientConfig config) :
5755
/// <param name="executionContext"></param>
5856
public override void WaitBeforeRetry(IExecutionContext executionContext)
5957
{
60-
pauseExponentially(executionContext.RequestContext.Retries);
58+
Thread.Sleep(CalculateRetryDelay(executionContext.RequestContext.Retries));
59+
}
60+
61+
/// <summary>
62+
/// Overriden to cause a pause between retries.
63+
/// </summary>
64+
/// <param name="executionContext"></param>
65+
public override Task WaitBeforeRetryAsync(IExecutionContext executionContext)
66+
{
67+
return Task.Delay(CalculateRetryDelay(executionContext.RequestContext.Retries), executionContext.RequestContext.CancellationToken);
6168
}
6269

6370
/// <summary>
6471
/// Override the pausing function so retries would happen more frequent then the default operation.
6572
/// </summary>
66-
/// <param name="retries">Current number of retries.</param>
67-
private void pauseExponentially(int retries)
73+
private int CalculateRetryDelay(int retries)
6874
{
6975
int delay;
70-
76+
7177
if (retries <= 0) delay = 0;
7278
else if (retries < 20) delay = Convert.ToInt32(Math.Pow(2, retries - 1) * 50.0);
7379
else delay = Int32.MaxValue;
7480

7581
if (retries > 0 && (delay > MaxBackoffInMilliseconds || delay <= 0))
7682
delay = MaxBackoffInMilliseconds;
77-
Amazon.Util.AWSSDKUtils.Sleep(delay);
83+
return delay;
7884
}
7985
}
8086
}

0 commit comments

Comments
 (0)