-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathHttpTimeoutPolicyForThinClient.cs
More file actions
71 lines (59 loc) · 2.66 KB
/
HttpTimeoutPolicyForThinClient.cs
File metadata and controls
71 lines (59 loc) · 2.66 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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Net.Http;
internal sealed class HttpTimeoutPolicyForThinClient : HttpTimeoutPolicy
{
public bool shouldRetry;
public bool shouldThrow503OnTimeout;
private static readonly string Name = nameof(HttpTimeoutPolicyForThinClient);
public static readonly HttpTimeoutPolicy InstanceShouldRetryAndThrow503OnTimeout = new HttpTimeoutPolicyForThinClient(true, true);
public static readonly HttpTimeoutPolicy InstanceShouldNotRetryAndThrow503OnTimeout = new HttpTimeoutPolicyForThinClient(true, false);
private HttpTimeoutPolicyForThinClient(
bool shouldThrow503OnTimeout,
bool shouldRetry)
{
this.shouldThrow503OnTimeout = shouldThrow503OnTimeout;
this.shouldRetry = shouldRetry;
}
private readonly IReadOnlyList<(TimeSpan requestTimeout, TimeSpan delayForNextRequest)> TimeoutsAndDelays = new List<(TimeSpan requestTimeout, TimeSpan delayForNextRequest)>()
{
(TimeSpan.FromSeconds(.5), TimeSpan.Zero),
(TimeSpan.FromSeconds(1), TimeSpan.Zero),
(TimeSpan.FromSeconds(1.5), TimeSpan.Zero),
};
public override string TimeoutPolicyName => HttpTimeoutPolicyForThinClient.Name;
public override int TotalRetryCount => this.TimeoutsAndDelays.Count;
public override IEnumerator<(TimeSpan requestTimeout, TimeSpan delayForNextRequest)> GetTimeoutEnumerator()
{
return this.TimeoutsAndDelays.GetEnumerator();
}
// The hot path should always be safe to retires since it should be retrieving meta data
// information that is not idempotent.
public override bool IsSafeToRetry(HttpMethod httpMethod)
{
return this.shouldRetry;
}
public override bool ShouldRetryBasedOnResponse(HttpMethod requestHttpMethod, HttpResponseMessage responseMessage)
{
if (responseMessage == null)
{
return false;
}
if (responseMessage.StatusCode != System.Net.HttpStatusCode.RequestTimeout)
{
return false;
}
if (!this.IsSafeToRetry(requestHttpMethod))
{
return false;
}
return true;
}
public override bool ShouldThrow503OnTimeout => this.shouldThrow503OnTimeout;
}
}