-
Notifications
You must be signed in to change notification settings - Fork 12
/
DeadlineMiddleware.cs
67 lines (55 loc) · 2.56 KB
/
DeadlineMiddleware.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
// Copyright (c) ZeroC, Inc.
using IceRpc.Extensions.DependencyInjection;
using IceRpc.Features;
using ZeroC.Slice;
namespace IceRpc.Deadline;
/// <summary>Represents a middleware that decodes deadline fields into deadline features. When the decoded deadline
/// expires, this middleware cancels the dispatch and returns an <see cref="OutgoingResponse" /> with status code
/// <see cref="StatusCode.DeadlineExceeded" />.</summary>
/// <seealso cref="DeadlineRouterExtensions"/>
/// <seealso cref="DeadlineDispatcherBuilderExtensions"/>
public class DeadlineMiddleware : IDispatcher
{
private readonly IDispatcher _next;
/// <summary>Constructs a deadline middleware.</summary>
/// <param name="next">The next dispatcher in the dispatch pipeline.</param>
public DeadlineMiddleware(IDispatcher next) => _next = next;
/// <inheritdoc/>
public ValueTask<OutgoingResponse> DispatchAsync(
IncomingRequest request,
CancellationToken cancellationToken = default)
{
TimeSpan? timeout = null;
// not found returns default == DateTime.MinValue.
DateTime deadline = request.Fields.DecodeValue(
RequestFieldKey.Deadline,
(ref SliceDecoder decoder) => decoder.DecodeTimeStamp());
if (deadline != DateTime.MinValue)
{
timeout = deadline - DateTime.UtcNow;
if (timeout <= TimeSpan.Zero)
{
return new(new OutgoingResponse(
request,
StatusCode.DeadlineExceeded,
"The request deadline has expired."));
}
request.Features = request.Features.With<IDeadlineFeature>(new DeadlineFeature(deadline));
}
return timeout is null ? _next.DispatchAsync(request, cancellationToken) : PerformDispatchAsync(timeout.Value);
async ValueTask<OutgoingResponse> PerformDispatchAsync(TimeSpan timeout)
{
using var timeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeoutTokenSource.CancelAfter(timeout);
try
{
return await _next.DispatchAsync(request, timeoutTokenSource.Token).ConfigureAwait(false);
}
catch (OperationCanceledException exception) when (exception.CancellationToken == timeoutTokenSource.Token)
{
cancellationToken.ThrowIfCancellationRequested();
return new OutgoingResponse(request, StatusCode.DeadlineExceeded, "The request deadline has expired.");
}
}
}
}