| | 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.Collections.Generic; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Core; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Contract for all client entities with Open-Close/Abort state m/c |
| | 14 | | /// main-purpose: closeAll related entities |
| | 15 | | /// </summary> |
| | 16 | | public abstract class ClientEntity : IClientEntity |
| | 17 | | { |
| | 18 | | static int nextId; |
| | 19 | | readonly string clientTypeName; |
| | 20 | | readonly object syncLock; |
| | 21 | | bool isClosedOrClosing; |
| | 22 | |
|
| 12 | 23 | | protected ClientEntity(string clientTypeName, string postfix, RetryPolicy retryPolicy) |
| | 24 | | { |
| 12 | 25 | | this.clientTypeName = clientTypeName; |
| 12 | 26 | | this.ClientId = GenerateClientId(clientTypeName, postfix); |
| 12 | 27 | | this.RetryPolicy = retryPolicy ?? RetryPolicy.Default; |
| 12 | 28 | | this.syncLock = new object(); |
| 12 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Returns true if the client is closed or closing. |
| | 33 | | /// </summary> |
| | 34 | | public bool IsClosedOrClosing |
| | 35 | | { |
| | 36 | | get |
| | 37 | | { |
| 0 | 38 | | lock (syncLock) |
| | 39 | | { |
| 0 | 40 | | return isClosedOrClosing; |
| | 41 | | } |
| 0 | 42 | | } |
| | 43 | | internal set |
| | 44 | | { |
| 0 | 45 | | lock (syncLock) |
| | 46 | | { |
| 0 | 47 | | isClosedOrClosing = value; |
| 0 | 48 | | } |
| 0 | 49 | | } |
| | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Connection object to the service bus namespace. |
| | 54 | | /// </summary> |
| | 55 | | public abstract ServiceBusConnection ServiceBusConnection { get; } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Returns true if connection is owned and false if connection is shared. |
| | 59 | | /// </summary> |
| 0 | 60 | | public bool OwnsConnection { get; internal set; } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Gets the name of the entity. |
| | 64 | | /// </summary> |
| | 65 | | public abstract string Path { get; } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Duration after which individual operations will timeout. |
| | 69 | | /// </summary> |
| | 70 | | public abstract TimeSpan OperationTimeout { get; set; } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Gets the ID to identify this client. This can be used to correlate logs and exceptions. |
| | 74 | | /// </summary> |
| | 75 | | /// <remarks>Every new client has a unique ID (in that process).</remarks> |
| 36 | 76 | | public string ClientId { get; private set; } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Gets the <see cref="ServiceBus.RetryPolicy"/> defined on the client. |
| | 80 | | /// </summary> |
| 12 | 81 | | public RetryPolicy RetryPolicy { get; } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Closes the Client. Closes the connections opened by it. |
| | 85 | | /// </summary> |
| | 86 | | public async Task CloseAsync() |
| | 87 | | { |
| 0 | 88 | | var callClose = false; |
| 0 | 89 | | lock (this.syncLock) |
| | 90 | | { |
| 0 | 91 | | if (!this.IsClosedOrClosing) |
| | 92 | | { |
| 0 | 93 | | this.IsClosedOrClosing = true; |
| 0 | 94 | | callClose = true; |
| | 95 | | } |
| 0 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | if (callClose) |
| | 99 | | { |
| 0 | 100 | | await this.OnClosingAsync().ConfigureAwait(false); |
| 0 | 101 | | if (OwnsConnection && this.ServiceBusConnection.IsClosedOrClosing == false) |
| | 102 | | { |
| 0 | 103 | | await this.ServiceBusConnection.CloseAsync().ConfigureAwait(false); |
| | 104 | | } |
| | 105 | | } |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// Gets a list of currently registered plugins for this client. |
| | 110 | | /// </summary> |
| | 111 | | public abstract IList<ServiceBusPlugin> RegisteredPlugins { get; } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Registers a <see cref="ServiceBusPlugin"/> to be used with this client. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="serviceBusPlugin">The <see cref="ServiceBusPlugin"/> to register.</param> |
| | 117 | | public abstract void RegisterPlugin(ServiceBusPlugin serviceBusPlugin); |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Unregisters a <see cref="ServiceBusPlugin"/>. |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="serviceBusPluginName">The name <see cref="ServiceBusPlugin.Name"/> to be unregistered</param> |
| | 123 | | public abstract void UnregisterPlugin(string serviceBusPluginName); |
| | 124 | |
|
| | 125 | | protected abstract Task OnClosingAsync(); |
| | 126 | |
|
| | 127 | | protected static long GetNextId() |
| | 128 | | { |
| 12 | 129 | | return Interlocked.Increment(ref nextId); |
| | 130 | | } |
| | 131 | |
|
| | 132 | | /// <summary> |
| | 133 | | /// Generates a new client id that can be used to identify a specific client in logs and error messages. |
| | 134 | | /// </summary> |
| | 135 | | /// <param name="postfix">Information that can be appended by the client.</param> |
| | 136 | | protected static string GenerateClientId(string clientTypeName, string postfix = "") |
| | 137 | | { |
| 12 | 138 | | return $"{clientTypeName}{GetNextId()}{postfix}"; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | /// <summary> |
| | 142 | | /// Throw an OperationCanceledException if the object is Closing. |
| | 143 | | /// </summary> |
| | 144 | | protected virtual void ThrowIfClosed() |
| | 145 | | { |
| 0 | 146 | | this.ServiceBusConnection.ThrowIfClosed(); |
| 0 | 147 | | if (this.IsClosedOrClosing) |
| | 148 | | { |
| 0 | 149 | | throw new ObjectDisposedException($"{this.clientTypeName} with Id '{this.ClientId}' has already been clo |
| | 150 | | } |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// Updates the client id. |
| | 155 | | /// </summary> |
| | 156 | | internal void UpdateClientId(string newClientId) |
| | 157 | | { |
| 0 | 158 | | MessagingEventSource.Log.UpdateClientId(this.ClientId, newClientId); |
| 0 | 159 | | this.ClientId = newClientId; |
| 0 | 160 | | } |
| | 161 | | } |
| | 162 | | } |