| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Primitives |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Diagnostics; |
| | 8 | |
|
| | 9 | | [DebuggerStepThrough] |
| | 10 | | struct TimeoutHelper |
| | 11 | | { |
| | 12 | | DateTime deadline; |
| | 13 | | bool deadlineSet; |
| | 14 | | TimeSpan originalTimeout; |
| | 15 | |
|
| | 16 | | public TimeoutHelper(TimeSpan timeout, bool startTimeout) |
| | 17 | | { |
| | 18 | | Debug.Assert(timeout >= TimeSpan.Zero, "timeout must be non-negative"); |
| | 19 | |
|
| 8 | 20 | | this.originalTimeout = timeout; |
| 8 | 21 | | this.deadline = DateTime.MaxValue; |
| 8 | 22 | | this.deadlineSet = (timeout == TimeSpan.MaxValue); |
| | 23 | |
|
| 8 | 24 | | if (startTimeout && !this.deadlineSet) |
| | 25 | | { |
| 8 | 26 | | this.SetDeadline(); |
| | 27 | | } |
| 8 | 28 | | } |
| | 29 | |
|
| | 30 | | public static DateTime Add(DateTime time, TimeSpan timeout) |
| | 31 | | { |
| 0 | 32 | | if (timeout >= TimeSpan.Zero && DateTime.MaxValue - time <= timeout) |
| | 33 | | { |
| 0 | 34 | | return DateTime.MaxValue; |
| | 35 | | } |
| 0 | 36 | | if (timeout <= TimeSpan.Zero && DateTime.MinValue - time >= timeout) |
| | 37 | | { |
| 0 | 38 | | return DateTime.MinValue; |
| | 39 | | } |
| 0 | 40 | | return time + timeout; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public static DateTime Subtract(DateTime time, TimeSpan timeout) |
| | 44 | | { |
| 0 | 45 | | return Add(time, TimeSpan.Zero - timeout); |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public static TimeSpan Divide(TimeSpan timeout, int factor) |
| | 49 | | { |
| 0 | 50 | | if (timeout == TimeSpan.MaxValue) |
| | 51 | | { |
| 0 | 52 | | return TimeSpan.MaxValue; |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | return Ticks.ToTimeSpan((Ticks.FromTimeSpan(timeout) / factor) + 1); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public static TimeSpan Min(TimeSpan time1, TimeSpan time2) |
| | 59 | | { |
| 0 | 60 | | if (time1 <= time2) |
| | 61 | | { |
| 0 | 62 | | return time1; |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | return time2; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public static DateTime Min(DateTime first, DateTime second) |
| | 69 | | { |
| 0 | 70 | | if (first <= second) |
| | 71 | | { |
| 0 | 72 | | return first; |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | return second; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | public static void ThrowIfNegativeArgument(TimeSpan timeout) |
| | 79 | | { |
| 0 | 80 | | ThrowIfNegativeArgument(timeout, nameof(timeout)); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | public static void ThrowIfNegativeArgument(TimeSpan timeout, string argumentName) |
| | 84 | | { |
| 84 | 85 | | if (timeout < TimeSpan.Zero) |
| | 86 | | { |
| 0 | 87 | | throw Fx.Exception.ArgumentOutOfRange(argumentName, timeout, Resources.TimeoutMustBeNonNegative.FormatFo |
| | 88 | | } |
| 84 | 89 | | } |
| | 90 | |
|
| | 91 | | public static void ThrowIfNonPositiveArgument(TimeSpan timeout) |
| | 92 | | { |
| 6 | 93 | | ThrowIfNonPositiveArgument(timeout, nameof(timeout)); |
| 6 | 94 | | } |
| | 95 | |
|
| | 96 | | public static void ThrowIfNonPositiveArgument(TimeSpan timeout, string argumentName) |
| | 97 | | { |
| 174 | 98 | | if (timeout <= TimeSpan.Zero) |
| | 99 | | { |
| 0 | 100 | | throw Fx.Exception.ArgumentOutOfRange(argumentName, timeout, Resources.TimeoutMustBePositive.FormatForUs |
| | 101 | | } |
| 174 | 102 | | } |
| | 103 | |
|
| | 104 | | public TimeSpan RemainingTime() |
| | 105 | | { |
| 12 | 106 | | if (!this.deadlineSet) |
| | 107 | | { |
| 0 | 108 | | this.SetDeadline(); |
| 0 | 109 | | return this.originalTimeout; |
| | 110 | | } |
| | 111 | |
|
| 12 | 112 | | if (this.deadline == DateTime.MaxValue) |
| | 113 | | { |
| 0 | 114 | | return TimeSpan.MaxValue; |
| | 115 | | } |
| | 116 | |
|
| 12 | 117 | | var remaining = this.deadline - DateTime.UtcNow; |
| 12 | 118 | | if (remaining <= TimeSpan.Zero) |
| | 119 | | { |
| 0 | 120 | | return TimeSpan.Zero; |
| | 121 | | } |
| | 122 | |
|
| 12 | 123 | | return remaining; |
| | 124 | | } |
| | 125 | |
|
| | 126 | | public TimeSpan ElapsedTime() |
| | 127 | | { |
| 0 | 128 | | return this.originalTimeout - this.RemainingTime(); |
| | 129 | | } |
| | 130 | |
|
| | 131 | | void SetDeadline() |
| | 132 | | { |
| | 133 | | Debug.Assert(!this.deadlineSet, "TimeoutHelper deadline set twice."); |
| 8 | 134 | | this.deadline = DateTime.UtcNow + this.originalTimeout; |
| 8 | 135 | | this.deadlineSet = true; |
| 8 | 136 | | } |
| | 137 | | } |
| | 138 | | } |