-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathRequestOptions.cs
74 lines (63 loc) · 3.01 KB
/
RequestOptions.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
65
66
67
68
69
70
71
72
73
74
using System;
using System.Net;
namespace NGitLab
{
/// <summary>
/// Allows to override how NGitLab executes queries to the GitLab server,
/// each http call goes in that handler.
/// </summary>
public class RequestOptions
{
public int RetryCount { get; set; }
public TimeSpan RetryInterval { get; set; }
public bool IsIncremental { get; set; }
/// <summary>
/// ID or case-insensitive username of the user to impersonate, if any
/// </summary>
public string Sudo { get; set; }
/// <summary>
/// Configure the default client side timeout when calling GitLab.
/// GitLab exposes some end points which are really slow so
/// the default we use is larger than the default 100 seconds of .net
/// </summary>
public TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromMinutes(5);
public RequestOptions(int retryCount, TimeSpan retryInterval, bool isIncremental = true)
{
RetryCount = retryCount;
RetryInterval = retryInterval;
IsIncremental = isIncremental;
}
/// <summary>
/// Predicate indicating, after a request has failed, if a new attempt should occur
/// </summary>
/// <param name="ex">Exception thrown by the failed request</param>
/// <param name="retryNumber">Number of retries left</param>
/// <returns>Whether the request should be retried</returns>
public virtual bool ShouldRetry(Exception ex, int retryNumber)
{
if (ex is not GitLabException gitLabException)
return false;
// For requests that are potentially NOT Safe/Idempotent, do not retry
// See https://developer.mozilla.org/en-US/docs/Glossary/Safe/HTTP
// If there is no HTTP request method specified, carry on the predicate assessment.
if (gitLabException.MethodType.HasValue &&
gitLabException.MethodType != Impl.MethodType.Get &&
gitLabException.MethodType != Impl.MethodType.Head &&
gitLabException.MethodType != Impl.MethodType.Options)
return false;
// Use the same Transient HTTP StatusCodes as Polly's HttpPolicyExtensions
// https://github.com/App-vNext/Polly.Extensions.Http/blob/69fd292bc603cb3032e57b028522737255f03a49/src/Polly.Extensions.Http/HttpPolicyExtensions.cs#L14
return gitLabException.StatusCode >= HttpStatusCode.InternalServerError ||
gitLabException.StatusCode == HttpStatusCode.RequestTimeout;
}
public static RequestOptions Default => new RequestOptions(0, TimeSpan.Zero);
/// <summary>
/// Allows to monitor the web requests from the caller library, for example
/// to log the request duration and debug the library.
/// </summary>
public virtual WebResponse GetResponse(HttpWebRequest request)
{
return request.GetResponse();
}
}
}