-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
BatchScheduler.cs
229 lines (201 loc) · 5.89 KB
/
BatchScheduler.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using GreenDonut;
using static System.Threading.Interlocked;
namespace HotChocolate.Fetching;
/// <summary>
/// The execution engine batch dispatcher.
/// </summary>
public class BatchScheduler
: IBatchScheduler
, IBatchDispatcher
{
private static List<Func<ValueTask>>? _localTasks;
private static List<Task<Exception?>>? _localProcessing;
private const int _waitTimeout = 30_000;
private readonly SemaphoreSlim _semaphore = new(1, 1);
private readonly object _sync = new();
private readonly object _syncTaskEnqueued = new();
private readonly List<Func<ValueTask>> _tasks = new();
private bool _dispatchOnSchedule;
private readonly List<EventHandler> _listeners = new();
/// <inheritdoc />
public event EventHandler TaskEnqueued
{
add
{
lock (_syncTaskEnqueued)
{
_listeners.Add(value);
}
}
remove
{
lock (_syncTaskEnqueued)
{
_listeners.Remove(value);
}
}
}
/// <inheritdoc />
public bool DispatchOnSchedule
{
get => _dispatchOnSchedule;
set
{
lock (_sync)
{
_dispatchOnSchedule = value;
}
}
}
public void Schedule(Func<ValueTask> dispatch)
{
bool dispatchOnSchedule;
lock (_sync)
{
if (_dispatchOnSchedule)
{
dispatchOnSchedule = true;
}
else
{
dispatchOnSchedule = false;
_tasks.Add(dispatch);
}
}
if (dispatchOnSchedule)
{
BeginDispatchOnEnqueue(dispatch);
}
else
{
lock (_syncTaskEnqueued)
{
for (var i = 0; i < _listeners.Count; i++)
{
try
{
_listeners[i].Invoke(this, EventArgs.Empty);
}
catch
{
// ignored
}
}
}
}
}
/// <inheritdoc />
public void BeginDispatch(CancellationToken cancellationToken = default)
{
List<Func<ValueTask>>? tasks = null;
Func<ValueTask>? task = null;
lock (_sync)
{
switch (_tasks.Count)
{
case 0:
return;
case 1:
task = _tasks[0];
_tasks.Clear();
break;
default:
// we will try to reuse the pooled list.
tasks = Exchange(ref _localTasks, null) ?? new();
tasks.AddRange(_tasks);
_tasks.Clear();
break;
}
}
if (task is not null)
{
BeginProcessTask(task, cancellationToken);
}
else if (tasks is not null)
{
BeginProcessTasks(tasks, cancellationToken);
}
}
#pragma warning disable 4014
private void BeginProcessTask(
Func<ValueTask> task,
CancellationToken cancellationToken = default)
=> ProcessTaskAsync(task, cancellationToken);
#pragma warning restore 4014
private async Task ProcessTaskAsync(
Func<ValueTask> task,
CancellationToken cancellationToken = default)
{
await Task.Yield();
await ExecuteBatchAsync(task, cancellationToken).ConfigureAwait(false);
}
#pragma warning disable 4014
private void BeginProcessTasks(
List<Func<ValueTask>> tasks,
CancellationToken cancellationToken = default)
=> ProcessTasksAsync(tasks, cancellationToken);
#pragma warning restore 4014
private async Task ProcessTasksAsync(
List<Func<ValueTask>> tasks,
CancellationToken cancellationToken = default)
{
await Task.Yield();
// First we will get a list to hold on to the tasks.
var processing = Exchange(ref _localProcessing, null) ?? new();
foreach (var task in tasks)
{
processing.Add(ExecuteBatchAsync(task, cancellationToken));
}
await Task.WhenAll(processing).ConfigureAwait(false);
processing.Clear();
tasks.Clear();
// if there is no new instance for processing we will return our processing instances.
CompareExchange(ref _localProcessing, processing, null);
CompareExchange(ref _localTasks, tasks, null);
}
private static async Task<Exception?> ExecuteBatchAsync(
Func<ValueTask> executeBatchAsync,
CancellationToken cancellationToken)
{
try
{
await executeBatchAsync().ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// If we run into this exception the request was aborted.
// In this case we do nothing and just return.
}
catch (Exception ex)
{
if (cancellationToken.IsCancellationRequested)
{
// if cancellation is request we do no longer report errors to the
// operation context.
return null;
}
return ex;
}
return null;
}
#pragma warning disable 4014
private void BeginDispatchOnEnqueue(Func<ValueTask> dispatch)
=> DispatchOnEnqueue(dispatch);
#pragma warning restore 4014
private async Task DispatchOnEnqueue(Func<ValueTask> dispatch)
{
await _semaphore.WaitAsync(_waitTimeout).ConfigureAwait(false);
try
{
await dispatch().ConfigureAwait(false);
}
finally
{
_semaphore.Release();
}
}
}