| | 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 |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Primitives; |
| | 10 | |
|
| | 11 | | /// <summary>Provides options associated with session pump processing using |
| | 12 | | /// <see cref="QueueClient.RegisterSessionHandler(Func{IMessageSession, Message, CancellationToken, Task}, SessionHa |
| | 13 | | /// <see cref="SubscriptionClient.RegisterSessionHandler(Func{IMessageSession, Message, CancellationToken, Task}, Se |
| | 14 | | public sealed class SessionHandlerOptions |
| | 15 | | { |
| | 16 | | int maxConcurrentSessions; |
| | 17 | | TimeSpan messageWaitTimeout; |
| | 18 | | TimeSpan maxAutoRenewDuration; |
| | 19 | |
|
| | 20 | | /// <summary>Initializes a new instance of the <see cref="SessionHandlerOptions" /> class. |
| | 21 | | /// Default Values: |
| | 22 | | /// <see cref="MaxConcurrentSessions"/> = 2000 |
| | 23 | | /// <see cref="AutoComplete"/> = true |
| | 24 | | /// <see cref="MessageWaitTimeout"/> = 1 minute |
| | 25 | | /// <see cref="MaxAutoRenewDuration"/> = 5 minutes |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="exceptionReceivedHandler">A <see cref="Func{T1, TResult}"/> that is invoked during exceptions. |
| | 28 | | /// <see cref="ExceptionReceivedEventArgs"/> contains contextual information regarding the exception.</param> |
| 0 | 29 | | public SessionHandlerOptions(Func<ExceptionReceivedEventArgs, Task> exceptionReceivedHandler) |
| | 30 | | { |
| | 31 | | // These are default values |
| 0 | 32 | | this.AutoComplete = true; |
| 0 | 33 | | this.MaxConcurrentSessions = 2000; |
| 0 | 34 | | this.MessageWaitTimeout = TimeSpan.FromMinutes(1); |
| 0 | 35 | | this.MaxAutoRenewDuration = Constants.ClientPumpRenewLockTimeout; |
| 0 | 36 | | this.ExceptionReceivedHandler = exceptionReceivedHandler ?? throw new ArgumentNullException(nameof(exception |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary>Occurs when an exception is received. Enables you to be notified of any errors encountered by the s |
| | 40 | | /// When errors are received calls will automatically be retried, so this is informational. </summary> |
| 0 | 41 | | public Func<ExceptionReceivedEventArgs, Task> ExceptionReceivedHandler { get; } |
| | 42 | |
|
| | 43 | | /// <summary>Gets or sets the duration for which the session lock will be renewed automatically. If a session lo |
| | 44 | | /// this value is the max duration for the session lock to be automatically renewed. </summary> |
| | 45 | | /// <remarks>If this auto renewal fails, clients will receive an exception in the ExceptionReceivedHandler. </re |
| | 46 | | /// <value>The duration for which the session renew its state. Default is 5 min.</value> |
| | 47 | | public TimeSpan MaxAutoRenewDuration |
| | 48 | | { |
| 0 | 49 | | get => this.maxAutoRenewDuration; |
| | 50 | |
|
| | 51 | | set |
| | 52 | | { |
| 0 | 53 | | TimeoutHelper.ThrowIfNegativeArgument(value, nameof(value)); |
| 0 | 54 | | this.maxAutoRenewDuration = value; |
| 0 | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary>Gets or sets the timeout to wait for receiving a message. This is the time the session-pump waits b |
| | 59 | | /// session and switching to a different session. </summary> |
| | 60 | | /// <remarks>This value has an impact on the message throughput. If the value is very large, then every time the |
| | 61 | | /// before closing to make sure that all the messages have been received. If users are having a lot of sessions |
| | 62 | | /// try setting this to be a relative smaller value based on how frequent new messages arrive in the session. </ |
| | 63 | | /// <value>The time to wait for receiving the message. Default is 1 min.</value> |
| | 64 | | public TimeSpan MessageWaitTimeout |
| | 65 | | { |
| 0 | 66 | | get => this.messageWaitTimeout; |
| | 67 | |
|
| | 68 | | set |
| | 69 | | { |
| 0 | 70 | | TimeoutHelper.ThrowIfNegativeArgument(value, nameof(value)); |
| 0 | 71 | | this.messageWaitTimeout = value; |
| 0 | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary>Gets or sets the maximum number of existing sessions that the User wants to handle concurrently. Se |
| | 76 | | /// greater than the max number of active sessions in the service will not increase message throughput. </summar |
| | 77 | | /// <remarks>The session-pump (SDK) will accept MaxConcurrentSessions number of sessions in parallel and dispatc |
| | 78 | | /// within a session are delivered sequentially. If more than <see cref="MaxConcurrentSessions"/> number of sess |
| | 79 | | /// entity, they will be accepted one-by-one after closing the existing sessions.</remarks> |
| | 80 | | /// <value>The maximum number of sessions that the User wants to handle concurrently. Default is 2000.</value> |
| | 81 | | public int MaxConcurrentSessions |
| | 82 | | { |
| 0 | 83 | | get => this.maxConcurrentSessions; |
| | 84 | |
|
| | 85 | | set |
| | 86 | | { |
| 0 | 87 | | if (value <= 0) |
| | 88 | | { |
| 0 | 89 | | throw new ArgumentOutOfRangeException(Resources.MaxConcurrentCallsMustBeGreaterThanZero.FormatForUse |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | this.maxConcurrentSessions = value; |
| 0 | 93 | | this.MaxConcurrentAcceptSessionCalls = Math.Min(value, 2 * Environment.ProcessorCount); |
| 0 | 94 | | } |
| | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary>Gets or sets whether the autocomplete option for messages in the session handler is enabled. |
| | 98 | | /// If this value is true, if the handler returns without any failure, then the message is completed and will no |
| | 99 | | /// exception is thrown from the handler, the message is abandoned and the DeliveryCount of this message will in |
| | 100 | | /// If this value is false, if the handler returns without any failure, then user has to write the logic to expl |
| | 101 | | /// otherwise the message is not considered 'completed' and will reappear in the session. </summary> |
| | 102 | | /// <value>true if the autocomplete option of the session handler is enabled; otherwise, false. Default is ture. |
| 0 | 103 | | public bool AutoComplete { get; set; } |
| | 104 | |
|
| 0 | 105 | | internal bool AutoRenewLock => this.MaxAutoRenewDuration > TimeSpan.Zero; |
| | 106 | |
|
| 0 | 107 | | internal int MaxConcurrentAcceptSessionCalls { get; set; } |
| | 108 | |
|
| | 109 | | internal async Task RaiseExceptionReceived(ExceptionReceivedEventArgs eventArgs) |
| | 110 | | { |
| | 111 | | try |
| | 112 | | { |
| 0 | 113 | | await this.ExceptionReceivedHandler(eventArgs).ConfigureAwait(false); |
| 0 | 114 | | } |
| 0 | 115 | | catch (Exception exception) |
| | 116 | | { |
| 0 | 117 | | MessagingEventSource.Log.ExceptionReceivedHandlerThrewException(exception); |
| 0 | 118 | | } |
| 0 | 119 | | } |
| | 120 | | } |
| | 121 | | } |