Skip to content
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

Improve DelayStrategy sample #35654

Merged
merged 2 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Improve DelayStrategy sample
  • Loading branch information
JoshLove-msft committed Apr 19, 2023
commit bcd98ac604a2c00d3b5914af68bf87bc232f3cf2
31 changes: 28 additions & 3 deletions sdk/core/Azure.Core/samples/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,36 @@ SecretClientOptions options = new SecretClientOptions()

Another scenario where it may be helpful to use a custom retry policy is when you need to customize the delay behavior, but don't need to adjust the logic used to determine whether a request should be retried or not. In this case, it isn't necessary to create a custom `RetryPolicy` class - instead, you can pass in a `DelayStrategy` into the `RetryPolicy` constructor.

In the below example, we create a customized exponential delay strategy that uses different jitter factors from the default values. We then pass the strategy into the `RetryPolicy` constructor and set the constructed policy in our options.
```C# Snippet:CustomizeExponentialDelay
In the below example, we create a customized delay strategy that uses a fixed sequence of delays that will be iterated through using the retry number. We then pass the strategy into the `RetryPolicy` constructor and set the constructed policy in our options.
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved
```C# Snippet:SequentialDelayStrategy
public class SequentialDelayStrategy : DelayStrategy
{
private static readonly TimeSpan[] PollingSequence = new TimeSpan[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(4),
TimeSpan.FromSeconds(8),
TimeSpan.FromSeconds(16),
TimeSpan.FromSeconds(32)
};
private static readonly TimeSpan MaxDelay = PollingSequence[PollingSequence.Length - 1];

protected override TimeSpan GetNextDelayCore(Response response, int retryNumber)
{
int index = retryNumber - 1;
return index >= PollingSequence.Length ? MaxDelay : PollingSequence[index];
}
}
```

Here is how the custom delay would be used in the client options.
```C# Snippet:CustomizedDelay
SecretClientOptions options = new SecretClientOptions()
{
RetryPolicy = new RetryPolicy(delayStrategy: new MyCustomDelayStrategy())
RetryPolicy = new RetryPolicy(delayStrategy: new SequentialDelayStrategy())
};
```

Expand Down
26 changes: 21 additions & 5 deletions sdk/core/Azure.Core/tests/samples/ConfigurationSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,38 @@ public void SetGlobalTimeoutRetryPolicy()
}

[Test]
public void CustomizedJitterExponentialDelay()
public void CustomizedDelayStrategy()
{
#region Snippet:CustomizeExponentialDelay
#region Snippet:CustomizedDelay
SecretClientOptions options = new SecretClientOptions()
{
RetryPolicy = new RetryPolicy(delayStrategy: new MyCustomDelayStrategy())
RetryPolicy = new RetryPolicy(delayStrategy: new SequentialDelayStrategy())
};
#endregion
}

private class MyCustomDelayStrategy : DelayStrategy
#region Snippet:SequentialDelayStrategy
public class SequentialDelayStrategy : DelayStrategy
{
private static readonly TimeSpan[] PollingSequence = new TimeSpan[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(4),
TimeSpan.FromSeconds(8),
TimeSpan.FromSeconds(16),
TimeSpan.FromSeconds(32)
};
private static readonly TimeSpan MaxDelay = PollingSequence[PollingSequence.Length - 1];

protected override TimeSpan GetNextDelayCore(Response response, int retryNumber)
{
throw new NotImplementedException();
int index = retryNumber - 1;
return index >= PollingSequence.Length ? MaxDelay : PollingSequence[index];
}
}
#endregion
}
}