-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
DefaultRequestDispatcher.cs
467 lines (397 loc) · 16.7 KB
/
DefaultRequestDispatcher.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime;
namespace Microsoft.NET.Sdk.Razor.Tool
{
// Heavily influenced by:
// https://github.com/dotnet/roslyn/blob/14aed138a01c448143b9acf0fe77a662e3dfe2f4/src/Compilers/Server/ServerShared/ServerDispatcher.cs#L15
internal class DefaultRequestDispatcher : RequestDispatcher
{
private readonly CancellationToken _cancellationToken;
private readonly CompilerHost _compilerHost;
private readonly ConnectionHost _connectionHost;
private readonly EventBus _eventBus;
private KeepAlive _keepAlive;
private State _state;
private Task _timeoutTask;
private Task _gcTask;
private Task<Connection> _listenTask;
private CancellationTokenSource _listenCancellationTokenSource;
private List<Task<ConnectionResult>> _connections = new List<Task<ConnectionResult>>();
public DefaultRequestDispatcher(
ConnectionHost connectionHost,
CompilerHost compilerHost,
CancellationToken cancellationToken,
EventBus eventBus = null,
TimeSpan? keepAlive = null)
{
_connectionHost = connectionHost;
_compilerHost = compilerHost;
_cancellationToken = cancellationToken;
_eventBus = eventBus ?? EventBus.Default;
var keepAliveTimeout = DefaultServerKeepAlive;
if (keepAlive.HasValue)
{
keepAliveTimeout = keepAlive.Value;
}
_keepAlive = new KeepAlive(keepAliveTimeout, isDefault: true);
}
// The server accepts connections until we reach a state that requires a shutdown. At that
// time no new connections will be accepted and the server will drain existing connections.
//
// The idea is that it's better to let clients fallback to in-proc (and slow down) than it is to keep
// running in an undesired state.
public override void Run()
{
_state = State.Running;
try
{
Listen();
do
{
Debug.Assert(_listenTask != null);
MaybeCreateTimeoutTask();
MaybeCreateGCTask();
WaitForAnyCompletion(_cancellationToken);
CheckCompletedTasks(_cancellationToken);
}
while (_connections.Count > 0 || _state == State.Running);
}
finally
{
_state = State.Completed;
_gcTask = null;
_timeoutTask = null;
if (_listenTask != null)
{
CloseListenTask();
}
}
}
private void CheckCompletedTasks(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
HandleCancellation();
return;
}
if (_listenTask.IsCompleted)
{
HandleCompletedListenTask(cancellationToken);
}
if (_timeoutTask?.IsCompleted == true)
{
HandleCompletedTimeoutTask();
}
if (_gcTask?.IsCompleted == true)
{
HandleCompletedGCTask();
}
HandleCompletedConnections();
}
private void HandleCancellation()
{
Debug.Assert(_listenTask != null);
// If cancellation has been requested then the server needs to be in the process
// of shutting down.
_state = State.ShuttingDown;
CloseListenTask();
try
{
Task.WaitAll(_connections.ToArray());
}
catch
{
// It's expected that some will throw exceptions, in particular OperationCanceledException. It's
// okay for them to throw so long as they complete.
}
HandleCompletedConnections();
Debug.Assert(_connections.Count == 0);
}
/// <summary>
/// The server farms out work to Task values and this method needs to wait until at least one of them
/// has completed.
/// </summary>
private void WaitForAnyCompletion(CancellationToken cancellationToken)
{
var all = new List<Task>(_connections.Count + 3);
all.AddRange(_connections);
all.Add(_timeoutTask);
all.Add(_listenTask);
all.Add(_gcTask);
try
{
var waitArray = all.Where(x => x != null).ToArray();
Task.WaitAny(waitArray, cancellationToken);
}
catch (OperationCanceledException)
{
// Thrown when the provided cancellationToken is cancelled. This is handled in the caller,
// here it just serves to break out of the WaitAny call.
}
}
private void Listen()
{
Debug.Assert(_listenTask == null);
Debug.Assert(_timeoutTask == null);
_listenCancellationTokenSource = new CancellationTokenSource();
_listenTask = _connectionHost.WaitForConnectionAsync(_listenCancellationTokenSource.Token);
_eventBus.ConnectionListening();
}
private void CloseListenTask()
{
Debug.Assert(_listenTask != null);
_listenCancellationTokenSource.Cancel();
_listenCancellationTokenSource = null;
_listenTask = null;
}
private void HandleCompletedListenTask(CancellationToken cancellationToken)
{
_eventBus.ConnectionReceived();
// Don't accept any new connections once we're in shutdown mode, instead gracefully reject the request.
// This should cause the client to run in process.
var accept = _state == State.Running;
var connectionTask = AcceptConnection(_listenTask, accept, cancellationToken);
_connections.Add(connectionTask);
// Timeout and GC are only done when there are no active connections. Now that we have a new
// connection cancel out these tasks.
_timeoutTask = null;
_gcTask = null;
// Begin listening again for new connections.
_listenTask = null;
Listen();
}
private void HandleCompletedTimeoutTask()
{
_eventBus.KeepAliveReached();
_listenCancellationTokenSource.Cancel();
_timeoutTask = null;
_state = State.ShuttingDown;
}
private void HandleCompletedGCTask()
{
_gcTask = null;
for (var i = 0; i < 10; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();
}
private void MaybeCreateTimeoutTask()
{
// If there are no active clients running then the server needs to be in a timeout mode.
if (_connections.Count == 0 && _timeoutTask == null)
{
Debug.Assert(_listenTask != null);
_timeoutTask = Task.Delay(_keepAlive.TimeSpan);
}
}
private void MaybeCreateGCTask()
{
if (_connections.Count == 0 && _gcTask == null)
{
_gcTask = Task.Delay(GCTimeout);
}
}
/// <summary>
/// Checks the completed connection objects.
/// </summary>
/// <returns>False if the server needs to begin shutting down</returns>
private void HandleCompletedConnections()
{
var shutdown = false;
var processedCount = 0;
var i = 0;
while (i < _connections.Count)
{
var current = _connections[i];
if (!current.IsCompleted)
{
i++;
continue;
}
_connections.RemoveAt(i);
processedCount++;
var result = current.Result;
if (result.KeepAlive.HasValue)
{
var updated = _keepAlive.Update(result.KeepAlive.Value);
if (updated.Equals(_keepAlive))
{
_eventBus.UpdateKeepAlive(updated.TimeSpan);
}
}
switch (result.CloseReason)
{
case ConnectionResult.Reason.CompilationCompleted:
case ConnectionResult.Reason.CompilationNotStarted:
// These are all normal end states. Nothing to do here.
break;
case ConnectionResult.Reason.ClientDisconnect:
// Have to assume the worst here which is user pressing Ctrl+C at the command line and
// hence wanting all compilation to end.
_eventBus.ConnectionRudelyEnded();
shutdown = true;
break;
case ConnectionResult.Reason.ClientException:
case ConnectionResult.Reason.ClientShutdownRequest:
_eventBus.ConnectionRudelyEnded();
shutdown = true;
break;
default:
throw new InvalidOperationException($"Unexpected enum value {result.CloseReason}");
}
}
if (processedCount > 0)
{
_eventBus.ConnectionCompleted(processedCount);
}
if (shutdown)
{
_state = State.ShuttingDown;
}
}
internal async Task<ConnectionResult> AcceptConnection(Task<Connection> task, bool accept, CancellationToken cancellationToken)
{
Connection connection;
try
{
connection = await task;
}
catch (Exception ex)
{
// Unable to establish a connection with the client. The client is responsible for
// handling this case. Nothing else for us to do here.
ServerLogger.LogException(ex, "Error creating client named pipe");
return new ConnectionResult(ConnectionResult.Reason.CompilationNotStarted);
}
try
{
using (connection)
{
ServerRequest request;
try
{
ServerLogger.Log("Begin reading request.");
request = await ServerRequest.ReadAsync(connection.Stream, cancellationToken).ConfigureAwait(false);
ServerLogger.Log("End reading request.");
}
catch (Exception e)
{
ServerLogger.LogException(e, "Error reading build request.");
return new ConnectionResult(ConnectionResult.Reason.CompilationNotStarted);
}
if (request.IsShutdownRequest())
{
// Reply with the PID of this process so that the client can wait for it to exit.
var response = new ShutdownServerResponse(Environment.ProcessId);
await response.WriteAsync(connection.Stream, cancellationToken);
// We can safely disconnect the client, then when this connection gets cleaned up by the event loop
// the server will go to a shutdown state.
return new ConnectionResult(ConnectionResult.Reason.ClientShutdownRequest);
}
else if (!accept)
{
// We're already in shutdown mode, respond gracefully so the client can run in-process.
var response = new RejectedServerResponse();
await response.WriteAsync(connection.Stream, cancellationToken).ConfigureAwait(false);
return new ConnectionResult(ConnectionResult.Reason.CompilationNotStarted);
}
else
{
// If we get here then this is a real request that we will accept and process.
//
// Kick off both the compilation and a task to monitor the pipe for closing.
var buildCancelled = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var watcher = connection.WaitForDisconnectAsync(buildCancelled.Token);
var worker = ExecuteRequestAsync(request, buildCancelled.Token);
// await will end when either the work is complete or the connection is closed.
await Task.WhenAny(worker, watcher);
// Do an 'await' on the completed task, preference being compilation, to force
// any exceptions to be realized in this method for logging.
ConnectionResult.Reason reason;
if (worker.IsCompleted)
{
var response = await worker;
try
{
ServerLogger.Log("Begin writing response.");
await response.WriteAsync(connection.Stream, cancellationToken);
ServerLogger.Log("End writing response.");
reason = ConnectionResult.Reason.CompilationCompleted;
_eventBus.CompilationCompleted();
}
catch
{
reason = ConnectionResult.Reason.ClientDisconnect;
}
}
else
{
await watcher;
reason = ConnectionResult.Reason.ClientDisconnect;
}
// Begin the tear down of the Task which didn't complete.
buildCancelled.Cancel();
return new ConnectionResult(reason, request.KeepAlive);
}
}
}
catch (Exception ex)
{
ServerLogger.LogException(ex, "Error handling connection");
return new ConnectionResult(ConnectionResult.Reason.ClientException);
}
}
private Task<ServerResponse> ExecuteRequestAsync(ServerRequest buildRequest, CancellationToken cancellationToken)
{
Func<ServerResponse> func = () =>
{
ServerLogger.Log("Begin processing request");
var response = _compilerHost.Execute(buildRequest, cancellationToken);
ServerLogger.Log("End processing request");
return response;
};
var task = new Task<ServerResponse>(func, cancellationToken, TaskCreationOptions.LongRunning);
task.Start();
return task;
}
private enum State
{
/// <summary>
/// Server running and accepting all requests
/// </summary>
Running,
/// <summary>
/// Server processing existing requests, responding to shutdown commands but is not accepting
/// new build requests.
/// </summary>
ShuttingDown,
/// <summary>
/// Server is done.
/// </summary>
Completed,
}
private struct KeepAlive
{
public TimeSpan TimeSpan;
public bool IsDefault;
public KeepAlive(TimeSpan timeSpan, bool isDefault)
{
TimeSpan = timeSpan;
IsDefault = isDefault;
}
public KeepAlive Update(TimeSpan timeSpan)
{
if (IsDefault || timeSpan > TimeSpan)
{
return new KeepAlive(timeSpan, isDefault: false);
}
return this;
}
}
}
}