-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honor retry delay on Azure/OpenAI status codes 429 and 503 (#795)
## Motivation and Context (Why the change? What's the scenario?) When sending too many requests to Azure / OpenAI and receiving status code 429, the response includes how long to wait before retrying. The same might be true for status code 503. KM default retry strategy ignores this information, often retrying too soon, and causing unnecessary errors and potential pipeline failures. ## High level description (Approach, Design) Fix the retry policy to honor these headers in case of 429 and 503: * retry-after-ms * x-ms-retry-after-ms * Retry-After
- Loading branch information
Showing
12 changed files
with
199 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Net.Http.Headers; | ||
|
||
namespace Microsoft.KernelMemory.Utils; | ||
|
||
#pragma warning disable CA1303 | ||
#pragma warning disable CA1812 | ||
|
||
// TMP workaround for Azure SDK bug | ||
// See https://github.com/Azure/azure-sdk-for-net/issues/46109 | ||
internal sealed class AuthFixHandler : DelegatingHandler | ||
{ | ||
protected override Task<HttpResponseMessage> SendAsync( | ||
HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
if (request.Headers.TryGetValues("Authorization", out var headers) && headers.Count() > 1) | ||
{ | ||
request.Headers.Authorization = new AuthenticationHeaderValue( | ||
request.Headers.Authorization!.Scheme, | ||
request.Headers.Authorization.Parameter); | ||
} | ||
|
||
return base.SendAsync(request, cancellationToken); | ||
} | ||
} | ||
|
||
internal sealed class HttpLogger : DelegatingHandler | ||
{ | ||
protected async override Task<HttpResponseMessage> SendAsync( | ||
HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
// Log the request | ||
Console.WriteLine("## Request:"); | ||
Console.WriteLine(request.ToString()); | ||
if (request.Content != null) | ||
{ | ||
Console.WriteLine(await request.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)); | ||
} | ||
|
||
Console.WriteLine("Headers"); | ||
foreach (var h in request.Headers) | ||
{ | ||
foreach (string x in h.Value) | ||
{ | ||
Console.WriteLine($"{h.Key}: {x}"); | ||
} | ||
} | ||
|
||
Console.WriteLine(); | ||
|
||
// Proceed with the request | ||
HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); | ||
|
||
// Log the response | ||
Console.WriteLine("\n\n## Response:"); | ||
Console.WriteLine(response.ToString()); | ||
if (response.Content != null) | ||
{ | ||
Console.WriteLine(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)); | ||
} | ||
|
||
Console.WriteLine(); | ||
|
||
return response; | ||
} | ||
} |
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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