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 | |
| 130 | /// <summary> |
| 131 | /// Returned tasks completes once channel state has become different from |
| 132 | /// given lastObservedState. |
| 133 | /// If deadline is reached or and error occurs, returned task is cancelled. |
| 134 | /// </summary> |
| 135 | public Task WaitForStateChangedAsync(ChannelState lastObservedState, DateTime? deadline = null) |
| 136 | { |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 137 | GrpcPreconditions.CheckArgument(lastObservedState != ChannelState.Shutdown, |
| 138 | "Shutdown is a terminal state. No further state changes can occur."); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 139 | var tcs = new TaskCompletionSource<object>(); |
| 140 | var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture; |
| 141 | var handler = new BatchCompletionDelegate((success, ctx) => |
| 142 | { |
| 143 | if (success) |
| 144 | { |
| 145 | tcs.SetResult(null); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | tcs.SetCanceled(); |
| 150 | } |
| 151 | }); |
Jan Tattermusch | e6d1de6 | 2016-05-25 19:32:15 -0400 | [diff] [blame] | 152 | handle.WatchConnectivityState(lastObservedState, deadlineTimespec, completionQueue, handler); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 153 | return tcs.Task; |
| 154 | } |
| 155 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 156 | /// <summary>Resolved address of the remote endpoint in URI format.</summary> |
| 157 | public string ResolvedTarget |
Jan Tattermusch | dead905 | 2015-08-01 21:34:31 -0700 | [diff] [blame] | 158 | { |
| 159 | get |
| 160 | { |
| 161 | return handle.GetTarget(); |
| 162 | } |
| 163 | } |
| 164 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 165 | /// <summary>The original target used to create the channel.</summary> |
| 166 | public string Target |
| 167 | { |
| 168 | get |
| 169 | { |
| 170 | return this.target; |
| 171 | } |
| 172 | } |
| 173 | |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 174 | /// <summary> |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 175 | /// Returns a token that gets cancelled once <c>ShutdownAsync</c> is invoked. |
| 176 | /// </summary> |
| 177 | public CancellationToken ShutdownToken |
| 178 | { |
| 179 | get |
| 180 | { |
| 181 | return this.shutdownTokenSource.Token; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /// <summary> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 186 | /// Allows explicitly requesting channel to connect without starting an RPC. |
| 187 | /// 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] | 188 | /// or channel enters the Shutdown state, the task is cancelled. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 189 | /// There is no need to call this explicitly unless your use case requires that. |
| 190 | /// Starting an RPC on a new channel will request connection implicitly. |
| 191 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 192 | /// <param name="deadline">The deadline. <c>null</c> indicates no deadline.</param> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 193 | public async Task ConnectAsync(DateTime? deadline = null) |
| 194 | { |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 195 | var currentState = GetConnectivityState(true); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 196 | while (currentState != ChannelState.Ready) |
| 197 | { |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 198 | if (currentState == ChannelState.Shutdown) |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 199 | { |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 200 | throw new OperationCanceledException("Channel has reached Shutdown state."); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 201 | } |
Jan Tattermusch | 723c34b | 2015-12-07 08:02:01 -0800 | [diff] [blame] | 202 | await WaitForStateChangedAsync(currentState, deadline).ConfigureAwait(false); |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 203 | currentState = GetConnectivityState(false); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | /// <summary> |
Jan Tattermusch | a134fa7 | 2016-06-03 17:24:50 -0700 | [diff] [blame] | 208 | /// Shuts down the channel cleanly. It is strongly recommended to shutdown |
| 209 | /// all previously created channels before exiting from the process. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 210 | /// </summary> |
Jan Tattermusch | a134fa7 | 2016-06-03 17:24:50 -0700 | [diff] [blame] | 211 | /// <remarks> |
| 212 | /// This method doesn't wait for all calls on this channel to finish (nor does |
| 213 | /// it explicitly cancel all outstanding calls). It is user's responsibility to make sure |
| 214 | /// all the calls on this channel have finished (successfully or with an error) |
| 215 | /// before shutting down the channel to ensure channel shutdown won't impact |
| 216 | /// the outcome of those remote calls. |
| 217 | /// </remarks> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 218 | public async Task ShutdownAsync() |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 219 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 220 | lock (myLock) |
| 221 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 222 | GrpcPreconditions.CheckState(!shutdownRequested); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 223 | shutdownRequested = true; |
| 224 | } |
Jan Tattermusch | 4aea528 | 2016-06-01 12:42:54 -0700 | [diff] [blame] | 225 | GrpcEnvironment.UnregisterChannel(this); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 226 | |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 227 | shutdownTokenSource.Cancel(); |
| 228 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 229 | var activeCallCount = activeCallCounter.Count; |
| 230 | if (activeCallCount > 0) |
| 231 | { |
| 232 | Logger.Warning("Channel shutdown was called but there are still {0} active calls for that channel.", activeCallCount); |
| 233 | } |
| 234 | |
| 235 | handle.Dispose(); |
| 236 | |
Jan Tattermusch | 8a50781 | 2017-05-08 16:30:12 +0200 | [diff] [blame] | 237 | await Task.WhenAll(GrpcEnvironment.ReleaseAsync(), connectivityWatcherTask).ConfigureAwait(false); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 240 | internal ChannelSafeHandle Handle |
| 241 | { |
| 242 | get |
| 243 | { |
| 244 | return this.handle; |
| 245 | } |
| 246 | } |
| 247 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 248 | internal GrpcEnvironment Environment |
| 249 | { |
| 250 | get |
| 251 | { |
| 252 | return this.environment; |
| 253 | } |
| 254 | } |
| 255 | |
Jan Tattermusch | 5ee8e77 | 2016-05-24 16:17:10 -0400 | [diff] [blame] | 256 | internal CompletionQueueSafeHandle CompletionQueue |
| 257 | { |
| 258 | get |
| 259 | { |
| 260 | return this.completionQueue; |
| 261 | } |
| 262 | } |
| 263 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 264 | internal void AddCallReference(object call) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 265 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 266 | activeCallCounter.Increment(); |
| 267 | |
| 268 | bool success = false; |
| 269 | handle.DangerousAddRef(ref success); |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 270 | GrpcPreconditions.CheckState(success); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | internal void RemoveCallReference(object call) |
| 274 | { |
| 275 | handle.DangerousRelease(); |
| 276 | |
| 277 | activeCallCounter.Decrement(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 278 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 279 | |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 280 | private ChannelState GetConnectivityState(bool tryToConnect) |
| 281 | { |
| 282 | try |
| 283 | { |
| 284 | return handle.CheckConnectivityState(tryToConnect); |
| 285 | } |
| 286 | catch (ObjectDisposedException) |
| 287 | { |
Jan Tattermusch | 49fb84a | 2016-06-03 16:34:48 -0700 | [diff] [blame] | 288 | return ChannelState.Shutdown; |
Jan Tattermusch | 528fb66 | 2016-05-12 08:38:41 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Jan Tattermusch | 8a50781 | 2017-05-08 16:30:12 +0200 | [diff] [blame] | 292 | /// <summary> |
| 293 | /// Constantly Watches channel connectivity status to work around https://github.com/GoogleCloudPlatform/google-cloud-dotnet/issues/822 |
| 294 | /// </summary> |
| 295 | private async Task RunConnectivityWatcherAsync() |
| 296 | { |
| 297 | try |
| 298 | { |
| 299 | var lastState = State; |
| 300 | while (lastState != ChannelState.Shutdown) |
| 301 | { |
| 302 | lock (myLock) |
| 303 | { |
| 304 | if (shutdownRequested) |
| 305 | { |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | try |
| 311 | { |
| 312 | await WaitForStateChangedAsync(lastState, DateTime.UtcNow.AddSeconds(1)).ConfigureAwait(false); |
| 313 | } |
| 314 | catch (TaskCanceledException) |
| 315 | { |
| 316 | // ignore timeout |
| 317 | } |
| 318 | lastState = State; |
| 319 | } |
| 320 | } |
| 321 | catch (ObjectDisposedException) { |
| 322 | // during shutdown, channel is going to be disposed. |
| 323 | } |
| 324 | } |
| 325 | |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 326 | private static void EnsureUserAgentChannelOption(Dictionary<string, ChannelOption> options) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 327 | { |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 328 | var key = ChannelOptions.PrimaryUserAgentString; |
| 329 | var userAgentString = ""; |
| 330 | |
| 331 | ChannelOption option; |
| 332 | if (options.TryGetValue(key, out option)) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 333 | { |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 334 | // user-provided userAgentString needs to be at the beginning |
| 335 | userAgentString = option.StringValue + " "; |
| 336 | }; |
| 337 | |
| 338 | // TODO(jtattermusch): it would be useful to also provide .NET/mono version. |
| 339 | userAgentString += string.Format("grpc-csharp/{0}", VersionInfo.CurrentVersion); |
| 340 | |
| 341 | options[ChannelOptions.PrimaryUserAgentString] = new ChannelOption(key, userAgentString); |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 344 | private static Dictionary<string, ChannelOption> CreateOptionsDictionary(IEnumerable<ChannelOption> options) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 345 | { |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 346 | var dict = new Dictionary<string, ChannelOption>(); |
| 347 | if (options == null) |
| 348 | { |
| 349 | return dict; |
| 350 | } |
| 351 | foreach (var option in options) |
| 352 | { |
| 353 | dict.Add(option.Name, option); |
| 354 | } |
| 355 | return dict; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 356 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 357 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 358 | } |