| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Extensions.DependencyInjection; |
| | | 4 | | using IceRpc.Features; |
| | | 5 | | using ZeroC.Slice.Codec; |
| | | 6 | | |
| | | 7 | | namespace IceRpc.Deadline; |
| | | 8 | | |
| | | 9 | | /// <summary>Represents an interceptor that sets deadlines on requests without deadlines, and enforces these deadlines. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks>When a request doesn't carry an <see cref="IDeadlineFeature"/> feature, this interceptor computes a |
| | | 12 | | /// deadline using its configured default timeout; otherwise, it uses the request's existing deadline feature. It then |
| | | 13 | | /// encodes the deadline value as a <see cref="RequestFieldKey.Deadline" /> field and makes the invocation throw a |
| | | 14 | | /// <see cref="TimeoutException" /> upon expiration of this deadline.<br/> |
| | | 15 | | /// The dispatch of a one-way request cannot be canceled since the invocation typically completes before this dispatch |
| | | 16 | | /// starts; as a result, for a one-way request, the deadline must be enforced by a <see cref="DeadlineMiddleware"/>. |
| | | 17 | | /// <br/> |
| | | 18 | | /// If the server installs a <see cref="DeadlineMiddleware"/>, this deadline middleware decodes the deadline and |
| | | 19 | | /// enforces it. In the unlikely event the middleware detects the expiration of the deadline before this interceptor, |
| | | 20 | | /// the invocation will return an <see cref="OutgoingResponse"/> carrying status code |
| | | 21 | | /// <see cref="StatusCode.DeadlineExceeded"/>.<br/> |
| | | 22 | | /// The deadline interceptor must be installed before any interceptor than can run multiple times per request. In |
| | | 23 | | /// particular, it must be installed before the retry interceptor.</remarks> |
| | | 24 | | /// <seealso cref="DeadlinePipelineExtensions"/> |
| | | 25 | | /// <seealso cref="DeadlineInvokerBuilderExtensions"/> |
| | | 26 | | public class DeadlineInterceptor : IInvoker |
| | | 27 | | { |
| | | 28 | | // The maximum supported timeout (int.MaxValue ms, ~24.8 days). This is the maximum delay |
| | | 29 | | // CancellationTokenSource.CancelAfter accepts. |
| | 1 | 30 | | private static readonly TimeSpan _maxSupportedTimeout = TimeSpan.FromMilliseconds(int.MaxValue); |
| | | 31 | | |
| | | 32 | | private readonly bool _alwaysEnforceDeadline; |
| | | 33 | | private readonly IInvoker _next; |
| | | 34 | | private readonly TimeSpan _defaultTimeout; |
| | | 35 | | private readonly TimeProvider _timeProvider; |
| | | 36 | | |
| | | 37 | | /// <summary>Constructs a Deadline interceptor.</summary> |
| | | 38 | | /// <param name="next">The next invoker in the invocation pipeline.</param> |
| | | 39 | | /// <param name="defaultTimeout">The default timeout applied to requests without a deadline. Must be a positive |
| | | 40 | | /// value not exceeding ~24.8 days, or <see cref="Timeout.InfiniteTimeSpan" /> to disable the default timeout |
| | | 41 | | /// entirely.</param> |
| | | 42 | | /// <param name="timeProvider">The optional time provider used to obtain the current time. If |
| | | 43 | | /// <see langword="null"/>, it uses <see cref="TimeProvider.System"/>.</param> |
| | | 44 | | /// <param name="alwaysEnforceDeadline">When <see langword="true" /> and the request carries a deadline, the |
| | | 45 | | /// interceptor always creates a cancellation token source to enforce this deadline. When <see langword="false" /> |
| | | 46 | | /// and the request carries a deadline, the interceptor creates a cancellation token source to enforce this deadline |
| | | 47 | | /// only when the invocation's cancellation token cannot be canceled. The default value is <see langword="false" />. |
| | | 48 | | /// </param> |
| | | 49 | | /// <exception cref="ArgumentException">Thrown if <paramref name="defaultTimeout" /> is neither |
| | | 50 | | /// <see cref="Timeout.InfiniteTimeSpan" /> nor a positive value within the supported range.</exception> |
| | 13 | 51 | | public DeadlineInterceptor(IInvoker next, TimeSpan defaultTimeout, bool alwaysEnforceDeadline, TimeProvider? timePro |
| | 13 | 52 | | { |
| | 13 | 53 | | if (defaultTimeout != Timeout.InfiniteTimeSpan && |
| | 13 | 54 | | (defaultTimeout <= TimeSpan.Zero || defaultTimeout > _maxSupportedTimeout)) |
| | 3 | 55 | | { |
| | 3 | 56 | | throw new ArgumentException( |
| | 3 | 57 | | $"The {nameof(defaultTimeout)} value must be Timeout.InfiniteTimeSpan or a positive value not exceeding |
| | 3 | 58 | | nameof(defaultTimeout)); |
| | | 59 | | } |
| | | 60 | | |
| | 10 | 61 | | _next = next; |
| | 10 | 62 | | _alwaysEnforceDeadline = alwaysEnforceDeadline; |
| | 10 | 63 | | _defaultTimeout = defaultTimeout; |
| | 10 | 64 | | _timeProvider = timeProvider ?? TimeProvider.System; |
| | 10 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc/> |
| | | 68 | | public Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken = default) |
| | 9 | 69 | | { |
| | 9 | 70 | | TimeSpan? timeout = null; |
| | 9 | 71 | | DateTime deadline = DateTime.MaxValue; |
| | | 72 | | |
| | 9 | 73 | | DateTime now = _timeProvider.GetUtcNow().UtcDateTime; |
| | 9 | 74 | | if (request.Features.Get<IDeadlineFeature>() is IDeadlineFeature deadlineFeature) |
| | 7 | 75 | | { |
| | | 76 | | // Normalize to UTC. |
| | 7 | 77 | | deadline = deadlineFeature.Value == DateTime.MaxValue ? |
| | 7 | 78 | | DateTime.MaxValue : deadlineFeature.Value.ToUniversalTime(); |
| | 7 | 79 | | if (deadline != DateTime.MaxValue && (_alwaysEnforceDeadline || !cancellationToken.CanBeCanceled)) |
| | 6 | 80 | | { |
| | 6 | 81 | | timeout = deadline - now; |
| | 6 | 82 | | } |
| | 7 | 83 | | } |
| | 2 | 84 | | else if (_defaultTimeout != Timeout.InfiniteTimeSpan) |
| | 2 | 85 | | { |
| | 2 | 86 | | timeout = _defaultTimeout; |
| | 2 | 87 | | deadline = now + timeout.Value; |
| | 2 | 88 | | } |
| | | 89 | | |
| | 9 | 90 | | if (timeout is not null && timeout.Value <= TimeSpan.Zero) |
| | 0 | 91 | | { |
| | 0 | 92 | | throw new TimeoutException("The request deadline has expired."); |
| | | 93 | | } |
| | | 94 | | |
| | 9 | 95 | | if (deadline != DateTime.MaxValue) |
| | 9 | 96 | | { |
| | 9 | 97 | | request.Fields = request.Fields.With( |
| | 9 | 98 | | RequestFieldKey.Deadline, |
| | 9 | 99 | | deadline, |
| | 14 | 100 | | (ref SliceEncoder encoder, DateTime deadline) => encoder.EncodeTimeStamp(deadline)); |
| | 9 | 101 | | } |
| | | 102 | | |
| | 9 | 103 | | return timeout is null ? _next.InvokeAsync(request, cancellationToken) : PerformInvokeAsync(timeout.Value); |
| | | 104 | | |
| | | 105 | | async Task<IncomingResponse> PerformInvokeAsync(TimeSpan timeout) |
| | 8 | 106 | | { |
| | | 107 | | // Reject a caller-provided IDeadlineFeature whose remaining timeout exceeds what this interceptor |
| | | 108 | | // can enforce. Such deadlines are nonsensical in practice (~24.8 days), and silently clamping the |
| | | 109 | | // value the caller asked for is worse than failing cleanly. |
| | 8 | 110 | | if (timeout > _maxSupportedTimeout) |
| | 1 | 111 | | { |
| | 1 | 112 | | throw new NotSupportedException( |
| | 1 | 113 | | $"The request deadline exceeds the maximum timeout supported by this interceptor: {_maxSupportedTime |
| | | 114 | | } |
| | | 115 | | |
| | 7 | 116 | | using var timeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 7 | 117 | | timeoutTokenSource.CancelAfter(timeout); |
| | | 118 | | |
| | | 119 | | try |
| | 7 | 120 | | { |
| | 7 | 121 | | return await _next.InvokeAsync(request, timeoutTokenSource.Token).ConfigureAwait(false); |
| | | 122 | | } |
| | 2 | 123 | | catch (OperationCanceledException exception) when (exception.CancellationToken == timeoutTokenSource.Token) |
| | 2 | 124 | | { |
| | 2 | 125 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 126 | | throw new TimeoutException("The request deadline has expired."); |
| | | 127 | | } |
| | 5 | 128 | | } |
| | 9 | 129 | | } |
| | | 130 | | } |