| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Runtime.ExceptionServices; |
| | 7 | | using System.Security.Authentication; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Azure.Core; |
| | 11 | | using Azure.Core.Diagnostics; |
| | 12 | | using Azure.Messaging.EventHubs.Authorization; |
| | 13 | | using Azure.Messaging.EventHubs.Consumer; |
| | 14 | | using Azure.Messaging.EventHubs.Core; |
| | 15 | | using Azure.Messaging.EventHubs.Diagnostics; |
| | 16 | | using Microsoft.Azure.Amqp; |
| | 17 | |
|
| | 18 | | namespace Azure.Messaging.EventHubs.Amqp |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// A transport client abstraction responsible for brokering operations for AMQP-based connections. |
| | 22 | | /// It is intended that the public <see cref="EventHubConnection" /> make use of an instance via containment |
| | 23 | | /// and delegate operations to it. |
| | 24 | | /// </summary> |
| | 25 | | /// |
| | 26 | | /// <seealso cref="Azure.Messaging.EventHubs.Core.TransportClient" /> |
| | 27 | | /// |
| | 28 | | internal class AmqpClient : TransportClient |
| | 29 | | { |
| | 30 | | /// <summary> |
| | 31 | | /// The buffer to apply when considering refreshing; credentials that expire less than this duration will be r |
| | 32 | | /// </summary> |
| | 33 | | /// |
| 38 | 34 | | private static TimeSpan CredentialRefreshBuffer { get; } = TimeSpan.FromMinutes(5); |
| | 35 | |
|
| | 36 | | /// <summary>Indicates whether or not this instance has been closed.</summary> |
| | 37 | | private volatile bool _closed = false; |
| | 38 | |
|
| | 39 | | /// <summary>The currently active token to use for authorization with the Event Hubs service.</summary> |
| | 40 | | private AccessToken _accessToken; |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Indicates whether or not this client has been closed. |
| | 44 | | /// </summary> |
| | 45 | | /// |
| | 46 | | /// <value> |
| | 47 | | /// <c>true</c> if the client is closed; otherwise, <c>false</c>. |
| | 48 | | /// </value> |
| | 49 | | /// |
| 16 | 50 | | public override bool IsClosed => _closed; |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// The endpoint for the Event Hubs service to which the client is associated. |
| | 54 | | /// </summary> |
| | 55 | | /// |
| 174 | 56 | | public override Uri ServiceEndpoint { get; } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// The name of the Event Hub to which the client is bound. |
| | 60 | | /// </summary> |
| | 61 | | /// |
| 468 | 62 | | private string EventHubName { get; } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Gets the credential to use for authorization with the Event Hubs service. |
| | 66 | | /// </summary> |
| | 67 | | /// |
| 36 | 68 | | private EventHubTokenCredential Credential { get; } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// The converter to use for translating between AMQP messages and client library |
| | 72 | | /// types. |
| | 73 | | /// </summary> |
| | 74 | | /// |
| 146 | 75 | | private AmqpMessageConverter MessageConverter { get; } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// The AMQP connection scope responsible for managing transport constructs for this instance. |
| | 79 | | /// </summary> |
| | 80 | | /// |
| 274 | 81 | | private AmqpConnectionScope ConnectionScope { get; } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// The AMQP link intended for use with management operations. |
| | 85 | | /// </summary> |
| | 86 | | /// |
| 124 | 87 | | private FaultTolerantAmqpObject<RequestResponseAmqpLink> ManagementLink { get; } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Initializes a new instance of the <see cref="AmqpClient"/> class. |
| | 91 | | /// </summary> |
| | 92 | | /// |
| | 93 | | /// <param name="host">The fully qualified host name for the Event Hubs namespace. This is likely to be similar |
| | 94 | | /// <param name="eventHubName">The name of the specific Event Hub to connect the client to.</param> |
| | 95 | | /// <param name="credential">The Azure managed identity credential to use for authorization. Access controls ma |
| | 96 | | /// <param name="clientOptions">A set of options to apply when configuring the client.</param> |
| | 97 | | /// |
| | 98 | | /// <remarks> |
| | 99 | | /// As an internal type, this class performs only basic sanity checks against its arguments. It |
| | 100 | | /// is assumed that callers are trusted and have performed deep validation. |
| | 101 | | /// |
| | 102 | | /// Any parameters passed are assumed to be owned by this instance and safe to mutate or dispose; |
| | 103 | | /// creation of clones or otherwise protecting the parameters is assumed to be the purview of the |
| | 104 | | /// caller. |
| | 105 | | /// </remarks> |
| | 106 | | /// |
| | 107 | | public AmqpClient(string host, |
| | 108 | | string eventHubName, |
| | 109 | | EventHubTokenCredential credential, |
| 174 | 110 | | EventHubConnectionOptions clientOptions) : this(host, eventHubName, credential, clientOptions, |
| | 111 | | { |
| 170 | 112 | | } |
| | 113 | |
|
| | 114 | | /// <summary> |
| | 115 | | /// Initializes a new instance of the <see cref="AmqpClient"/> class. |
| | 116 | | /// </summary> |
| | 117 | | /// |
| | 118 | | /// <param name="host">The fully qualified host name for the Event Hubs namespace. This is likely to be similar |
| | 119 | | /// <param name="eventHubName">The name of the specific Event Hub to connect the client to.</param> |
| | 120 | | /// <param name="credential">The Azure managed identity credential to use for authorization. Access controls ma |
| | 121 | | /// <param name="clientOptions">A set of options to apply when configuring the client.</param> |
| | 122 | | /// <param name="connectionScope">The optional scope to use for AMQP connection management. If <c>null</c>, a n |
| | 123 | | /// <param name="messageConverter">The optional converter to use for transforming AMQP message-related types. I |
| | 124 | | /// |
| | 125 | | /// <remarks> |
| | 126 | | /// As an internal type, this class performs only basic sanity checks against its arguments. It |
| | 127 | | /// is assumed that callers are trusted and have performed deep validation. |
| | 128 | | /// |
| | 129 | | /// Any parameters passed are assumed to be owned by this instance and safe to mutate or dispose; |
| | 130 | | /// creation of clones or otherwise protecting the parameters is assumed to be the purview of the |
| | 131 | | /// caller. |
| | 132 | | /// </remarks> |
| | 133 | | /// |
| 210 | 134 | | protected AmqpClient(string host, |
| 210 | 135 | | string eventHubName, |
| 210 | 136 | | EventHubTokenCredential credential, |
| 210 | 137 | | EventHubConnectionOptions clientOptions, |
| 210 | 138 | | AmqpConnectionScope connectionScope, |
| 210 | 139 | | AmqpMessageConverter messageConverter) |
| | 140 | | { |
| 210 | 141 | | Argument.AssertNotNullOrEmpty(host, nameof(host)); |
| 210 | 142 | | Argument.AssertNotNullOrEmpty(eventHubName, nameof(eventHubName)); |
| 210 | 143 | | Argument.AssertNotNull(credential, nameof(credential)); |
| 208 | 144 | | Argument.AssertNotNull(clientOptions, nameof(clientOptions)); |
| | 145 | |
|
| | 146 | | try |
| | 147 | | { |
| 206 | 148 | | EventHubsEventSource.Log.EventHubClientCreateStart(host, eventHubName); |
| | 149 | |
|
| 206 | 150 | | ServiceEndpoint = new UriBuilder |
| 206 | 151 | | { |
| 206 | 152 | | Scheme = clientOptions.TransportType.GetUriScheme(), |
| 206 | 153 | | Host = host |
| 206 | 154 | |
|
| 206 | 155 | | }.Uri; |
| | 156 | |
|
| 206 | 157 | | EventHubName = eventHubName; |
| 206 | 158 | | Credential = credential; |
| 206 | 159 | | MessageConverter = messageConverter ?? new AmqpMessageConverter(); |
| 206 | 160 | | ConnectionScope = connectionScope ?? new AmqpConnectionScope(ServiceEndpoint, eventHubName, credential, |
| | 161 | |
|
| 206 | 162 | | ManagementLink = new FaultTolerantAmqpObject<RequestResponseAmqpLink>( |
| 274 | 163 | | timeout => ConnectionScope.OpenManagementLinkAsync(timeout, CancellationToken.None), |
| 206 | 164 | | link => |
| 206 | 165 | | { |
| 0 | 166 | | link.Session?.SafeClose(); |
| 0 | 167 | | link.SafeClose(); |
| 0 | 168 | | }); |
| 206 | 169 | | } |
| | 170 | | finally |
| | 171 | | { |
| 206 | 172 | | EventHubsEventSource.Log.EventHubClientCreateComplete(host, eventHubName); |
| 206 | 173 | | } |
| 206 | 174 | | } |
| | 175 | |
|
| | 176 | | /// <summary> |
| | 177 | | /// Retrieves information about an Event Hub, including the number of partitions present |
| | 178 | | /// and their identifiers. |
| | 179 | | /// </summary> |
| | 180 | | /// |
| | 181 | | /// <param name="retryPolicy">The retry policy to use as the basis for retrieving the information.</param> |
| | 182 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 183 | | /// |
| | 184 | | /// <returns>The set of information for the Event Hub that this client is associated with.</returns> |
| | 185 | | /// |
| | 186 | | public override async Task<EventHubProperties> GetPropertiesAsync(EventHubsRetryPolicy retryPolicy, |
| | 187 | | CancellationToken cancellationToken) |
| | 188 | | { |
| 24 | 189 | | Argument.AssertNotClosed(_closed, nameof(AmqpClient)); |
| 22 | 190 | | Argument.AssertNotNull(retryPolicy, nameof(retryPolicy)); |
| | 191 | |
|
| 20 | 192 | | var failedAttemptCount = 0; |
| 20 | 193 | | var retryDelay = default(TimeSpan?); |
| | 194 | |
|
| 20 | 195 | | var stopWatch = ValueStopwatch.StartNew(); |
| | 196 | |
|
| | 197 | | try |
| | 198 | | { |
| 20 | 199 | | var tryTimeout = retryPolicy.CalculateTryTimeout(0); |
| | 200 | |
|
| 38 | 201 | | while (!cancellationToken.IsCancellationRequested) |
| | 202 | | { |
| | 203 | | try |
| | 204 | | { |
| 36 | 205 | | EventHubsEventSource.Log.GetPropertiesStart(EventHubName); |
| | 206 | |
|
| | 207 | | // Create the request message and the management link. |
| | 208 | |
|
| 36 | 209 | | var token = await AcquireAccessTokenAsync(cancellationToken).ConfigureAwait(false); |
| 36 | 210 | | using AmqpMessage request = MessageConverter.CreateEventHubPropertiesRequest(EventHubName, token |
| 36 | 211 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 212 | |
|
| 34 | 213 | | RequestResponseAmqpLink link = await ManagementLink.GetOrCreateAsync(UseMinimum(ConnectionScope. |
| 0 | 214 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 215 | |
|
| | 216 | | // Send the request and wait for the response. |
| | 217 | |
|
| 0 | 218 | | using AmqpMessage response = await link.RequestAsync(request, tryTimeout.CalculateRemaining(stop |
| 0 | 219 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 220 | |
|
| | 221 | | // Process the response. |
| | 222 | |
|
| 0 | 223 | | AmqpError.ThrowIfErrorResponse(response, EventHubName); |
| 0 | 224 | | return MessageConverter.CreateEventHubPropertiesFromResponse(response); |
| | 225 | | } |
| | 226 | | catch (Exception ex) |
| | 227 | | { |
| 36 | 228 | | Exception activeEx = ex.TranslateServiceException(EventHubName); |
| | 229 | |
|
| | 230 | | // Determine if there should be a retry for the next attempt; if so enforce the delay but do not |
| | 231 | | // Otherwise, mark the exception as active and break out of the loop. |
| | 232 | |
|
| 36 | 233 | | ++failedAttemptCount; |
| 36 | 234 | | retryDelay = retryPolicy.CalculateRetryDelay(activeEx, failedAttemptCount); |
| | 235 | |
|
| 36 | 236 | | if ((retryDelay.HasValue) && (!ConnectionScope.IsDisposed) && (!cancellationToken.IsCancellation |
| | 237 | | { |
| 18 | 238 | | EventHubsEventSource.Log.GetPropertiesError(EventHubName, activeEx.Message); |
| 18 | 239 | | await Task.Delay(retryDelay.Value, cancellationToken).ConfigureAwait(false); |
| | 240 | |
|
| 18 | 241 | | tryTimeout = retryPolicy.CalculateTryTimeout(failedAttemptCount); |
| 18 | 242 | | stopWatch = ValueStopwatch.StartNew(); |
| | 243 | | } |
| 18 | 244 | | else if (ex is AmqpException) |
| | 245 | | { |
| 0 | 246 | | ExceptionDispatchInfo.Capture(activeEx).Throw(); |
| | 247 | | } |
| | 248 | | else |
| | 249 | | { |
| 18 | 250 | | throw; |
| | 251 | | } |
| | 252 | | } |
| | 253 | | } |
| | 254 | |
|
| | 255 | | // If no value has been returned nor exception thrown by this point, |
| | 256 | | // then cancellation has been requested. |
| | 257 | |
|
| 2 | 258 | | throw new TaskCanceledException(); |
| | 259 | | } |
| 20 | 260 | | catch (Exception ex) |
| | 261 | | { |
| 20 | 262 | | EventHubsEventSource.Log.GetPropertiesError(EventHubName, ex.Message); |
| 20 | 263 | | throw; |
| | 264 | | } |
| | 265 | | finally |
| | 266 | | { |
| 20 | 267 | | EventHubsEventSource.Log.GetPropertiesComplete(EventHubName); |
| | 268 | | } |
| 0 | 269 | | } |
| | 270 | |
|
| | 271 | | /// <summary> |
| | 272 | | /// Retrieves information about a specific partition for an Event Hub, including elements that describe the av |
| | 273 | | /// events in the partition event stream. |
| | 274 | | /// </summary> |
| | 275 | | /// |
| | 276 | | /// <param name="partitionId">The unique identifier of a partition associated with the Event Hub.</param> |
| | 277 | | /// <param name="retryPolicy">The retry policy to use as the basis for retrieving the information.</param> |
| | 278 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 279 | | /// |
| | 280 | | /// <returns>The set of information for the requested partition under the Event Hub this client is associated wi |
| | 281 | | /// |
| | 282 | | public override async Task<PartitionProperties> GetPartitionPropertiesAsync(string partitionId, |
| | 283 | | EventHubsRetryPolicy retryPolicy, |
| | 284 | | CancellationToken cancellationToken) |
| | 285 | | { |
| 28 | 286 | | Argument.AssertNotClosed(_closed, nameof(AmqpClient)); |
| 26 | 287 | | Argument.AssertNotNullOrEmpty(partitionId, nameof(partitionId)); |
| 22 | 288 | | Argument.AssertNotNull(retryPolicy, nameof(retryPolicy)); |
| | 289 | |
|
| 20 | 290 | | var failedAttemptCount = 0; |
| 20 | 291 | | var retryDelay = default(TimeSpan?); |
| 20 | 292 | | var token = default(string); |
| | 293 | | var link = default(RequestResponseAmqpLink); |
| | 294 | |
|
| 20 | 295 | | var stopWatch = ValueStopwatch.StartNew(); |
| | 296 | |
|
| | 297 | | try |
| | 298 | | { |
| 20 | 299 | | var tryTimeout = retryPolicy.CalculateTryTimeout(0); |
| | 300 | |
|
| 38 | 301 | | while (!cancellationToken.IsCancellationRequested) |
| | 302 | | { |
| | 303 | | try |
| | 304 | | { |
| 36 | 305 | | EventHubsEventSource.Log.GetPartitionPropertiesStart(EventHubName, partitionId); |
| | 306 | |
|
| | 307 | | // Create the request message and the management link. |
| | 308 | |
|
| 36 | 309 | | token = await AcquireAccessTokenAsync(cancellationToken).ConfigureAwait(false); |
| 36 | 310 | | using AmqpMessage request = MessageConverter.CreatePartitionPropertiesRequest(EventHubName, part |
| 36 | 311 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 312 | |
|
| 34 | 313 | | link = await ManagementLink.GetOrCreateAsync(UseMinimum(ConnectionScope.SessionTimeout, tryTimeo |
| 0 | 314 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 315 | |
|
| | 316 | | // Send the request and wait for the response. |
| | 317 | |
|
| 0 | 318 | | using AmqpMessage response = await link.RequestAsync(request, tryTimeout.CalculateRemaining(stop |
| 0 | 319 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 320 | |
|
| | 321 | | // Process the response. |
| | 322 | |
|
| 0 | 323 | | AmqpError.ThrowIfErrorResponse(response, EventHubName); |
| 0 | 324 | | return MessageConverter.CreatePartitionPropertiesFromResponse(response); |
| | 325 | | } |
| | 326 | | catch (Exception ex) |
| | 327 | | { |
| 36 | 328 | | Exception activeEx = ex.TranslateServiceException(EventHubName); |
| | 329 | |
|
| | 330 | | // Determine if there should be a retry for the next attempt; if so enforce the delay but do not |
| | 331 | | // Otherwise, mark the exception as active and break out of the loop. |
| | 332 | |
|
| 36 | 333 | | ++failedAttemptCount; |
| 36 | 334 | | retryDelay = retryPolicy.CalculateRetryDelay(activeEx, failedAttemptCount); |
| | 335 | |
|
| 36 | 336 | | if ((retryDelay.HasValue) && (!ConnectionScope.IsDisposed) && (!cancellationToken.IsCancellation |
| | 337 | | { |
| 18 | 338 | | EventHubsEventSource.Log.GetPartitionPropertiesError(EventHubName, partitionId, activeEx.Mes |
| 18 | 339 | | await Task.Delay(retryDelay.Value, cancellationToken).ConfigureAwait(false); |
| | 340 | |
|
| 18 | 341 | | tryTimeout = retryPolicy.CalculateTryTimeout(failedAttemptCount); |
| 18 | 342 | | stopWatch = ValueStopwatch.StartNew(); |
| | 343 | | } |
| 18 | 344 | | else if (ex is AmqpException) |
| | 345 | | { |
| 0 | 346 | | ExceptionDispatchInfo.Capture(activeEx).Throw(); |
| | 347 | | } |
| | 348 | | else |
| | 349 | | { |
| 18 | 350 | | throw; |
| | 351 | | } |
| | 352 | | } |
| | 353 | | } |
| | 354 | |
|
| | 355 | | // If no value has been returned nor exception thrown by this point, |
| | 356 | | // then cancellation has been requested. |
| | 357 | |
|
| 2 | 358 | | throw new TaskCanceledException(); |
| | 359 | | } |
| 20 | 360 | | catch (Exception ex) |
| | 361 | | { |
| 20 | 362 | | EventHubsEventSource.Log.GetPartitionPropertiesError(EventHubName, partitionId, ex.Message); |
| 20 | 363 | | throw; |
| | 364 | | } |
| | 365 | | finally |
| | 366 | | { |
| 20 | 367 | | EventHubsEventSource.Log.GetPartitionPropertiesComplete(EventHubName, partitionId); |
| | 368 | | } |
| 0 | 369 | | } |
| | 370 | |
|
| | 371 | | /// <summary> |
| | 372 | | /// Creates a producer strongly aligned with the active protocol and transport, |
| | 373 | | /// responsible for publishing <see cref="EventData" /> to the Event Hub. |
| | 374 | | /// </summary> |
| | 375 | | /// |
| | 376 | | /// <param name="partitionId">The identifier of the partition to which the transport producer should be bound; i |
| | 377 | | /// <param name="retryPolicy">The policy which governs retry behavior and try timeouts.</param> |
| | 378 | | /// |
| | 379 | | /// <returns>A <see cref="TransportProducer"/> configured in the requested manner.</returns> |
| | 380 | | /// |
| | 381 | | public override TransportProducer CreateProducer(string partitionId, |
| | 382 | | EventHubsRetryPolicy retryPolicy) |
| | 383 | | { |
| 14 | 384 | | Argument.AssertNotClosed(_closed, nameof(AmqpClient)); |
| | 385 | |
|
| 12 | 386 | | return new AmqpProducer |
| 12 | 387 | | ( |
| 12 | 388 | | EventHubName, |
| 12 | 389 | | partitionId, |
| 12 | 390 | | ConnectionScope, |
| 12 | 391 | | MessageConverter, |
| 12 | 392 | | retryPolicy |
| 12 | 393 | | ); |
| | 394 | | } |
| | 395 | |
|
| | 396 | | /// <summary> |
| | 397 | | /// Creates a consumer strongly aligned with the active protocol and transport, responsible |
| | 398 | | /// for reading <see cref="EventData" /> from a specific Event Hub partition, in the context |
| | 399 | | /// of a specific consumer group. |
| | 400 | | /// |
| | 401 | | /// A consumer may be exclusive, which asserts ownership over the partition for the consumer |
| | 402 | | /// group to ensure that only one consumer from that group is reading the from the partition. |
| | 403 | | /// These exclusive consumers are sometimes referred to as "Epoch Consumers." |
| | 404 | | /// |
| | 405 | | /// A consumer may also be non-exclusive, allowing multiple consumers from the same consumer |
| | 406 | | /// group to be actively reading events from the partition. These non-exclusive consumers are |
| | 407 | | /// sometimes referred to as "Non-epoch Consumers." |
| | 408 | | /// |
| | 409 | | /// Designating a consumer as exclusive may be specified by setting the <paramref name="ownerLevel" />. |
| | 410 | | /// When <c>null</c>, consumers are created as non-exclusive. |
| | 411 | | /// </summary> |
| | 412 | | /// |
| | 413 | | /// <param name="consumerGroup">The name of the consumer group this consumer is associated with. Events are rea |
| | 414 | | /// <param name="partitionId">The identifier of the Event Hub partition from which events will be received.</par |
| | 415 | | /// <param name="eventPosition">The position within the partition where the consumer should begin reading events |
| | 416 | | /// <param name="retryPolicy">The policy which governs retry behavior and try timeouts.</param> |
| | 417 | | /// <param name="trackLastEnqueuedEventProperties">Indicates whether information on the last enqueued event on t |
| | 418 | | /// <param name="ownerLevel">The relative priority to associate with the link; for a non-exclusive link, this va |
| | 419 | | /// <param name="prefetchCount">Controls the number of events received and queued locally without regard to whet |
| | 420 | | /// |
| | 421 | | /// <returns>A <see cref="TransportConsumer" /> configured in the requested manner.</returns> |
| | 422 | | /// |
| | 423 | | public override TransportConsumer CreateConsumer(string consumerGroup, |
| | 424 | | string partitionId, |
| | 425 | | EventPosition eventPosition, |
| | 426 | | EventHubsRetryPolicy retryPolicy, |
| | 427 | | bool trackLastEnqueuedEventProperties, |
| | 428 | | long? ownerLevel, |
| | 429 | | uint? prefetchCount) |
| | 430 | | { |
| 64 | 431 | | Argument.AssertNotClosed(_closed, nameof(AmqpClient)); |
| | 432 | |
|
| 62 | 433 | | return new AmqpConsumer |
| 62 | 434 | | ( |
| 62 | 435 | | EventHubName, |
| 62 | 436 | | consumerGroup, |
| 62 | 437 | | partitionId, |
| 62 | 438 | | eventPosition, |
| 62 | 439 | | trackLastEnqueuedEventProperties, |
| 62 | 440 | | ownerLevel, |
| 62 | 441 | | prefetchCount, |
| 62 | 442 | | ConnectionScope, |
| 62 | 443 | | MessageConverter, |
| 62 | 444 | | retryPolicy |
| 62 | 445 | | ); |
| | 446 | | } |
| | 447 | |
|
| | 448 | | /// <summary> |
| | 449 | | /// Closes the connection to the transport client instance. |
| | 450 | | /// </summary> |
| | 451 | | /// |
| | 452 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 453 | | /// |
| | 454 | | public override async Task CloseAsync(CancellationToken cancellationToken) |
| | 455 | | { |
| 30 | 456 | | if (_closed) |
| | 457 | | { |
| 0 | 458 | | return; |
| | 459 | | } |
| | 460 | |
|
| 30 | 461 | | _closed = true; |
| | 462 | |
|
| 30 | 463 | | var clientId = GetHashCode().ToString(CultureInfo.InvariantCulture); |
| 30 | 464 | | var clientType = GetType().Name; |
| | 465 | |
|
| | 466 | | try |
| | 467 | | { |
| 30 | 468 | | EventHubsEventSource.Log.ClientCloseStart(clientType, EventHubName, clientId); |
| 30 | 469 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 470 | |
|
| 28 | 471 | | if (ManagementLink?.TryGetOpenedObject(out var _) == true) |
| | 472 | | { |
| 0 | 473 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 0 | 474 | | await ManagementLink.CloseAsync().ConfigureAwait(false); |
| | 475 | | } |
| | 476 | |
|
| 28 | 477 | | ManagementLink?.Dispose(); |
| 28 | 478 | | ConnectionScope?.Dispose(); |
| 28 | 479 | | } |
| 2 | 480 | | catch (Exception ex) |
| | 481 | | { |
| 2 | 482 | | _closed = false; |
| 2 | 483 | | EventHubsEventSource.Log.ClientCloseError(clientType, EventHubName, clientId, ex.Message); |
| | 484 | |
|
| 2 | 485 | | throw; |
| | 486 | | } |
| | 487 | | finally |
| | 488 | | { |
| 30 | 489 | | EventHubsEventSource.Log.ClientCloseComplete(clientType, EventHubName, clientId); |
| | 490 | | } |
| 28 | 491 | | } |
| | 492 | |
|
| | 493 | | /// <summary> |
| | 494 | | /// Acquires an access token for authorization with the Event Hubs service. |
| | 495 | | /// </summary> |
| | 496 | | /// |
| | 497 | | /// <returns>The token to use for service authorization.</returns> |
| | 498 | | /// |
| | 499 | | private async Task<string> AcquireAccessTokenAsync(CancellationToken cancellationToken) |
| | 500 | | { |
| 72 | 501 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 502 | |
|
| 72 | 503 | | AccessToken activeToken = _accessToken; |
| | 504 | |
|
| | 505 | | // If there was no current token, or it is within the buffer for expiration, request a new token. |
| | 506 | | // There is a benign race condition here, where there may be multiple requests in-flight for a new token. S |
| | 507 | | // overlapping requests should be within a small window, allow the acquired token to replace the current one |
| | 508 | | // attempting to coordinate or ensure that the most recent is kept. |
| | 509 | |
|
| 72 | 510 | | if ((string.IsNullOrEmpty(activeToken.Token)) || (activeToken.ExpiresOn <= DateTimeOffset.UtcNow.Add(Credent |
| | 511 | | { |
| 36 | 512 | | activeToken = await Credential.GetTokenUsingDefaultScopeAsync(cancellationToken).ConfigureAwait(false); |
| | 513 | |
|
| 36 | 514 | | if ((string.IsNullOrEmpty(activeToken.Token))) |
| | 515 | | { |
| 0 | 516 | | throw new AuthenticationException(Resources.CouldNotAcquireAccessToken); |
| | 517 | | } |
| | 518 | |
|
| 36 | 519 | | _accessToken = activeToken; |
| | 520 | | } |
| | 521 | |
|
| 72 | 522 | | return activeToken.Token; |
| 72 | 523 | | } |
| | 524 | |
|
| | 525 | | /// <summary> |
| | 526 | | /// Uses the minimum value of the two specified <see cref="TimeSpan" /> instances. |
| | 527 | | /// </summary> |
| | 528 | | /// |
| | 529 | | /// <param name="firstOption">The first option to consider.</param> |
| | 530 | | /// <param name="secondOption">The second option to consider.</param> |
| | 531 | | /// |
| | 532 | | /// <returns></returns> |
| | 533 | | /// |
| | 534 | | private static TimeSpan UseMinimum(TimeSpan firstOption, |
| 68 | 535 | | TimeSpan secondOption) => (firstOption < secondOption) ? firstOption : second |
| | 536 | | } |
| | 537 | | } |