Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | // Copyright 2015 gRPC authors. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 3 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 7 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 15 | #endregion |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 16 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 17 | using System; |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 18 | using System.Collections.Generic; |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 19 | using System.Threading; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 20 | using System.Threading.Tasks; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 21 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 22 | using Grpc.Core.Internal; |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 23 | using Grpc.Core.Logging; |
| 24 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 25 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 26 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 27 | { |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 28 | /// <summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 29 | /// Represents a gRPC channel. Channels are an abstraction of long-lived connections to remote servers. |
| 30 | /// More client objects can reuse the same channel. Creating a channel is an expensive operation compared to invoking |
| 31 | /// a remote call so in general you should reuse a single channel for as many calls as possible. |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 32 | /// </summary> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 33 | public class Channel |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 34 | { |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 35 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Channel>(); |
| 36 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 37 | readonly object myLock = new object(); |
| 38 | readonly AtomicCounter activeCallCounter = new AtomicCounter(); |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 39 | readonly CancellationTokenSource shutdownTokenSource = new CancellationTokenSource(); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 40 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 41 | readonly string target; |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 42 | readonly GrpcEnvironment environment; |
Jan Tattermusch | 5ee8e77 | 2016-05-24 16:17:10 -0400 | [diff] [blame] | 43 | readonly CompletionQueueSafeHandle completionQueue; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 44 | readonly ChannelSafeHandle handle; |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 45 | readonly Dictionary<string, ChannelOption> options; |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 46 | |
Jan Tattermusch | 8a50781 | 2017-05-08 16:30:12 +0200 | [diff] [blame] | 47 | readonly Task connectivityWatcherTask; |
| 48 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 49 | bool shutdownRequested; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 50 | |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 51 | /// <summary> |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 52 | /// Creates a channel that connects to a specific host. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 53 | /// Port will default to 80 for an unsecure channel and to 443 for a secure channel. |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 54 | /// </summary> |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 55 | /// <param name="target">Target of the channel.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 56 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 8d829d0 | 2016-06-06 16:43:54 -0700 | [diff] [blame] | 57 | public Channel(string target, ChannelCredentials credentials) : |
| 58 | this(target, credentials, null) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | /// <summary> |
| 63 | /// Creates a channel that connects to a specific host. |
| 64 | /// Port will default to 80 for an unsecure channel and to 443 for a secure channel. |
| 65 | /// </summary> |
| 66 | /// <param name="target">Target of the channel.</param> |
| 67 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 68 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | 8d829d0 | 2016-06-06 16:43:54 -0700 | [diff] [blame] | 69 | public Channel(string target, ChannelCredentials credentials, IEnumerable<ChannelOption> options) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 70 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 71 | this.target = GrpcPreconditions.CheckNotNull(target, "target"); |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 72 | this.options = CreateOptionsDictionary(options); |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 73 | EnsureUserAgentChannelOption(this.options); |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 74 | this.environment = GrpcEnvironment.AddRef(); |
| 75 | |
Jan Tattermusch | 5ee8e77 | 2016-05-24 16:17:10 -0400 | [diff] [blame] | 76 | this.completionQueue = this.environment.PickCompletionQueue(); |
Jan Tattermusch | 08dea32 | 2015-10-26 17:34:10 -0700 | [diff] [blame] | 77 | using (var nativeCredentials = credentials.ToNativeCredentials()) |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 78 | using (var nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options.Values)) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 79 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 80 | if (nativeCredentials != null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 81 | { |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 82 | this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 83 | } |
| 84 | else |
| 85 | { |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 86 | this.handle = ChannelSafeHandle.CreateInsecure(target, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
Jan Tattermusch | 8a50781 | 2017-05-08 16:30:12 +0200 | [diff] [blame] | 89 | // TODO(jtattermusch): Workaround for https://github.com/GoogleCloudPlatform/google-cloud-dotnet/issues/822. |
| 90 | // Remove once retries are supported in C core |
| 91 | this.connectivityWatcherTask = RunConnectivityWatcherAsync(); |
Jan Tattermusch | 4aea528 | 2016-06-01 12:42:54 -0700 | [diff] [blame] | 92 | GrpcEnvironment.RegisterChannel(this); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 93 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 94 | |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 95 | /// <summary> |
| 96 | /// Creates a channel that connects to a specific host and port. |
| 97 | /// </summary> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 98 | /// <param name="host">The name or IP address of the host.</param> |
| 99 | /// <param name="port">The port.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 100 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 8d829d0 | 2016-06-06 16:43:54 -0700 | [diff] [blame] | 101 | public Channel(string host, int port, ChannelCredentials credentials) : |
| 102 | this(host, port, credentials, null) |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | /// <summary> |
| 107 | /// Creates a channel that connects to a specific host and port. |
| 108 | /// </summary> |
| 109 | /// <param name="host">The name or IP address of the host.</param> |
| 110 | /// <param name="port">The port.</param> |
| 111 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 112 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | 8d829d0 | 2016-06-06 16:43:54 -0700 | [diff] [blame] | 113 | public Channel(string host, int port, ChannelCredentials credentials, IEnumerable<ChannelOption> options) : |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 114 | this(string.Format("{0}:{1}", host, port), credentials, options) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 115 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 118 | /// <summary> |
| 119 | /// Gets current connectivity state of this channel. |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 120 | /// After channel is has been shutdown, <c>ChannelState.Shutdown</c> will be returned. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 121 | /// </summary> |
| 122 | public ChannelState State |
| 123 | { |
| 124 | get |
| 125 | { |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 126 | return GetConnectivityState(false); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Jan Tattermusch | 8b451ed | 2017-11-20 11:46:48 +0100 | [diff] [blame] | 130 | // cached handler for watch connectivity state |
| 131 | static readonly BatchCompletionDelegate WatchConnectivityStateHandler = (success, ctx, state) => |
| 132 | { |
Jan Tattermusch | 7b0bca2 | 2018-01-09 12:13:57 +0100 | [diff] [blame] | 133 | var tcs = (TaskCompletionSource<bool>) state; |
| 134 | tcs.SetResult(success); |
Jan Tattermusch | 8b451ed | 2017-11-20 11:46:48 +0100 | [diff] [blame] | 135 | }; |
| 136 | |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 137 | /// <summary> |
| 138 | /// Returned tasks completes once channel state has become different from |
| 139 | /// given lastObservedState. |
| 140 | /// If deadline is reached or and error occurs, returned task is cancelled. |
| 141 | /// </summary> |
Jan Tattermusch | 7b0bca2 | 2018-01-09 12:13:57 +0100 | [diff] [blame] | 142 | public async Task WaitForStateChangedAsync(ChannelState lastObservedState, DateTime? deadline = null) |
| 143 | { |
| 144 | var result = await WaitForStateChangedInternalAsync(lastObservedState, deadline).ConfigureAwait(false); |
| 145 | if (!result) |
| 146 | { |
| 147 | throw new TaskCanceledException("Reached deadline."); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /// <summary> |
| 152 | /// Returned tasks completes once channel state has become different from |
| 153 | /// given lastObservedState (<c>true</c> is returned) or if the wait has timed out (<c>false</c> is returned). |
| 154 | /// </summary> |
| 155 | internal Task<bool> WaitForStateChangedInternalAsync(ChannelState lastObservedState, DateTime? deadline = null) |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 156 | { |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 157 | GrpcPreconditions.CheckArgument(lastObservedState != ChannelState.Shutdown, |
| 158 | "Shutdown is a terminal state. No further state changes can occur."); |
Jan Tattermusch | 7b0bca2 | 2018-01-09 12:13:57 +0100 | [diff] [blame] | 159 | var tcs = new TaskCompletionSource<bool>(); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 160 | var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture; |
Jan Tattermusch | 737f6e7 | 2017-12-07 18:07:35 +0100 | [diff] [blame] | 161 | lock (myLock) |
| 162 | { |
| 163 | // pass "tcs" as "state" for WatchConnectivityStateHandler. |
| 164 | handle.WatchConnectivityState(lastObservedState, deadlineTimespec, completionQueue, WatchConnectivityStateHandler, tcs); |
| 165 | } |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 166 | return tcs.Task; |
| 167 | } |
| 168 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 169 | /// <summary>Resolved address of the remote endpoint in URI format.</summary> |
| 170 | public string ResolvedTarget |
Jan Tattermusch | dead905 | 2015-08-01 21:34:31 -0700 | [diff] [blame] | 171 | { |
| 172 | get |
| 173 | { |
| 174 | return handle.GetTarget(); |
| 175 | } |
| 176 | } |
| 177 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 178 | /// <summary>The original target used to create the channel.</summary> |
| 179 | public string Target |
| 180 | { |
| 181 | get |
| 182 | { |
| 183 | return this.target; |
| 184 | } |
| 185 | } |
| 186 | |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 187 | /// <summary> |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 188 | /// Returns a token that gets cancelled once <c>ShutdownAsync</c> is invoked. |
| 189 | /// </summary> |
| 190 | public CancellationToken ShutdownToken |
| 191 | { |
| 192 | get |
| 193 | { |
| 194 | return this.shutdownTokenSource.Token; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /// <summary> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 199 | /// Allows explicitly requesting channel to connect without starting an RPC. |
| 200 | /// Returned task completes once state Ready was seen. If the deadline is reached, |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 201 | /// or channel enters the Shutdown state, the task is cancelled. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 202 | /// There is no need to call this explicitly unless your use case requires that. |
| 203 | /// Starting an RPC on a new channel will request connection implicitly. |
| 204 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 205 | /// <param name="deadline">The deadline. <c>null</c> indicates no deadline.</param> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 206 | public async Task ConnectAsync(DateTime? deadline = null) |
| 207 | { |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 208 | var currentState = GetConnectivityState(true); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 209 | while (currentState != ChannelState.Ready) |
| 210 | { |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 211 | if (currentState == ChannelState.Shutdown) |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 212 | { |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 213 | throw new OperationCanceledException("Channel has reached Shutdown state."); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 214 | } |
Jan Tattermusch | 723c34b | 2015-12-07 08:02:01 -0800 | [diff] [blame] | 215 | await WaitForStateChangedAsync(currentState, deadline).ConfigureAwait(false); |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 216 | currentState = GetConnectivityState(false); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | /// <summary> |
Jan Tattermusch | a134fa7 | 2016-06-03 17:24:50 -0700 | [diff] [blame] | 221 | /// Shuts down the channel cleanly. It is strongly recommended to shutdown |
| 222 | /// all previously created channels before exiting from the process. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 223 | /// </summary> |
Jan Tattermusch | a134fa7 | 2016-06-03 17:24:50 -0700 | [diff] [blame] | 224 | /// <remarks> |
| 225 | /// This method doesn't wait for all calls on this channel to finish (nor does |
| 226 | /// it explicitly cancel all outstanding calls). It is user's responsibility to make sure |
| 227 | /// all the calls on this channel have finished (successfully or with an error) |
| 228 | /// before shutting down the channel to ensure channel shutdown won't impact |
| 229 | /// the outcome of those remote calls. |
| 230 | /// </remarks> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 231 | public async Task ShutdownAsync() |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 232 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 233 | lock (myLock) |
| 234 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 235 | GrpcPreconditions.CheckState(!shutdownRequested); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 236 | shutdownRequested = true; |
| 237 | } |
Jan Tattermusch | 4aea528 | 2016-06-01 12:42:54 -0700 | [diff] [blame] | 238 | GrpcEnvironment.UnregisterChannel(this); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 239 | |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 240 | shutdownTokenSource.Cancel(); |
| 241 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 242 | var activeCallCount = activeCallCounter.Count; |
| 243 | if (activeCallCount > 0) |
| 244 | { |
| 245 | Logger.Warning("Channel shutdown was called but there are still {0} active calls for that channel.", activeCallCount); |
| 246 | } |
| 247 | |
Jan Tattermusch | 737f6e7 | 2017-12-07 18:07:35 +0100 | [diff] [blame] | 248 | lock (myLock) |
| 249 | { |
| 250 | handle.Dispose(); |
| 251 | } |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 252 | |
Jan Tattermusch | 8a50781 | 2017-05-08 16:30:12 +0200 | [diff] [blame] | 253 | await Task.WhenAll(GrpcEnvironment.ReleaseAsync(), connectivityWatcherTask).ConfigureAwait(false); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 256 | internal ChannelSafeHandle Handle |
| 257 | { |
| 258 | get |
| 259 | { |
| 260 | return this.handle; |
| 261 | } |
| 262 | } |
| 263 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 264 | internal GrpcEnvironment Environment |
| 265 | { |
| 266 | get |
| 267 | { |
| 268 | return this.environment; |
| 269 | } |
| 270 | } |
| 271 | |
Jan Tattermusch | 5ee8e77 | 2016-05-24 16:17:10 -0400 | [diff] [blame] | 272 | internal CompletionQueueSafeHandle CompletionQueue |
| 273 | { |
| 274 | get |
| 275 | { |
| 276 | return this.completionQueue; |
| 277 | } |
| 278 | } |
| 279 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 280 | internal void AddCallReference(object call) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 281 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 282 | activeCallCounter.Increment(); |
| 283 | |
| 284 | bool success = false; |
| 285 | handle.DangerousAddRef(ref success); |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 286 | GrpcPreconditions.CheckState(success); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | internal void RemoveCallReference(object call) |
| 290 | { |
| 291 | handle.DangerousRelease(); |
| 292 | |
| 293 | activeCallCounter.Decrement(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 294 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 295 | |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 296 | private ChannelState GetConnectivityState(bool tryToConnect) |
| 297 | { |
| 298 | try |
| 299 | { |
Jan Tattermusch | 737f6e7 | 2017-12-07 18:07:35 +0100 | [diff] [blame] | 300 | lock (myLock) |
| 301 | { |
| 302 | return handle.CheckConnectivityState(tryToConnect); |
| 303 | } |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 304 | } |
| 305 | catch (ObjectDisposedException) |
| 306 | { |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 307 | return ChannelState.Shutdown; |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Jan Tattermusch | 8a50781 | 2017-05-08 16:30:12 +0200 | [diff] [blame] | 311 | /// <summary> |
| 312 | /// Constantly Watches channel connectivity status to work around https://github.com/GoogleCloudPlatform/google-cloud-dotnet/issues/822 |
| 313 | /// </summary> |
| 314 | private async Task RunConnectivityWatcherAsync() |
| 315 | { |
| 316 | try |
| 317 | { |
| 318 | var lastState = State; |
| 319 | while (lastState != ChannelState.Shutdown) |
| 320 | { |
| 321 | lock (myLock) |
| 322 | { |
| 323 | if (shutdownRequested) |
| 324 | { |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
Jan Tattermusch | 7b0bca2 | 2018-01-09 12:13:57 +0100 | [diff] [blame] | 329 | // ignore the result |
| 330 | await WaitForStateChangedInternalAsync(lastState, DateTime.UtcNow.AddSeconds(1)).ConfigureAwait(false); |
Jan Tattermusch | 8a50781 | 2017-05-08 16:30:12 +0200 | [diff] [blame] | 331 | lastState = State; |
| 332 | } |
| 333 | } |
| 334 | catch (ObjectDisposedException) { |
| 335 | // during shutdown, channel is going to be disposed. |
| 336 | } |
| 337 | } |
| 338 | |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 339 | private static void EnsureUserAgentChannelOption(Dictionary<string, ChannelOption> options) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 340 | { |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 341 | var key = ChannelOptions.PrimaryUserAgentString; |
| 342 | var userAgentString = ""; |
| 343 | |
| 344 | ChannelOption option; |
| 345 | if (options.TryGetValue(key, out option)) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 346 | { |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 347 | // user-provided userAgentString needs to be at the beginning |
| 348 | userAgentString = option.StringValue + " "; |
| 349 | }; |
| 350 | |
| 351 | // TODO(jtattermusch): it would be useful to also provide .NET/mono version. |
| 352 | userAgentString += string.Format("grpc-csharp/{0}", VersionInfo.CurrentVersion); |
| 353 | |
| 354 | options[ChannelOptions.PrimaryUserAgentString] = new ChannelOption(key, userAgentString); |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 357 | private static Dictionary<string, ChannelOption> CreateOptionsDictionary(IEnumerable<ChannelOption> options) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 358 | { |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 359 | var dict = new Dictionary<string, ChannelOption>(); |
| 360 | if (options == null) |
| 361 | { |
| 362 | return dict; |
| 363 | } |
| 364 | foreach (var option in options) |
| 365 | { |
| 366 | dict.Add(option.Name, option); |
| 367 | } |
| 368 | return dict; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 369 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 370 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 371 | } |