Class DeadlineInterceptor
Represents an interceptor that sets deadlines on requests without deadlines, and enforces these deadlines.
public class DeadlineInterceptor : IInvoker
- Inheritance
-
DeadlineInterceptor
- Implements
- Inherited Members
- Extension Methods
Remarks
When a request doesn't carry an IDeadlineFeature feature, this interceptor computes a
deadline using its configured default timeout; otherwise, it uses the request's existing deadline feature. It then
encodes the deadline value as a Deadline field and makes the invocation throw a
TimeoutException upon expiration of this deadline.
The dispatch of a one-way request cannot be canceled since the invocation typically completes before this dispatch
starts; as a result, for a one-way request, the deadline must be enforced by a DeadlineMiddleware.
If the server installs a DeadlineMiddleware, this deadline middleware decodes the deadline and
enforces it. In the unlikely event the middleware detects the expiration of the deadline before this interceptor,
the invocation will return an OutgoingResponse carrying status code
DeadlineExceeded.
The deadline interceptor must be installed before any interceptor than can run multiple times per request. In
particular, it must be installed before the retry interceptor.
Constructors
DeadlineInterceptor(IInvoker, TimeSpan, bool, TimeProvider?)
Constructs a Deadline interceptor.
public DeadlineInterceptor(IInvoker next, TimeSpan defaultTimeout, bool alwaysEnforceDeadline, TimeProvider? timeProvider = null)
Parameters
nextIInvokerThe next invoker in the invocation pipeline.
defaultTimeoutTimeSpanThe default timeout applied to requests without a deadline. Must be a positive value not exceeding ~24.8 days, or InfiniteTimeSpan to disable the default timeout entirely.
alwaysEnforceDeadlineboolWhen true and the request carries a deadline, the interceptor always creates a cancellation token source to enforce this deadline. When false and the request carries a deadline, the interceptor creates a cancellation token source to enforce this deadline only when the invocation's cancellation token cannot be canceled. The default value is false.
timeProviderTimeProviderThe optional time provider used to obtain the current time. If null, it uses System.
Exceptions
- ArgumentException
Thrown if
defaultTimeoutis neither InfiniteTimeSpan nor a positive value within the supported range.
Methods
InvokeAsync(OutgoingRequest, CancellationToken)
Sends an outgoing request and returns the corresponding incoming response.
public Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken = default)
Parameters
requestOutgoingRequestThe outgoing request being sent.
cancellationTokenCancellationTokenA cancellation token that receives the cancellation requests.
Returns
- Task<IncomingResponse>
The corresponding IncomingResponse.
Remarks
When request is a two-way request, the returned task will not complete successfully
until after the request's Payload is fully sent and the response is received from
the peer. When the request is a one-way request, the returned task completes successfully with an empty response
when the request's Payload is fully sent. For all requests (one-way and two-way),
the sending of the request's PayloadContinuation can continue in a background task
after the returned task has completed successfully.