| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | namespace IceRpc.Features; |
| | | 4 | | |
| | | 5 | | /// <summary>Default implementation of <see cref="IDeadlineFeature" />.</summary> |
| | | 6 | | public sealed class DeadlineFeature : IDeadlineFeature |
| | | 7 | | { |
| | | 8 | | // The maximum supported timeout (int.MaxValue ms, ~24.8 days). This is the maximum delay |
| | | 9 | | // CancellationTokenSource.CancelAfter accepts. |
| | 1 | 10 | | private static readonly TimeSpan MaxSupportedTimeout = TimeSpan.FromMilliseconds(int.MaxValue); |
| | | 11 | | |
| | | 12 | | /// <summary>Creates a deadline from a timeout.</summary> |
| | | 13 | | /// <param name="timeout">The timeout. Must be a positive value not exceeding ~24.8 days.</param> |
| | | 14 | | /// <returns>A new deadline equal to now plus the timeout.</returns> |
| | | 15 | | /// <exception cref="ArgumentException">Thrown if <paramref name="timeout" /> is not a positive value |
| | | 16 | | /// within the supported range.</exception> |
| | | 17 | | public static DeadlineFeature FromTimeout(TimeSpan timeout) |
| | 3 | 18 | | { |
| | 3 | 19 | | if (timeout <= TimeSpan.Zero || timeout > MaxSupportedTimeout) |
| | 3 | 20 | | { |
| | 3 | 21 | | throw new ArgumentException( |
| | 3 | 22 | | $"The {nameof(timeout)} value must be positive and not exceed {MaxSupportedTimeout}.", |
| | 3 | 23 | | nameof(timeout)); |
| | | 24 | | } |
| | 0 | 25 | | return new(DateTime.UtcNow + timeout); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc/> |
| | 15 | 29 | | public DateTime Value { get; } |
| | | 30 | | |
| | | 31 | | /// <summary>Constructs a deadline feature.</summary> |
| | | 32 | | /// <param name="deadline">The deadline value.</param> |
| | 18 | 33 | | public DeadlineFeature(DateTime deadline) => Value = deadline; |
| | | 34 | | } |