-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
CancellationTokenExtentions.cs
123 lines (110 loc) · 5.15 KB
/
CancellationTokenExtentions.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// This file is part of Hangfire. Copyright © 2018 Hangfire OÜ.
//
// Hangfire is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3
// of the License, or any later version.
//
// Hangfire is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with Hangfire. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Diagnostics;
using System.Threading;
using Hangfire.Logging;
namespace Hangfire.Common
{
public static class CancellationTokenExtentions
{
/// <summary>
/// Returns a class that contains a <see cref="EventWaitHandle"/> that is set, when
/// the given <paramref name="cancellationToken"/> is canceled. This method is based
/// on cancellation token registration and avoids using the <see cref="CancellationToken.WaitHandle"/>
/// property as it may lead to high CPU issues.
/// </summary>
[Obsolete("CancellationToken.WaitHandle is now preferred, since early days of .NET Core passed. Will be removed in 2.0.0.")]
public static CancellationEvent GetCancellationEvent(this CancellationToken cancellationToken)
{
return new CancellationEvent(cancellationToken);
}
/// <summary>
/// Performs a wait until the specified <paramref name="timeout"/> is elapsed or the
/// given cancellation token is canceled and throw <see cref="OperationCanceledException"/>
/// exception if wait succeeded. The wait is performed on a dedicated event
/// wait handle to avoid using the <see cref="CancellationToken.WaitHandle"/> property
/// that may lead to high CPU issues.
/// </summary>
public static void WaitOrThrow(this CancellationToken cancellationToken, TimeSpan timeout)
{
if (Wait(cancellationToken, timeout))
{
throw new OperationCanceledException(cancellationToken);
}
}
/// <summary>
/// Performs a wait until the specified <paramref name="timeout"/> is elapsed or the
/// given cancellation token is canceled. The wait is performed on a dedicated event
/// wait handle to avoid using the <see cref="CancellationToken.WaitHandle"/> property
/// that may lead to high CPU issues.
/// </summary>
public static bool Wait(this CancellationToken cancellationToken, TimeSpan timeout)
{
var stopwatch = Stopwatch.StartNew();
var waitResult = cancellationToken.WaitHandle.WaitOne(timeout);
stopwatch.Stop();
var timeoutThreshold = TimeSpan.FromMilliseconds(1000);
var elapsedThreshold = TimeSpan.FromMilliseconds(500);
var protectionTime = TimeSpan.FromSeconds(1);
if (!cancellationToken.IsCancellationRequested &&
timeout >= timeoutThreshold &&
stopwatch.Elapsed < elapsedThreshold)
{
try
{
var logger = LogProvider.GetLogger(typeof(CancellationTokenExtentions));
logger.Error($"Actual wait time for non-canceled token was '{stopwatch.Elapsed.TotalMilliseconds}' ms instead of '{timeout.TotalMilliseconds}' ms, wait result: {waitResult}, using protective wait. Please report this to Hangfire developers.");
}
finally
{
Thread.Sleep(protectionTime);
}
}
return waitResult;
}
[Obsolete("CancellationToken.WaitHandle is now preferred, since early days of .NET Core passed. Will be removed in 2.0.0.")]
public sealed class CancellationEvent : IDisposable
{
private static readonly Action<object> SetEventCallback = SetEvent;
private readonly ManualResetEvent _mre;
private CancellationTokenRegistration _registration;
public CancellationEvent(CancellationToken cancellationToken)
{
_mre = new ManualResetEvent(false);
_registration = cancellationToken.Register(SetEventCallback, _mre);
}
public EventWaitHandle WaitHandle => _mre;
public void Dispose()
{
_registration.Dispose();
_mre.Dispose();
}
private static void SetEvent(object state)
{
try
{
((ManualResetEvent)state).Set();
}
catch (ObjectDisposedException)
{
// When our event instance is already disposed, we already
// aren't interested in any notifications. This statement
// is just to ensure we don't throw any exceptions.
}
}
}
}
}