forked from App-vNext/Polly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChaosManager.cs
45 lines (36 loc) · 1.25 KB
/
ChaosManager.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
using Polly;
namespace Chaos;
internal class ChaosManager(IWebHostEnvironment environment, IHttpContextAccessor contextAccessor) : IChaosManager
{
private const string UserQueryParam = "user";
private const string TestUser = "test";
public ValueTask<bool> IsChaosEnabledAsync(ResilienceContext context)
{
if (environment.IsDevelopment())
{
return ValueTask.FromResult(true);
}
// This condition is demonstrative and not recommended to use in real apps.
if (environment.IsProduction() &&
contextAccessor.HttpContext is { } httpContext &&
httpContext.Request.Query.TryGetValue(UserQueryParam, out var values) &&
values == TestUser)
{
// Enable chaos for 'test' user even in production
return ValueTask.FromResult(true);
}
return ValueTask.FromResult(false);
}
public ValueTask<double> GetInjectionRateAsync(ResilienceContext context)
{
if (environment.IsDevelopment())
{
return ValueTask.FromResult(0.05);
}
if (environment.IsProduction())
{
return ValueTask.FromResult(0.03);
}
return ValueTask.FromResult(0.0);
}
}