e79aa3c0ed
Former-commit-id: a2155e9bd80020e49e72e86c44da02a8ac0e57a4
41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
//-----------------------------------------------------------------------------
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
//-----------------------------------------------------------------------------
|
|
|
|
namespace System.Activities.DurableInstancing
|
|
{
|
|
using System;
|
|
|
|
class LoadRetryExponentialBackoffStrategy : ILoadRetryStrategy
|
|
{
|
|
readonly TimeSpan DefaultBackoffLimit = TimeSpan.FromSeconds(10);
|
|
readonly TimeSpan DefaultBackoffMultiplier = TimeSpan.FromMilliseconds(100);
|
|
readonly int expLimit = (int)(Math.Log(Int32.MaxValue, 2)) - 1;
|
|
readonly TimeSpan multiplier;
|
|
readonly TimeSpan maxDelay;
|
|
|
|
Random random = new Random(DateTime.Now.Millisecond);
|
|
|
|
public LoadRetryExponentialBackoffStrategy()
|
|
{
|
|
this.multiplier = DefaultBackoffMultiplier;
|
|
this.maxDelay = DefaultBackoffLimit;
|
|
}
|
|
|
|
public TimeSpan RetryDelay(int retryAttempt)
|
|
{
|
|
int power = Math.Min(retryAttempt, this.expLimit);
|
|
|
|
return TimeSpan.FromMilliseconds
|
|
(
|
|
Math.Min
|
|
(
|
|
this.maxDelay.TotalMilliseconds,
|
|
this.multiplier.TotalMilliseconds * random.Next(1, ((2 << power) - 1))
|
|
)
|
|
);
|
|
}
|
|
|
|
}
|
|
}
|