| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using Azure.Core; |
| | 7 | | using Azure.Messaging.EventHubs.Core; |
| | 8 | | using Azure.Messaging.EventHubs.Processor; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.EventHubs |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// The set of options that can be specified when creating an <see cref="EventProcessorClient" /> |
| | 14 | | /// to configure its behavior. |
| | 15 | | /// </summary> |
| | 16 | | /// |
| | 17 | | public class EventProcessorClientOptions |
| | 18 | | { |
| | 19 | | /// <summary>The maximum amount of time to wait for an event to become available before emitting an <c>null</c> |
| | 20 | | private TimeSpan? _maximumWaitTime = null; |
| | 21 | |
|
| | 22 | | /// <summary>The event catch count to use when reading events.</summary> |
| 48 | 23 | | private int _cacheEventCount = 100; |
| | 24 | |
|
| | 25 | | /// <summary>The prefetch count to use when reading events.</summary> |
| 48 | 26 | | private int _prefetchCount = 300; |
| | 27 | |
|
| | 28 | | /// <summary>The set of options to use for configuring the connection to the Event Hubs service.</summary> |
| 48 | 29 | | private EventHubConnectionOptions _connectionOptions = new EventHubConnectionOptions(); |
| | 30 | |
|
| | 31 | | /// <summary>The set of options to govern retry behavior and try timeouts.</summary> |
| 48 | 32 | | private EventHubsRetryOptions _retryOptions = new EventHubsRetryOptions(); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// A unique name used to identify the event processor. If <c>null</c> or empty, a GUID will be used as the |
| | 36 | | /// identifier. |
| | 37 | | /// </summary> |
| | 38 | | /// |
| 64 | 39 | | public string Identifier { get; set; } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Indicates whether or not the consumer should request information on the last enqueued event on the partiti |
| | 43 | | /// associated with a given event, and track that information as events are received. |
| | 44 | | /// </summary> |
| | 45 | | /// |
| | 46 | | /// <value><c>true</c> if information about a partition's last event should be requested and tracked; otherwise, |
| | 47 | | /// |
| | 48 | | /// <remarks> |
| | 49 | | /// When information about a partition's last enqueued event is being tracked, each event received from the Ev |
| | 50 | | /// service will carry metadata about the partition that it otherwise would not. This results in a small amoun |
| | 51 | | /// additional network bandwidth consumption that is generally a favorable trade-off when considered |
| | 52 | | /// against periodically making requests for partition properties using one of the Event Hub clients. |
| | 53 | | /// </remarks> |
| | 54 | | /// |
| 112 | 55 | | public bool TrackLastEnqueuedEventProperties { get; set; } = true; |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// The strategy that an event processor will use to make decisions about |
| | 59 | | /// partition ownership when performing load balancing to share work with |
| | 60 | | /// other event processors. |
| | 61 | | /// </summary> |
| | 62 | | /// |
| | 63 | | /// <seealso cref="Processor.LoadBalancingStrategy" /> |
| | 64 | | /// |
| 62 | 65 | | public LoadBalancingStrategy LoadBalancingStrategy { get; set; } = LoadBalancingStrategy.Balanced; |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// The maximum amount of time to wait for an event to become available for a given partition before emitting |
| | 69 | | /// an empty event. |
| | 70 | | /// </summary> |
| | 71 | | /// |
| | 72 | | /// <value> |
| | 73 | | /// If <c>null</c>, the processor will wait indefinitely for an event to become available; otherwise, a value |
| | 74 | | /// always be emitted within this interval, whether an event was available or not. |
| | 75 | | /// </value> |
| | 76 | | /// |
| | 77 | | /// <remarks> |
| | 78 | | /// When set, if no events are received before the timeout, <see cref="EventProcessorClient.ProcessEventAsync" |
| | 79 | | /// is called with a <see cref="ProcessEventArgs" /> instance that does not contain any event data. The |
| | 80 | | /// <see cref="ProcessEventArgs.HasEvent" /> property is intended to help detect this. |
| | 81 | | /// </remarks> |
| | 82 | | /// |
| | 83 | | /// <seealso cref="ProcessEventArgs.HasEvent" /> |
| | 84 | | /// |
| | 85 | | public TimeSpan? MaximumWaitTime |
| | 86 | | { |
| 54 | 87 | | get => _maximumWaitTime; |
| | 88 | |
|
| | 89 | | set |
| | 90 | | { |
| 16 | 91 | | if (value.HasValue) |
| | 92 | | { |
| 14 | 93 | | Argument.AssertNotNegative(value.Value, nameof(MaximumWaitTime)); |
| | 94 | | } |
| | 95 | |
|
| 8 | 96 | | _maximumWaitTime = value; |
| 8 | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// The maximum number of events that will be read from the Event Hubs service and held in a local memory |
| | 102 | | /// cache when reading is active and events are being emitted to an enumerator for processing. |
| | 103 | | /// </summary> |
| | 104 | | /// |
| | 105 | | /// <value> |
| | 106 | | /// The <see cref="CacheEventCount" /> is a control that developers can use to help tune performance for the s |
| | 107 | | /// needs of an application, given its expected size of events, throughput needs, and expected scenarios for u |
| | 108 | | /// Event Hubs. |
| | 109 | | /// </value> |
| | 110 | | /// |
| | 111 | | /// <remarks> |
| | 112 | | /// The size of this cache has an influence on the efficiency of reading events from the Event Hubs service. |
| | 113 | | /// larger the size of the cache, the more efficiently service operations can be buffered in the background to |
| | 114 | | /// improve throughput. This comes at the cost of additional memory use and potentially increases network I/O |
| | 115 | | /// |
| | 116 | | /// For scenarios where the size of events is small and many events are flowing through the system, using a la |
| | 117 | | /// <see cref="CacheEventCount"/> and <see cref="PrefetchCount" /> may help improve throughput. For scenarios |
| | 118 | | /// the size of events is larger or when processing of events is expected to be a heavier and slower operation |
| | 119 | | /// a smaller size <see cref="CacheEventCount"/> and <see cref="PrefetchCount"/> may help manage resource use |
| | 120 | | /// incurring a non-trivial cost to throughput. |
| | 121 | | /// |
| | 122 | | /// Regardless of the values, it is generally recommended that the <see cref="PrefetchCount" /> be at least 2- |
| | 123 | | /// times as large as the <see cref="CacheEventCount" /> to allow for efficient buffering of service operation |
| | 124 | | /// </remarks> |
| | 125 | | /// |
| | 126 | | public int CacheEventCount |
| | 127 | | { |
| 48 | 128 | | get => _cacheEventCount; |
| | 129 | |
|
| | 130 | | set |
| | 131 | | { |
| 4 | 132 | | Argument.AssertAtLeast(value, 1, nameof(CacheEventCount)); |
| 2 | 133 | | _cacheEventCount = value; |
| 2 | 134 | | } |
| | 135 | | } |
| | 136 | |
|
| | 137 | | /// <summary> |
| | 138 | | /// The number of events that will be eagerly requested from the Event Hubs service and staged locally without |
| | 139 | | /// whether a reader is currently active, intended to help maximize throughput by buffering service operations |
| | 140 | | /// readers needing to wait for service operations to complete. |
| | 141 | | /// </summary> |
| | 142 | | /// |
| | 143 | | /// <value> |
| | 144 | | /// The <see cref="PrefetchCount" /> is a control that developers can use to help tune performance for the spe |
| | 145 | | /// needs of an application, given its expected size of events, throughput needs, and expected scenarios for u |
| | 146 | | /// Event Hubs. |
| | 147 | | /// </value> |
| | 148 | | /// |
| | 149 | | /// <remarks> |
| | 150 | | /// The size of the prefetch count has an influence on the efficiency of reading events from the Event Hubs se |
| | 151 | | /// larger the size of the cache, the more efficiently service operations can be buffered in the background to |
| | 152 | | /// improve throughput. This comes at the cost of additional memory use and potentially increases network I/O |
| | 153 | | /// |
| | 154 | | /// For scenarios where the size of events is small and many events are flowing through the system, using a la |
| | 155 | | /// <see cref="CacheEventCount"/> and <see cref="PrefetchCount" /> may help improve throughput. For scenarios |
| | 156 | | /// the size of events is larger or when processing of events is expected to be a heavier and slower operation |
| | 157 | | /// a smaller size <see cref="CacheEventCount"/> and <see cref="PrefetchCount"/> may help manage resource use |
| | 158 | | /// incurring a non-trivial cost to throughput. |
| | 159 | | /// |
| | 160 | | /// Regardless of the values, it is generally recommended that the <see cref="PrefetchCount" /> be at least 2- |
| | 161 | | /// times as large as the <see cref="CacheEventCount" /> to allow for efficient buffering of service operation |
| | 162 | | /// </remarks> |
| | 163 | | /// |
| | 164 | | public int PrefetchCount |
| | 165 | | { |
| 54 | 166 | | get => _prefetchCount; |
| | 167 | |
|
| | 168 | | set |
| | 169 | | { |
| 8 | 170 | | Argument.AssertAtLeast(value, 0, nameof(PrefetchCount)); |
| 6 | 171 | | _prefetchCount = value; |
| 6 | 172 | | } |
| | 173 | | } |
| | 174 | |
|
| | 175 | | /// <summary> |
| | 176 | | /// Gets or sets the options used for configuring the connection to the Event Hubs service. |
| | 177 | | /// </summary> |
| | 178 | | /// |
| | 179 | | public EventHubConnectionOptions ConnectionOptions |
| | 180 | | { |
| 62 | 181 | | get => _connectionOptions; |
| | 182 | | set |
| | 183 | | { |
| 8 | 184 | | Argument.AssertNotNull(value, nameof(ConnectionOptions)); |
| 6 | 185 | | _connectionOptions = value; |
| 6 | 186 | | } |
| | 187 | | } |
| | 188 | |
|
| | 189 | | /// <summary> |
| | 190 | | /// The set of options to use for determining whether a failed operation should be retried and, |
| | 191 | | /// if so, the amount of time to wait between retry attempts. These options also control the |
| | 192 | | /// amount of time allowed for publishing events and other interactions with the Event Hubs service. |
| | 193 | | /// </summary> |
| | 194 | | /// |
| | 195 | | public EventHubsRetryOptions RetryOptions |
| | 196 | | { |
| 62 | 197 | | get => _retryOptions; |
| | 198 | | set |
| | 199 | | { |
| 10 | 200 | | Argument.AssertNotNull(value, nameof(RetryOptions)); |
| 8 | 201 | | _retryOptions = value; |
| 8 | 202 | | } |
| | 203 | | } |
| | 204 | |
|
| | 205 | | /// <summary> |
| | 206 | | /// Determines whether the specified <see cref="System.Object" /> is equal to this instance. |
| | 207 | | /// </summary> |
| | 208 | | /// |
| | 209 | | /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> |
| | 210 | | /// |
| | 211 | | /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c> |
| | 212 | | /// |
| | 213 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 214 | | public override bool Equals(object obj) => base.Equals(obj); |
| | 215 | |
|
| | 216 | | /// <summary> |
| | 217 | | /// Returns a hash code for this instance. |
| | 218 | | /// </summary> |
| | 219 | | /// |
| | 220 | | /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha |
| | 221 | | /// |
| | 222 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 223 | | public override int GetHashCode() => base.GetHashCode(); |
| | 224 | |
|
| | 225 | | /// <summary> |
| | 226 | | /// Converts the instance to string representation. |
| | 227 | | /// </summary> |
| | 228 | | /// |
| | 229 | | /// <returns>A <see cref="System.String" /> that represents this instance.</returns> |
| | 230 | | /// |
| | 231 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 232 | | public override string ToString() => base.ToString(); |
| | 233 | |
|
| | 234 | | /// <summary> |
| | 235 | | /// Creates a new copy of the current <see cref="EventProcessorClientOptions" />, cloning its attributes into |
| | 236 | | /// </summary> |
| | 237 | | /// |
| | 238 | | /// <returns>A new copy of <see cref="EventProcessorClientOptions" />.</returns> |
| | 239 | | /// |
| | 240 | | internal EventProcessorClientOptions Clone() => |
| 2 | 241 | | new EventProcessorClientOptions |
| 2 | 242 | | { |
| 2 | 243 | | Identifier = Identifier, |
| 2 | 244 | | TrackLastEnqueuedEventProperties = TrackLastEnqueuedEventProperties, |
| 2 | 245 | | LoadBalancingStrategy = LoadBalancingStrategy, |
| 2 | 246 | | _maximumWaitTime = _maximumWaitTime, |
| 2 | 247 | | _cacheEventCount = _cacheEventCount, |
| 2 | 248 | | _prefetchCount = _prefetchCount, |
| 2 | 249 | | _connectionOptions = ConnectionOptions.Clone(), |
| 2 | 250 | | _retryOptions = RetryOptions.Clone() |
| 2 | 251 | | }; |
| | 252 | | } |
| | 253 | | } |