Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1a5e00c
Bump version to 7.3.1
martincostello Jan 17, 2022
dfa9040
Change version to 7.2.4
martincostello Jan 17, 2022
ac01478
Add RateLimit benchmarks (#910)
martincostello Jan 17, 2022
9d13eac
Fixed an incorrect exception argument (#915)
FoxTes Feb 3, 2022
9d7d8bc
Upgrade FluentAssertions (#918)
dotnetspark Feb 13, 2022
3cbef2b
Add code coverage (#919)
martincostello Feb 13, 2022
98f7429
Add Visual Studio Code config files
martincostello Feb 13, 2022
c36712e
Bump Cake to v2.0.0 (#921)
eugeneogongo Feb 13, 2022
9f2c129
Tidy up build scripts
martincostello Feb 13, 2022
50b723b
Set executable bit
martincostello Feb 13, 2022
9f8d370
Disable download progress
martincostello Feb 13, 2022
475b7ba
Skip net4xx tests on non-Windows
martincostello Feb 13, 2022
47a2b5d
Drop test target for netcoreapp2.1
martincostello Feb 13, 2022
440955d
Speed up CI
martincostello Feb 13, 2022
28ebb94
Fix NullReferenceException (#923)
FoxTes Feb 14, 2022
e327306
Use file-scoped namespaces
martincostello Dec 16, 2022
053d891
Create .git-blame-ignore-revs
martincostello Dec 16, 2022
80002fe
Revert "expression body members"
martincostello Dec 16, 2022
63b151e
Revert "file scoped namespaces"
martincostello Dec 16, 2022
a792446
Revert "use var"
martincostello Dec 16, 2022
78c9860
Merge branch 'v724-or-v730' into undo-syntax-changes
martincostello Dec 16, 2022
35cf357
Update .git-blame-ignore-revs
martincostello Dec 16, 2022
b08876b
Add GitHub Actions CI
martincostello Dec 16, 2022
6f4d997
Add build matrix
martincostello Dec 16, 2022
1073e82
Remove macOS and Linux
martincostello Dec 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Prev Previous commit
Next Next commit
Use file-scoped namespaces
Use file-scoped namespaces to try and resolve the merge conflicts with the default branch.
  • Loading branch information
martincostello committed Dec 16, 2022
commit e32730600746b479db0241826b9bdef8eec9bb28
81 changes: 40 additions & 41 deletions src/Polly.Benchmarks/Bulkhead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,47 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;

namespace Polly.Benchmarks
namespace Polly.Benchmarks;

[Config(typeof(PollyConfig))]
public class Bulkhead
{
[Config(typeof(PollyConfig))]
public class Bulkhead
private static readonly Policy SyncPolicy = Policy.Bulkhead(2);
private static readonly AsyncPolicy AsyncPolicy = Policy.BulkheadAsync(2);

[Benchmark]
public void Bulkhead_Synchronous()
{
SyncPolicy.Execute(() => Workloads.Action());
}

[Benchmark]
public async Task Bulkhead_Asynchronous()
{
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
}

[Benchmark]
public async Task Bulkhead_Asynchronous_With_CancellationToken()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
public int Bulkhead_Synchronous_With_Result()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result_With_CancellationToken()
{
private static readonly Policy SyncPolicy = Policy.Bulkhead(2);
private static readonly AsyncPolicy AsyncPolicy = Policy.BulkheadAsync(2);

[Benchmark]
public void Bulkhead_Synchronous()
{
SyncPolicy.Execute(() => Workloads.Action());
}

[Benchmark]
public async Task Bulkhead_Asynchronous()
{
await AsyncPolicy.ExecuteAsync(() => Workloads.ActionAsync());
}

[Benchmark]
public async Task Bulkhead_Asynchronous_With_CancellationToken()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
public int Bulkhead_Synchronous_With_Result()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}

[Benchmark]
public async Task<int> Bulkhead_Asynchronous_With_Result_With_CancellationToken()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
}
145 changes: 72 additions & 73 deletions src/Polly.Benchmarks/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,107 +5,106 @@
using Microsoft.Extensions.Caching.Memory;
using Polly.Caching;

namespace Polly.Benchmarks
namespace Polly.Benchmarks;

[Config(typeof(PollyConfig))]
public class Cache
{
[Config(typeof(PollyConfig))]
public class Cache
private static readonly MemoryCache MemoryCache = new MemoryCache(new MemoryCacheOptions());
private static readonly MemoryCacheProvider CacheProvider = new MemoryCacheProvider(MemoryCache);

private static readonly Policy SyncPolicyMiss = Policy.Cache(CacheProvider, TimeSpan.Zero);
private static readonly AsyncPolicy AsyncPolicyMiss = Policy.CacheAsync(CacheProvider, TimeSpan.Zero);

private static readonly Policy SyncPolicyHit = Policy.Cache(CacheProvider, TimeSpan.MaxValue);
private static readonly AsyncPolicy AsyncPolicyHit = Policy.CacheAsync(CacheProvider, TimeSpan.MaxValue);

private static readonly Context HitContext = new Context(nameof(HitContext));
private static readonly Context MissContext = new Context(nameof(MissContext));

[GlobalSetup]
public async Task GlobalSetup()
{
private static readonly MemoryCache MemoryCache = new MemoryCache(new MemoryCacheOptions());
private static readonly MemoryCacheProvider CacheProvider = new MemoryCacheProvider(MemoryCache);
SyncPolicyHit.Execute((context) => GetObject(), HitContext);
await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}

private static readonly Policy SyncPolicyMiss = Policy.Cache(CacheProvider, TimeSpan.Zero);
private static readonly AsyncPolicy AsyncPolicyMiss = Policy.CacheAsync(CacheProvider, TimeSpan.Zero);
[Benchmark]
public object Cache_Synchronous_Hit()
{
return SyncPolicyHit.Execute((context) => GetObject(), HitContext);
}

private static readonly Policy SyncPolicyHit = Policy.Cache(CacheProvider, TimeSpan.MaxValue);
private static readonly AsyncPolicy AsyncPolicyHit = Policy.CacheAsync(CacheProvider, TimeSpan.MaxValue);
[Benchmark]
public async Task<object> Cache_Asynchronous_Hit()
{
return await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}

private static readonly Context HitContext = new Context(nameof(HitContext));
private static readonly Context MissContext = new Context(nameof(MissContext));
[Benchmark]
public object Cache_Synchronous_Miss()
{
return SyncPolicyMiss.Execute((context) => GetObject(), MissContext);
}

[GlobalSetup]
public async Task GlobalSetup()
{
SyncPolicyHit.Execute((context) => GetObject(), HitContext);
await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}
[Benchmark]
public async Task<object> Cache_Asynchronous_Miss()
{
return await AsyncPolicyMiss.ExecuteAsync((context, token) => GetObjectAsync(token), MissContext, CancellationToken.None);
}

[Benchmark]
public object Cache_Synchronous_Hit()
{
return SyncPolicyHit.Execute((context) => GetObject(), HitContext);
}
private static object GetObject() => new object();

[Benchmark]
public async Task<object> Cache_Asynchronous_Hit()
{
return await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None);
}
private static Task<object> GetObjectAsync(CancellationToken cancellationToken) => Task.FromResult(new object());

[Benchmark]
public object Cache_Synchronous_Miss()
private sealed class MemoryCacheProvider : ISyncCacheProvider, IAsyncCacheProvider
{
private readonly IMemoryCache _cache;

public MemoryCacheProvider(IMemoryCache memoryCache)
{
return SyncPolicyMiss.Execute((context) => GetObject(), MissContext);
_cache = memoryCache;
}

[Benchmark]
public async Task<object> Cache_Asynchronous_Miss()
public (bool, object) TryGet(string key)
{
return await AsyncPolicyMiss.ExecuteAsync((context, token) => GetObjectAsync(token), MissContext, CancellationToken.None);
bool cacheHit = _cache.TryGetValue(key, out var value);
return (cacheHit, value);
}

private static object GetObject() => new object();

private static Task<object> GetObjectAsync(CancellationToken cancellationToken) => Task.FromResult(new object());

private sealed class MemoryCacheProvider : ISyncCacheProvider, IAsyncCacheProvider
public void Put(string key, object value, Ttl ttl)
{
private readonly IMemoryCache _cache;

public MemoryCacheProvider(IMemoryCache memoryCache)
{
_cache = memoryCache;
}
TimeSpan remaining = DateTimeOffset.MaxValue - DateTimeOffset.UtcNow;
var options = new MemoryCacheEntryOptions();

public (bool, object) TryGet(string key)
if (ttl.SlidingExpiration)
{
bool cacheHit = _cache.TryGetValue(key, out var value);
return (cacheHit, value);
options.SlidingExpiration = ttl.Timespan < remaining ? ttl.Timespan : remaining;
}

public void Put(string key, object value, Ttl ttl)
else
{
TimeSpan remaining = DateTimeOffset.MaxValue - DateTimeOffset.UtcNow;
var options = new MemoryCacheEntryOptions();

if (ttl.SlidingExpiration)
if (ttl.Timespan == TimeSpan.MaxValue)
{
options.SlidingExpiration = ttl.Timespan < remaining ? ttl.Timespan : remaining;
options.AbsoluteExpiration = DateTimeOffset.MaxValue;
}
else
{
if (ttl.Timespan == TimeSpan.MaxValue)
{
options.AbsoluteExpiration = DateTimeOffset.MaxValue;
}
else
{
options.AbsoluteExpirationRelativeToNow = ttl.Timespan < remaining ? ttl.Timespan : remaining;
}
options.AbsoluteExpirationRelativeToNow = ttl.Timespan < remaining ? ttl.Timespan : remaining;
}

_cache.Set(key, value, options);
}

public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
return Task.FromResult(TryGet(key));
}
_cache.Set(key, value, options);
}

public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
Put(key, value, ttl);
return Task.CompletedTask;
}
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
return Task.FromResult(TryGet(key));
}

public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
Put(key, value, ttl);
return Task.CompletedTask;
}
}
}
51 changes: 25 additions & 26 deletions src/Polly.Benchmarks/CircuitBreaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;

namespace Polly.Benchmarks
namespace Polly.Benchmarks;

[Config(typeof(PollyConfig))]
public class CircuitBreaker
{
[Config(typeof(PollyConfig))]
public class CircuitBreaker
{
private static readonly Policy SyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreaker(2, TimeSpan.FromMinutes(1));
private static readonly AsyncPolicy AsyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreakerAsync(2, TimeSpan.FromMinutes(1));
private static readonly Policy SyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreaker(2, TimeSpan.FromMinutes(1));
private static readonly AsyncPolicy AsyncPolicy = Policy.Handle<InvalidOperationException>().CircuitBreakerAsync(2, TimeSpan.FromMinutes(1));

[Benchmark]
public void CircuitBreaker_Synchronous_Succeeds()
{
SyncPolicy.Execute(() => Workloads.Action());
}
[Benchmark]
public void CircuitBreaker_Synchronous_Succeeds()
{
SyncPolicy.Execute(() => Workloads.Action());
}

[Benchmark]
public async Task CircuitBreaker_Asynchronous_Succeeds()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}
[Benchmark]
public async Task CircuitBreaker_Asynchronous_Succeeds()
{
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None);
}

[Benchmark]
public int CircuitBreaker_Synchronous_With_Result_Succeeds()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}
[Benchmark]
public int CircuitBreaker_Synchronous_With_Result_Succeeds()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}

[Benchmark]
public async Task<int> CircuitBreaker_Asynchronous_With_Result_Succeeds()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
[Benchmark]
public async Task<int> CircuitBreaker_Asynchronous_With_Result_Succeeds()
{
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None);
}
}
51 changes: 25 additions & 26 deletions src/Polly.Benchmarks/Fallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;

namespace Polly.Benchmarks
namespace Polly.Benchmarks;

[Config(typeof(PollyConfig))]
public class Fallback
{
[Config(typeof(PollyConfig))]
public class Fallback
{
private static readonly Policy<int> SyncPolicy = Policy<int>.Handle<InvalidOperationException>().Fallback(0);
private static readonly AsyncPolicy<int> AsyncPolicy = Policy<int>.Handle<InvalidOperationException>().FallbackAsync(0);
private static readonly Policy<int> SyncPolicy = Policy<int>.Handle<InvalidOperationException>().Fallback(0);
private static readonly AsyncPolicy<int> AsyncPolicy = Policy<int>.Handle<InvalidOperationException>().FallbackAsync(0);

[Benchmark]
public int Fallback_Synchronous_Succeeds()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}
[Benchmark]
public int Fallback_Synchronous_Succeeds()
{
return SyncPolicy.Execute(() => Workloads.Func<int>());
}

[Benchmark]
public async Task<int> Fallback_Asynchronous_Succeeds()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}
[Benchmark]
public async Task<int> Fallback_Asynchronous_Succeeds()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncAsync<int>());
}

[Benchmark]
public int Fallback_Synchronous_Throws()
{
return SyncPolicy.Execute(() => Workloads.FuncThrows<int, InvalidOperationException>());
}
[Benchmark]
public int Fallback_Synchronous_Throws()
{
return SyncPolicy.Execute(() => Workloads.FuncThrows<int, InvalidOperationException>());
}

[Benchmark]
public async Task<int> Fallback_Asynchronous_Throws()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncThrowsAsync<int, InvalidOperationException>());
}
[Benchmark]
public async Task<int> Fallback_Asynchronous_Throws()
{
return await AsyncPolicy.ExecuteAsync(() => Workloads.FuncThrowsAsync<int, InvalidOperationException>());
}
}
Loading