-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #912 from App-vNext/v723-or-v730
v7.3.0: Rate-limit policy and codebase maintenance
- Loading branch information
Showing
159 changed files
with
4,879 additions
and
2,648 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,7 @@ UpgradeLog*.XML | |
|
||
artifacts | ||
build | ||
BenchmarkDotNet.Artifacts | ||
tools | ||
|
||
*.lock.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
next-version: 7.2.2 | ||
next-version: 7.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
os: Visual Studio 2019 | ||
os: Visual Studio 2022 | ||
|
||
# Build script | ||
build_script: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Polly.Benchmarks | ||
{ | ||
[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() | ||
{ | ||
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Polly.Caching; | ||
|
||
namespace Polly.Benchmarks | ||
{ | ||
[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() | ||
{ | ||
SyncPolicyHit.Execute((context) => GetObject(), HitContext); | ||
await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None); | ||
} | ||
|
||
[Benchmark] | ||
public object Cache_Synchronous_Hit() | ||
{ | ||
return SyncPolicyHit.Execute((context) => GetObject(), HitContext); | ||
} | ||
|
||
[Benchmark] | ||
public async Task<object> Cache_Asynchronous_Hit() | ||
{ | ||
return await AsyncPolicyHit.ExecuteAsync((context, token) => GetObjectAsync(token), HitContext, CancellationToken.None); | ||
} | ||
|
||
[Benchmark] | ||
public object Cache_Synchronous_Miss() | ||
{ | ||
return SyncPolicyMiss.Execute((context) => GetObject(), MissContext); | ||
} | ||
|
||
[Benchmark] | ||
public async Task<object> Cache_Asynchronous_Miss() | ||
{ | ||
return await AsyncPolicyMiss.ExecuteAsync((context, token) => GetObjectAsync(token), MissContext, CancellationToken.None); | ||
} | ||
|
||
private static object GetObject() => new object(); | ||
|
||
private static Task<object> GetObjectAsync(CancellationToken cancellationToken) => Task.FromResult(new object()); | ||
|
||
private sealed class MemoryCacheProvider : ISyncCacheProvider, IAsyncCacheProvider | ||
{ | ||
private readonly IMemoryCache _cache; | ||
|
||
public MemoryCacheProvider(IMemoryCache memoryCache) | ||
{ | ||
_cache = memoryCache; | ||
} | ||
|
||
public (bool, object) TryGet(string key) | ||
{ | ||
bool cacheHit = _cache.TryGetValue(key, out var value); | ||
return (cacheHit, value); | ||
} | ||
|
||
public void Put(string key, object value, Ttl ttl) | ||
{ | ||
TimeSpan remaining = DateTimeOffset.MaxValue - DateTimeOffset.UtcNow; | ||
var options = new MemoryCacheEntryOptions(); | ||
|
||
if (ttl.SlidingExpiration) | ||
{ | ||
options.SlidingExpiration = ttl.Timespan < remaining ? ttl.Timespan : remaining; | ||
} | ||
else | ||
{ | ||
if (ttl.Timespan == TimeSpan.MaxValue) | ||
{ | ||
options.AbsoluteExpiration = DateTimeOffset.MaxValue; | ||
} | ||
else | ||
{ | ||
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)); | ||
} | ||
|
||
public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext) | ||
{ | ||
Put(key, value, ttl); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Polly.Benchmarks | ||
{ | ||
[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)); | ||
|
||
[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 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Polly.Benchmarks | ||
{ | ||
[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); | ||
|
||
[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 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>()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Polly.Benchmarks | ||
{ | ||
[Config(typeof(PollyConfig))] | ||
public class NoOp | ||
{ | ||
private static readonly Policy SyncPolicy = Policy.NoOp(); | ||
private static readonly AsyncPolicy AsyncPolicy = Policy.NoOpAsync(); | ||
|
||
[Benchmark] | ||
public void NoOp_Synchronous() | ||
{ | ||
SyncPolicy.Execute(() => Workloads.Action()); | ||
} | ||
|
||
[Benchmark] | ||
public async Task NoOp_Asynchronous() | ||
{ | ||
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None); | ||
} | ||
|
||
[Benchmark] | ||
public int NoOp_Synchronous_With_Result() | ||
{ | ||
return SyncPolicy.Execute(() => Workloads.Func<int>()); | ||
} | ||
|
||
[Benchmark] | ||
public async Task<int> NoOp_Asynchronous_With_Result() | ||
{ | ||
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Polly.Benchmarks | ||
{ | ||
[Config(typeof(PollyConfig))] | ||
public class PolicyWrap | ||
{ | ||
private static readonly Policy SyncPolicy = Policy.Wrap( | ||
Policy.Handle<InvalidOperationException>().Retry(), | ||
Policy.Handle<InvalidOperationException>().CircuitBreaker(2, TimeSpan.FromMinutes(1)), | ||
Policy.Timeout(TimeSpan.FromMilliseconds(10)), | ||
Policy.Bulkhead(2)); | ||
|
||
private static readonly AsyncPolicy AsyncPolicy = Policy.WrapAsync( | ||
Policy.Handle<InvalidOperationException>().RetryAsync(), | ||
Policy.Handle<InvalidOperationException>().CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)), | ||
Policy.TimeoutAsync(TimeSpan.FromMilliseconds(10)), | ||
Policy.BulkheadAsync(2)); | ||
|
||
[Benchmark] | ||
public void PolicyWrap_Synchronous() | ||
{ | ||
SyncPolicy.Execute(() => Workloads.Action()); | ||
} | ||
|
||
[Benchmark] | ||
public async Task PolicyWrap_Asynchronous() | ||
{ | ||
await AsyncPolicy.ExecuteAsync((token) => Workloads.ActionAsync(token), CancellationToken.None); | ||
} | ||
|
||
[Benchmark] | ||
public int PolicyWrap_Synchronous_With_Result() | ||
{ | ||
return SyncPolicy.Execute(() => Workloads.Func<int>()); | ||
} | ||
|
||
[Benchmark] | ||
public async Task<int> PolicyWrap_Asynchronous_With_Result() | ||
{ | ||
return await AsyncPolicy.ExecuteAsync((token) => Workloads.FuncAsync<int>(token), CancellationToken.None); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
<LangVersion>latest</LangVersion> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.0" /> | ||
</ItemGroup> | ||
<ItemGroup Condition=" '$(BenchmarkFromNuGet)' != 'True' "> | ||
<ProjectReference Include="..\Polly\Polly.csproj" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.