-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
ResponseClassifier.cs
64 lines (58 loc) · 2.36 KB
/
ResponseClassifier.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
namespace Azure.Core
{
/// <summary>
/// A type that analyzes HTTP responses and exceptions and determines if they should be retried,
/// and/or analyzes responses and determines if they should be treated as error responses.
/// </summary>
public class ResponseClassifier
{
internal static ResponseClassifier Shared { get; } = new();
/// <summary>
/// Specifies if the request contained in the <paramref name="message"/> should be retried.
/// </summary>
public virtual bool IsRetriableResponse(HttpMessage message)
{
switch (message.Response.Status)
{
case 408: // Request Timeout
case 429: // Too Many Requests
case 500: // Internal Server Error
case 502: // Bad Gateway
case 503: // Service Unavailable
case 504: // Gateway Timeout
return true;
default:
return false;
}
}
/// <summary>
/// Specifies if the operation that caused the exception should be retried.
/// </summary>
public virtual bool IsRetriableException(Exception exception)
{
return (exception is IOException) ||
(exception is RequestFailedException requestFailed && requestFailed.Status == 0);
}
/// <summary>
/// Specifies if the operation that caused the exception should be retried taking the <see cref="HttpMessage"/> into consideration.
/// </summary>
public virtual bool IsRetriable(HttpMessage message, Exception exception)
{
return IsRetriableException(exception) ||
// Retry non-user initiated cancellations
(exception is OperationCanceledException && !message.CancellationToken.IsCancellationRequested);
}
/// <summary>
/// Specifies if the response contained in the <paramref name="message"/> is not successful.
/// </summary>
public virtual bool IsErrorResponse(HttpMessage message)
{
var statusKind = message.Response.Status / 100;
return statusKind == 4 || statusKind == 5;
}
}
}