| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Extensions.DependencyInjection; |
| | 4 | | using IceRpc.Features; |
| | 5 | | using ZeroC.Slice; |
| | 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 | | private readonly bool _alwaysEnforceDeadline; |
| | 29 | | private readonly IInvoker _next; |
| | 30 | | private readonly TimeSpan _defaultTimeout; |
| | 31 | |
|
| | 32 | | /// <summary>Constructs a Deadline interceptor.</summary> |
| | 33 | | /// <param name="next">The next invoker in the invocation pipeline.</param> |
| | 34 | | /// <param name="defaultTimeout">The default timeout. When not infinite, the interceptor adds a deadline to requests |
| | 35 | | /// without a deadline.</param> |
| | 36 | | /// <param name="alwaysEnforceDeadline">When <see langword="true" /> and the request carries a deadline, the |
| | 37 | | /// interceptor always creates a cancellation token source to enforce this deadline. When <see langword="false" /> |
| | 38 | | /// and the request carries a deadline, the interceptor creates a cancellation token source to enforce this deadline |
| | 39 | | /// only when the invocation's cancellation token cannot be canceled. The default value is <see langword="false" />. |
| | 40 | | /// </param> |
| 5 | 41 | | public DeadlineInterceptor(IInvoker next, TimeSpan defaultTimeout, bool alwaysEnforceDeadline) |
| 5 | 42 | | { |
| 5 | 43 | | _next = next; |
| 5 | 44 | | _alwaysEnforceDeadline = alwaysEnforceDeadline; |
| 5 | 45 | | _defaultTimeout = defaultTimeout; |
| 5 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <inheritdoc/> |
| | 49 | | public Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken = default) |
| 5 | 50 | | { |
| 5 | 51 | | TimeSpan? timeout = null; |
| 5 | 52 | | DateTime deadline = DateTime.MaxValue; |
| | 53 | |
|
| 5 | 54 | | if (request.Features.Get<IDeadlineFeature>() is IDeadlineFeature deadlineFeature) |
| 3 | 55 | | { |
| 3 | 56 | | deadline = deadlineFeature.Value; |
| 3 | 57 | | if (deadline != DateTime.MaxValue && (_alwaysEnforceDeadline || !cancellationToken.CanBeCanceled)) |
| 2 | 58 | | { |
| 2 | 59 | | timeout = deadline - DateTime.UtcNow; |
| 2 | 60 | | } |
| 3 | 61 | | } |
| 2 | 62 | | else if (_defaultTimeout != Timeout.InfiniteTimeSpan) |
| 2 | 63 | | { |
| 2 | 64 | | timeout = _defaultTimeout; |
| 2 | 65 | | deadline = DateTime.UtcNow + timeout.Value; |
| 2 | 66 | | } |
| | 67 | |
|
| 5 | 68 | | if (timeout is not null && timeout.Value <= TimeSpan.Zero) |
| 0 | 69 | | { |
| 0 | 70 | | throw new TimeoutException("The request deadline has expired."); |
| | 71 | | } |
| | 72 | |
|
| 5 | 73 | | if (deadline != DateTime.MaxValue) |
| 5 | 74 | | { |
| 5 | 75 | | request.Fields = request.Fields.With( |
| 5 | 76 | | RequestFieldKey.Deadline, |
| 5 | 77 | | deadline, |
| 7 | 78 | | (ref SliceEncoder encoder, DateTime deadline) => encoder.EncodeTimeStamp(deadline)); |
| 5 | 79 | | } |
| | 80 | |
|
| 5 | 81 | | return timeout is null ? _next.InvokeAsync(request, cancellationToken) : PerformInvokeAsync(timeout.Value); |
| | 82 | |
|
| | 83 | | async Task<IncomingResponse> PerformInvokeAsync(TimeSpan timeout) |
| 4 | 84 | | { |
| 4 | 85 | | using var timeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| 4 | 86 | | timeoutTokenSource.CancelAfter(timeout); |
| | 87 | |
|
| | 88 | | try |
| 4 | 89 | | { |
| 4 | 90 | | return await _next.InvokeAsync(request, timeoutTokenSource.Token).ConfigureAwait(false); |
| | 91 | | } |
| 2 | 92 | | catch (OperationCanceledException exception) when (exception.CancellationToken == timeoutTokenSource.Token) |
| 2 | 93 | | { |
| 2 | 94 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 95 | | throw new TimeoutException("The request deadline has expired."); |
| | 96 | | } |
| 2 | 97 | | } |
| 5 | 98 | | } |
| | 99 | | } |