forked from microsoft/kiota-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetryHandlerOption.cs
More file actions
82 lines (75 loc) · 2.96 KB
/
Copy pathRetryHandlerOption.cs
File metadata and controls
82 lines (75 loc) · 2.96 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
using System;
using System.Net;
using System.Net.Http;
using Microsoft.Kiota.Abstractions;
namespace Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options
{
/// <summary>
/// The retry request option class
/// </summary>
public class RetryHandlerOption : IRequestOption
{
internal const int DefaultDelay = 3;
internal const int DefaultMaxRetry = 3;
internal const int MaxMaxRetry = 10;
internal const int MaxDelay = 180;
private int _maxRetry = DefaultMaxRetry;
private int _delay = DefaultDelay;
/// <summary>
/// The waiting time in seconds before retrying a request with a maximum value of 180 seconds. This defaults to 3 seconds.
/// </summary>
public int Delay
{
get
{
return _delay;
}
set
{
if(value > MaxDelay)
{
throw new InvalidOperationException($"Maximum value for {nameof(MaxDelay)} property exceeded ");
}
_delay = value;
}
}
/// <summary>
/// The maximum number of retries for a request with a maximum value of 10. This defaults to 3.
/// </summary>
public int MaxRetry
{
get
{
return _maxRetry;
}
set
{
if(value > MaxMaxRetry)
{
throw new InvalidOperationException($"Maximum value for {nameof(MaxMaxRetry)} property exceeded ");
}
_maxRetry = value;
}
}
/// <summary>
/// The maximum time allowed for request retries.
/// </summary>
public TimeSpan RetriesTimeLimit { get; set; } = TimeSpan.Zero;
/// <summary>
/// A delegate that's called to determine whether a request should be retried or not.
/// The delegate method should accept a delay time in seconds of, number of retry attempts and <see cref="HttpResponseMessage"/> as its parameters and return a <see cref="bool"/>.
/// This defaults to a function that returns true for 503, 504, and 429 status codes and false otherwise.
/// </summary>
public Func<int, int, HttpResponseMessage, bool> ShouldRetry { get; set; } = (_, _, response) => response.StatusCode switch
{
// By default, retry on 503, 504, and 429 status codes
HttpStatusCode.ServiceUnavailable => true,
HttpStatusCode.GatewayTimeout => true,
(HttpStatusCode)429 => true,
_ => false
};
}
}