Skip to content

Commit d8ce39f

Browse files
committed
Adopt System.TimeProvider
1 parent f4d5347 commit d8ce39f

File tree

12 files changed

+40
-72
lines changed

12 files changed

+40
-72
lines changed

src/Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<PackageVersion Include="BenchmarkDotNet" Version="0.13.5" />
55
<PackageVersion Include="FluentAssertions" Version="6.11.0" />
66
<PackageVersion Include="GitHubActionsTestLogger" Version="2.1.0" />
7-
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" />
7+
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
8+
<PackageVersion Include="Microsoft.Bcl.TimeProvider" Version="8.0.0-preview.4.23259.5" />
89
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.1" />
910
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
1011
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />

src/Polly.Core.Tests/Hedging/HedgingTimeProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Threading.Tasks;
3-
using Polly.Utils;
43

54
namespace Polly.Core.Tests.Hedging;
65

@@ -25,15 +24,16 @@ public void Advance(TimeSpan diff)
2524

2625
public List<DelayEntry> DelayEntries { get; } = new List<DelayEntry>();
2726

28-
public override DateTimeOffset UtcNow => _utcNow;
27+
public override DateTimeOffset GetUtcNow() => _utcNow;
2928

30-
public override void CancelAfter(CancellationTokenSource source, TimeSpan delay)
29+
public override ITimer CreateTimer(TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period)
3130
{
32-
throw new NotSupportedException();
31+
return base.CreateTimer(callback, state, dueTime, period);
3332
}
3433

3534
public override Task Delay(TimeSpan delayValue, CancellationToken cancellationToken = default)
3635
{
36+
3737
var entry = new DelayEntry(delayValue, new TaskCompletionSource<bool>(), _utcNow.Add(delayValue));
3838
cancellationToken.Register(() => entry.Source.TrySetCanceled(cancellationToken));
3939
DelayEntries.Add(entry);

src/Polly.Core/CircuitBreaker/Controller/CircuitStateController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ private void CloseCircuit_NeedsLock<TResult>(Outcome<TResult> outcome, bool manu
272272

273273
private bool PermitHalfOpenCircuitTest_NeedsLock()
274274
{
275-
var now = _timeProvider.UtcNow;
275+
var now = _timeProvider.GetUtcNow();
276276
if (now >= _blockedUntil)
277277
{
278278
_blockedUntil = now + _breakDuration;
@@ -307,7 +307,7 @@ private void OpenCircuit_NeedsLock<TResult>(Outcome<TResult> outcome, bool manua
307307
private void OpenCircuitFor_NeedsLock<TResult>(Outcome<TResult> outcome, TimeSpan breakDuration, bool manual, ResilienceContext context, out Task? scheduledTask)
308308
{
309309
scheduledTask = null;
310-
var utcNow = _timeProvider.UtcNow;
310+
var utcNow = _timeProvider.GetUtcNow();
311311

312312
_blockedUntil = IsDateTimeOverflow(utcNow, breakDuration) ? DateTimeOffset.MaxValue : utcNow + breakDuration;
313313

src/Polly.Core/CircuitBreaker/Health/RollingHealthMetrics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override HealthInfo GetHealthInfo()
4444

4545
private HealthWindow UpdateCurrentWindow()
4646
{
47-
var now = TimeProvider.UtcNow;
47+
var now = TimeProvider.GetUtcNow();
4848
if (_currentWindow == null || now - _currentWindow.StartedAt >= _windowDuration)
4949
{
5050
_currentWindow = new()

src/Polly.Core/CircuitBreaker/Health/SingleHealthMetrics.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public SingleHealthMetrics(TimeSpan samplingDuration, TimeProvider timeProvider)
1515
: base(timeProvider)
1616
{
1717
_samplingDuration = samplingDuration;
18-
_startedAt = timeProvider.UtcNow;
18+
_startedAt = timeProvider.GetUtcNow();
1919
}
2020

2121
public override void IncrementSuccess()
@@ -32,7 +32,7 @@ public override void IncrementFailure()
3232

3333
public override void Reset()
3434
{
35-
_startedAt = TimeProvider.UtcNow;
35+
_startedAt = TimeProvider.GetUtcNow();
3636
_successes = 0;
3737
_failures = 0;
3838
}
@@ -46,7 +46,7 @@ public override HealthInfo GetHealthInfo()
4646

4747
private void TryReset()
4848
{
49-
if (TimeProvider.UtcNow - _startedAt >= _samplingDuration)
49+
if (TimeProvider.GetUtcNow() - _startedAt >= _samplingDuration)
5050
{
5151
Reset();
5252
}

src/Polly.Core/Hedging/Controller/HedgingController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public HedgingController(TimeProvider provider, HedgingHandler.Handler handler,
1515
_executionPool = new ObjectPool<TaskExecution>(() =>
1616
{
1717
Interlocked.Increment(ref _rentedExecutions);
18-
return new TaskExecution(handler);
18+
return new TaskExecution(handler, provider);
1919
},
2020
_ =>
2121
{

src/Polly.Core/Hedging/Controller/TaskExecution.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ internal sealed class TaskExecution
2323
{
2424
private readonly ResilienceContext _cachedContext = ResilienceContext.Get();
2525
private readonly HedgingHandler.Handler _handler;
26+
private readonly TimeProvider _timeProvider;
2627
private CancellationTokenSource? _cancellationSource;
2728
private CancellationTokenRegistration? _cancellationRegistration;
2829
private ResilienceContext? _activeContext;
2930

30-
public TaskExecution(HedgingHandler.Handler handler) => _handler = handler;
31+
public TaskExecution(HedgingHandler.Handler handler, TimeProvider timeProvider)
32+
{
33+
_handler = handler;
34+
_timeProvider = timeProvider;
35+
}
3136

3237
/// <summary>
3338
/// Gets the task that represents the execution of the hedged task.
@@ -81,7 +86,7 @@ public async ValueTask<bool> InitializeAsync<TResult, TState>(
8186
int attempt)
8287
{
8388
Type = type;
84-
_cancellationSource = CancellationTokenSourcePool.Get();
89+
_cancellationSource = CancellationTokenSourcePool.Get(TimeSpan.Zero, _timeProvider);
8590
Properties.Replace(snapshot.OriginalProperties);
8691

8792
if (snapshot.OriginalCancellationToken.CanBeCanceled)

src/Polly.Core/Hedging/HedgingResilienceStrategy.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Polly.Hedging;
66
using Polly.Hedging.Utils;
77
using Polly.Strategy;
8-
using Polly.Utils;
98

109
namespace Polly;
1110

src/Polly.Core/Polly.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
<ItemGroup>
2626
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Condition="!$([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'netcoreapp3.1'))" />
27+
<PackageReference Include="Microsoft.Bcl.TimeProvider" />
2728
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="!$([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'netcoreapp3.1'))" />
2829
<PackageReference Include="System.ValueTuple" Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETFramework'" />
2930
<PackageReference Include="System.ComponentModel.Annotations" Condition="!$([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'netcoreapp3.1'))" />

src/Polly.Core/Timeout/TimeoutResilienceStrategy.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ protected internal override async ValueTask<TResult> ExecuteCoreAsync<TResult, T
3535
}
3636

3737
var previousToken = context.CancellationToken;
38-
var cancellationSource = CancellationTokenSourcePool.Get();
39-
_timeProvider.CancelAfter(cancellationSource, timeout);
38+
var cancellationSource = CancellationTokenSourcePool.Get(timeout, _timeProvider);
4039
context.CancellationToken = cancellationSource.Token;
4140

4241
CancellationTokenRegistration? registration = null;

0 commit comments

Comments
 (0)