forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ClientModel: Add retry classification to MessageClassifier (Azure#41586)
* Add IsRetriable to MessageClassifier * simplify APIs * fix * fix * nits * Make TryClassify methods abstract
- Loading branch information
1 parent
5b894af
commit ad9610c
Showing
10 changed files
with
406 additions
and
109 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
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
78 changes: 68 additions & 10 deletions
78
sdk/core/System.ClientModel/src/Options/PipelineMessageClassifier.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 |
---|---|---|
@@ -1,25 +1,83 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.IO; | ||
|
||
namespace System.ClientModel.Primitives; | ||
|
||
public class PipelineMessageClassifier | ||
public abstract class PipelineMessageClassifier | ||
{ | ||
internal static PipelineMessageClassifier Default { get; } = new PipelineMessageClassifier(); | ||
public static PipelineMessageClassifier Default { get; } = new EndOfChainClassifier(); | ||
|
||
public static PipelineMessageClassifier Create(ReadOnlySpan<ushort> successStatusCodes) | ||
=> new ResponseStatusClassifier(successStatusCodes); | ||
|
||
protected internal PipelineMessageClassifier() { } | ||
protected PipelineMessageClassifier() { } | ||
|
||
public abstract bool TryClassify(PipelineMessage message, out bool isError); | ||
|
||
/// <summary> | ||
/// Specifies if the response contained in the <paramref name="message"/> is not successful. | ||
/// </summary> | ||
public virtual bool IsErrorResponse(PipelineMessage message) | ||
public abstract bool TryClassify(PipelineMessage message, Exception? exception, out bool isRetriable); | ||
|
||
internal class EndOfChainClassifier : PipelineMessageClassifier | ||
{ | ||
message.AssertResponse(); | ||
public override bool TryClassify(PipelineMessage message, out bool isError) | ||
{ | ||
message.AssertResponse(); | ||
|
||
int statusKind = message.Response!.Status / 100; | ||
isError = statusKind == 4 || statusKind == 5; | ||
|
||
// Always classify the message | ||
return true; | ||
} | ||
|
||
public override bool TryClassify(PipelineMessage message, Exception? exception, out bool isRetriable) | ||
{ | ||
isRetriable = exception is null ? | ||
IsRetriable(message) : | ||
IsRetriable(message, exception); | ||
|
||
// Always classify the message | ||
return true; | ||
} | ||
|
||
private static bool IsRetriable(PipelineMessage message) | ||
{ | ||
message.AssertResponse(); | ||
|
||
return message.Response!.Status switch | ||
{ | ||
// Request Timeout | ||
408 => true, | ||
|
||
// Too Many Requests | ||
429 => true, | ||
|
||
// Internal Server Error | ||
500 => true, | ||
|
||
// Bad Gateway | ||
502 => true, | ||
|
||
// Service Unavailable | ||
503 => true, | ||
|
||
// Gateway Timeout | ||
504 => true, | ||
|
||
// Default case | ||
_ => false | ||
}; | ||
} | ||
|
||
private static bool IsRetriable(PipelineMessage message, Exception exception) | ||
=> IsRetriable(exception) || | ||
// Retry non-user initiated cancellations | ||
(exception is OperationCanceledException && | ||
!message.CancellationToken.IsCancellationRequested); | ||
|
||
int statusKind = message.Response!.Status / 100; | ||
return statusKind == 4 || statusKind == 5; | ||
private static bool IsRetriable(Exception exception) | ||
=> (exception is IOException) || | ||
(exception is ClientResultException ex && ex.Status == 0); | ||
} | ||
} |
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
Oops, something went wrong.