Skip to content

Commit d05f172

Browse files
committed
file scoped namespaces
1 parent 87cf9c0 commit d05f172

File tree

296 files changed

+49432
-49756
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

296 files changed

+49432
-49756
lines changed

src/Polly.Benchmarks/Bulkhead.cs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,47 @@
22
using System.Threading.Tasks;
33
using BenchmarkDotNet.Attributes;
44

5-
namespace Polly.Benchmarks
5+
namespace Polly.Benchmarks;
6+
7+
[Config(typeof(PollyConfig))]
8+
public class Bulkhead
69
{
7-
[Config(typeof(PollyConfig))]
8-
public class Bulkhead
10+
private static readonly Policy SyncPolicy = Policy.Bulkhead(2);
11+
private static readonly AsyncPolicy AsyncPolicy = Policy.BulkheadAsync(2);
12+
13+
[Benchmark]
14+
public void Bulkhead_Synchronous()
15+
{
16+
SyncPolicy.Execute(() => Workloads.Action());
17+
}
18+
19+
[Benchmark]
20+
public async Task Bulkhead_Asynchronous()
21+
{
22+
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
23+
}
24+
25+
[Benchmark]
26+
public async Task Bulkhead_Asynchronous_With_CancellationToken()
27+
{
28+
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
29+
}
30+
31+
[Benchmark]
32+
public int Bulkhead_Synchronous_With_Result()
33+
{
34+
return SyncPolicy.Execute(() => Workloads.Func<int>());
35+
}
36+
37+
[Benchmark]
38+
public async Task<int> Bulkhead_Asynchronous_With_Result()
39+
{
40+
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
41+
}
42+
43+
[Benchmark]
44+
public async Task<int> Bulkhead_Asynchronous_With_Result_With_CancellationToken()
945
{
10-
private static readonly Policy SyncPolicy = Policy.Bulkhead(2);
11-
private static readonly AsyncPolicy AsyncPolicy = Policy.BulkheadAsync(2);
12-
13-
[Benchmark]
14-
public void Bulkhead_Synchronous()
15-
{
16-
SyncPolicy.Execute(() => Workloads.Action());
17-
}
18-
19-
[Benchmark]
20-
public async Task Bulkhead_Asynchronous()
21-
{
22-
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
23-
}
24-
25-
[Benchmark]
26-
public async Task Bulkhead_Asynchronous_With_CancellationToken()
27-
{
28-
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
29-
}
30-
31-
[Benchmark]
32-
public int Bulkhead_Synchronous_With_Result()
33-
{
34-
return SyncPolicy.Execute(() => Workloads.Func<int>());
35-
}
36-
37-
[Benchmark]
38-
public async Task<int> Bulkhead_Asynchronous_With_Result()
39-
{
40-
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
41-
}
42-
43-
[Benchmark]
44-
public async Task<int> Bulkhead_Asynchronous_With_Result_With_CancellationToken()
45-
{
46-
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
47-
}
46+
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
4847
}
49-
}
48+
}

src/Polly.Benchmarks/Cache.cs

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,107 +5,106 @@
55
using Microsoft.Extensions.Caching.Memory;
66
using Polly.Caching;
77

8-
namespace Polly.Benchmarks
8+
namespace Polly.Benchmarks;
9+
10+
[Config(typeof(PollyConfig))]
11+
public class Cache
912
{
10-
[Config(typeof(PollyConfig))]
11-
public class Cache
13+
private static readonly MemoryCache MemoryCache = new MemoryCache(new MemoryCacheOptions());
14+
private static readonly MemoryCacheProvider CacheProvider = new MemoryCacheProvider(MemoryCache);
15+
16+
private static readonly Policy SyncPolicyMiss = Policy.Cache(CacheProvider, TimeSpan.Zero);
17+
private static readonly AsyncPolicy AsyncPolicyMiss = Policy.CacheAsync(CacheProvider, TimeSpan.Zero);
18+
19+
private static readonly Policy SyncPolicyHit = Policy.Cache(CacheProvider, TimeSpan.MaxValue);
20+
private static readonly AsyncPolicy AsyncPolicyHit = Policy.CacheAsync(CacheProvider, TimeSpan.MaxValue);
21+
22+
private static readonly Context HitContext = new Context(nameof(HitContext));
23+
private static readonly Context MissContext = new Context(nameof(MissContext));
24+
25+
[GlobalSetup]
26+
public async Task GlobalSetup()
1227
{
13-
private static readonly MemoryCache MemoryCache = new MemoryCache(new MemoryCacheOptions());
14-
private static readonly MemoryCacheProvider CacheProvider = new MemoryCacheProvider(MemoryCache);
28+
SyncPolicyHit.Execute((context) => GetObject(), HitContext);
29+
await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
30+
}
1531

16-
private static readonly Policy SyncPolicyMiss = Policy.Cache(CacheProvider, TimeSpan.Zero);
17-
private static readonly AsyncPolicy AsyncPolicyMiss = Policy.CacheAsync(CacheProvider, TimeSpan.Zero);
32+
[Benchmark]
33+
public object Cache_Synchronous_Hit()
34+
{
35+
return SyncPolicyHit.Execute((context) => GetObject(), HitContext);
36+
}
1837

19-
private static readonly Policy SyncPolicyHit = Policy.Cache(CacheProvider, TimeSpan.MaxValue);
20-
private static readonly AsyncPolicy AsyncPolicyHit = Policy.CacheAsync(CacheProvider, TimeSpan.MaxValue);
38+
[Benchmark]
39+
public async Task<object> Cache_Asynchronous_Hit()
40+
{
41+
return await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
42+
}
2143

22-
private static readonly Context HitContext = new Context(nameof(HitContext));
23-
private static readonly Context MissContext = new Context(nameof(MissContext));
44+
[Benchmark]
45+
public object Cache_Synchronous_Miss()
46+
{
47+
return SyncPolicyMiss.Execute((context) => GetObject(), MissContext);
48+
}
2449

25-
[GlobalSetup]
26-
public async Task GlobalSetup()
27-
{
28-
SyncPolicyHit.Execute((context) => GetObject(), HitContext);
29-
await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
30-
}
50+
[Benchmark]
51+
public async Task<object> Cache_Asynchronous_Miss()
52+
{
53+
return await AsyncPolicyMiss.ExecuteAsync((context, token) => GetObjectAsync(token), MissContext, CancellationToken.None);
54+
}
3155

32-
[Benchmark]
33-
public object Cache_Synchronous_Hit()
34-
{
35-
return SyncPolicyHit.Execute((context) => GetObject(), HitContext);
36-
}
56+
private static object GetObject() => new object();
3757

38-
[Benchmark]
39-
public async Task<object> Cache_Asynchronous_Hit()
40-
{
41-
return await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
42-
}
58+
private static Task<object> GetObjectAsync(CancellationToken cancellationToken) => Task.FromResult(new object());
4359

44-
[Benchmark]
45-
public object Cache_Synchronous_Miss()
60+
private sealed class MemoryCacheProvider : ISyncCacheProvider, IAsyncCacheProvider
61+
{
62+
private readonly IMemoryCache _cache;
63+
64+
public MemoryCacheProvider(IMemoryCache memoryCache)
4665
{
47-
return SyncPolicyMiss.Execute((context) => GetObject(), MissContext);
66+
_cache = memoryCache;
4867
}
4968

50-
[Benchmark]
51-
public async Task<object> Cache_Asynchronous_Miss()
69+
public (bool, object) TryGet(string key)
5270
{
53-
return await AsyncPolicyMiss.ExecuteAsync((context, token) => GetObjectAsync(token), MissContext, CancellationToken.None);
71+
var cacheHit = _cache.TryGetValue(key, out var value);
72+
return (cacheHit, value);
5473
}
5574

56-
private static object GetObject() => new object();
57-
58-
private static Task<object> GetObjectAsync(CancellationToken cancellationToken) => Task.FromResult(new object());
59-
60-
private sealed class MemoryCacheProvider : ISyncCacheProvider, IAsyncCacheProvider
75+
public void Put(string key, object value, Ttl ttl)
6176
{
62-
private readonly IMemoryCache _cache;
63-
64-
public MemoryCacheProvider(IMemoryCache memoryCache)
65-
{
66-
_cache = memoryCache;
67-
}
77+
var remaining = DateTimeOffset.MaxValue - DateTimeOffset.UtcNow;
78+
var options = new MemoryCacheEntryOptions();
6879

69-
public (bool, object) TryGet(string key)
80+
if (ttl.SlidingExpiration)
7081
{
71-
var cacheHit = _cache.TryGetValue(key, out var value);
72-
return (cacheHit, value);
82+
options.SlidingExpiration = ttl.Timespan < remaining ? ttl.Timespan : remaining;
7383
}
74-
75-
public void Put(string key, object value, Ttl ttl)
84+
else
7685
{
77-
var remaining = DateTimeOffset.MaxValue - DateTimeOffset.UtcNow;
78-
var options = new MemoryCacheEntryOptions();
79-
80-
if (ttl.SlidingExpiration)
86+
if (ttl.Timespan == TimeSpan.MaxValue)
8187
{
82-
options.SlidingExpiration = ttl.Timespan < remaining ? ttl.Timespan : remaining;
88+
options.AbsoluteExpiration = DateTimeOffset.MaxValue;
8389
}
8490
else
8591
{
86-
if (ttl.Timespan == TimeSpan.MaxValue)
87-
{
88-
options.AbsoluteExpiration = DateTimeOffset.MaxValue;
89-
}
90-
else
91-
{
92-
options.AbsoluteExpirationRelativeToNow = ttl.Timespan < remaining ? ttl.Timespan : remaining;
93-
}
92+
options.AbsoluteExpirationRelativeToNow = ttl.Timespan < remaining ? ttl.Timespan : remaining;
9493
}
95-
96-
_cache.Set(key, value, options);
9794
}
9895

99-
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
100-
{
101-
return Task.FromResult(TryGet(key));
102-
}
96+
_cache.Set(key, value, options);
97+
}
10398

104-
public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
105-
{
106-
Put(key, value, ttl);
107-
return Task.CompletedTask;
108-
}
99+
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
100+
{
101+
return Task.FromResult(TryGet(key));
102+
}
103+
104+
public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
105+
{
106+
Put(key, value, ttl);
107+
return Task.CompletedTask;
109108
}
110109
}
111-
}
110+
}

src/Polly.Benchmarks/CircuitBreaker.cs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,35 @@
33
using System.Threading.Tasks;
44
using BenchmarkDotNet.Attributes;
55

6-
namespace Polly.Benchmarks
6+
namespace Polly.Benchmarks;
7+
8+
[Config(typeof(PollyConfig))]
9+
public class CircuitBreaker
710
{
8-
[Config(typeof(PollyConfig))]
9-
public class CircuitBreaker
10-
{
11-
private static readonly Policy SyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreaker(2, TimeSpan.FromMinutes(1));
12-
private static readonly AsyncPolicy AsyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreakerAsync(2, TimeSpan.FromMinutes(1));
11+
private static readonly Policy SyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreaker(2, TimeSpan.FromMinutes(1));
12+
private static readonly AsyncPolicy AsyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreakerAsync(2, TimeSpan.FromMinutes(1));
1313

14-
[Benchmark]
15-
public void CircuitBreaker_Synchronous_Succeeds()
16-
{
17-
SyncPolicy.Execute(() => Workloads.Action());
18-
}
14+
[Benchmark]
15+
public void CircuitBreaker_Synchronous_Succeeds()
16+
{
17+
SyncPolicy.Execute(() => Workloads.Action());
18+
}
1919

20-
[Benchmark]
21-
public async Task CircuitBreaker_Asynchronous_Succeeds()
22-
{
23-
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
24-
}
20+
[Benchmark]
21+
public async Task CircuitBreaker_Asynchronous_Succeeds()
22+
{
23+
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
24+
}
2525

26-
[Benchmark]
27-
public int CircuitBreaker_Synchronous_With_Result_Succeeds()
28-
{
29-
return SyncPolicy.Execute(() => Workloads.Func<int>());
30-
}
26+
[Benchmark]
27+
public int CircuitBreaker_Synchronous_With_Result_Succeeds()
28+
{
29+
return SyncPolicy.Execute(() => Workloads.Func<int>());
30+
}
3131

32-
[Benchmark]
33-
public async Task<int> CircuitBreaker_Asynchronous_With_Result_Succeeds()
34-
{
35-
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
36-
}
32+
[Benchmark]
33+
public async Task<int> CircuitBreaker_Asynchronous_With_Result_Succeeds()
34+
{
35+
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
3736
}
38-
}
37+
}

0 commit comments

Comments
 (0)