-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added RetryOnTooManyRequestsPolicy to tests
- Loading branch information
Showing
2 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/RetryOnTooManyRequestsPolicy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using Azure.Core.Pipeline; | ||
|
||
namespace Azure.AI.MetricsAdvisor.Tests | ||
{ | ||
public class RetryOnTooManyRequestsPolicy : HttpPipelinePolicy | ||
{ | ||
private const int TooManyRequestsStatusCode = 429; | ||
|
||
private const string RetryAfterHeaderName = "Retry-After"; | ||
|
||
public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) => | ||
ProcessAsync(message, pipeline, false).EnsureCompleted(); | ||
|
||
public override ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) => | ||
ProcessAsync(message, pipeline, true); | ||
|
||
private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool async) | ||
{ | ||
bool gotResponse = false; | ||
|
||
while (!gotResponse) | ||
{ | ||
if (async) | ||
{ | ||
await ProcessNextAsync(message, pipeline); | ||
} | ||
else | ||
{ | ||
ProcessNext(message, pipeline); | ||
} | ||
|
||
if (message.Response.Status == TooManyRequestsStatusCode) | ||
{ | ||
TimeSpan delay = message.Response.Headers.TryGetValue(RetryAfterHeaderName, out string retryAfterValue) | ||
&& int.TryParse(retryAfterValue, out int delayInSeconds) | ||
? TimeSpan.FromSeconds(delayInSeconds) | ||
: TimeSpan.FromSeconds(1); | ||
|
||
await WaitAsync(delay, async, message.CancellationToken); | ||
} | ||
else | ||
{ | ||
gotResponse = true; | ||
} | ||
} | ||
} | ||
|
||
private async ValueTask WaitAsync(TimeSpan delay, bool async, CancellationToken cancellationToken) | ||
{ | ||
if (async) | ||
{ | ||
await Task.Delay(delay, cancellationToken); | ||
} | ||
else | ||
{ | ||
cancellationToken.WaitHandle.WaitOne(delay); | ||
} | ||
} | ||
} | ||
} |