Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 2 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 3 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 18 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 30 | #endregion |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 31 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 32 | using System; |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 33 | using System.Collections.Generic; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 34 | using System.Linq; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 35 | using System.Threading.Tasks; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 36 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 37 | using Grpc.Core.Internal; |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 38 | using Grpc.Core.Logging; |
| 39 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 40 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 41 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 42 | { |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 43 | /// <summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 44 | /// Represents a gRPC channel. Channels are an abstraction of long-lived connections to remote servers. |
| 45 | /// More client objects can reuse the same channel. Creating a channel is an expensive operation compared to invoking |
| 46 | /// 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] | 47 | /// </summary> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 48 | public class Channel |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 49 | { |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 50 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Channel>(); |
| 51 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 52 | readonly object myLock = new object(); |
| 53 | readonly AtomicCounter activeCallCounter = new AtomicCounter(); |
| 54 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 55 | readonly string target; |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 56 | readonly GrpcEnvironment environment; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 57 | readonly ChannelSafeHandle handle; |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame^] | 58 | readonly Dictionary<string, ChannelOption> options; |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 59 | |
| 60 | bool shutdownRequested; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 61 | |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 62 | /// <summary> |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 63 | /// Creates a channel that connects to a specific host. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 64 | /// 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] | 65 | /// </summary> |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 66 | /// <param name="target">Target of the channel.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 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 | 5bd7005 | 2015-10-06 16:47:49 -0700 | [diff] [blame] | 69 | public Channel(string target, ChannelCredentials credentials, IEnumerable<ChannelOption> options = null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 70 | { |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 71 | this.target = Preconditions.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 | 08dea32 | 2015-10-26 17:34:10 -0700 | [diff] [blame] | 76 | using (var nativeCredentials = credentials.ToNativeCredentials()) |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame^] | 77 | using (var nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options.Values)) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 78 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 79 | if (nativeCredentials != null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 80 | { |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 81 | this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 82 | } |
| 83 | else |
| 84 | { |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 85 | this.handle = ChannelSafeHandle.CreateInsecure(target, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 88 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 89 | |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 90 | /// <summary> |
| 91 | /// Creates a channel that connects to a specific host and port. |
| 92 | /// </summary> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 93 | /// <param name="host">The name or IP address of the host.</param> |
| 94 | /// <param name="port">The port.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 95 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 96 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | 5bd7005 | 2015-10-06 16:47:49 -0700 | [diff] [blame] | 97 | public Channel(string host, int port, ChannelCredentials credentials, IEnumerable<ChannelOption> options = null) : |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 98 | this(string.Format("{0}:{1}", host, port), credentials, options) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 99 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 102 | /// <summary> |
| 103 | /// Gets current connectivity state of this channel. |
| 104 | /// </summary> |
| 105 | public ChannelState State |
| 106 | { |
| 107 | get |
| 108 | { |
| 109 | return handle.CheckConnectivityState(false); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// <summary> |
| 114 | /// Returned tasks completes once channel state has become different from |
| 115 | /// given lastObservedState. |
| 116 | /// If deadline is reached or and error occurs, returned task is cancelled. |
| 117 | /// </summary> |
| 118 | public Task WaitForStateChangedAsync(ChannelState lastObservedState, DateTime? deadline = null) |
| 119 | { |
| 120 | Preconditions.CheckArgument(lastObservedState != ChannelState.FatalFailure, |
| 121 | "FatalFailure is a terminal state. No further state changes can occur."); |
| 122 | var tcs = new TaskCompletionSource<object>(); |
| 123 | var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture; |
| 124 | var handler = new BatchCompletionDelegate((success, ctx) => |
| 125 | { |
| 126 | if (success) |
| 127 | { |
| 128 | tcs.SetResult(null); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | tcs.SetCanceled(); |
| 133 | } |
| 134 | }); |
| 135 | handle.WatchConnectivityState(lastObservedState, deadlineTimespec, environment.CompletionQueue, environment.CompletionRegistry, handler); |
| 136 | return tcs.Task; |
| 137 | } |
| 138 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 139 | /// <summary>Resolved address of the remote endpoint in URI format.</summary> |
| 140 | public string ResolvedTarget |
Jan Tattermusch | dead905 | 2015-08-01 21:34:31 -0700 | [diff] [blame] | 141 | { |
| 142 | get |
| 143 | { |
| 144 | return handle.GetTarget(); |
| 145 | } |
| 146 | } |
| 147 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 148 | /// <summary>The original target used to create the channel.</summary> |
| 149 | public string Target |
| 150 | { |
| 151 | get |
| 152 | { |
| 153 | return this.target; |
| 154 | } |
| 155 | } |
| 156 | |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 157 | /// <summary> |
| 158 | /// Allows explicitly requesting channel to connect without starting an RPC. |
| 159 | /// Returned task completes once state Ready was seen. If the deadline is reached, |
| 160 | /// or channel enters the FatalFailure state, the task is cancelled. |
| 161 | /// There is no need to call this explicitly unless your use case requires that. |
| 162 | /// Starting an RPC on a new channel will request connection implicitly. |
| 163 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 164 | /// <param name="deadline">The deadline. <c>null</c> indicates no deadline.</param> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 165 | public async Task ConnectAsync(DateTime? deadline = null) |
| 166 | { |
| 167 | var currentState = handle.CheckConnectivityState(true); |
| 168 | while (currentState != ChannelState.Ready) |
| 169 | { |
| 170 | if (currentState == ChannelState.FatalFailure) |
| 171 | { |
| 172 | throw new OperationCanceledException("Channel has reached FatalFailure state."); |
| 173 | } |
Jan Tattermusch | 723c34b | 2015-12-07 08:02:01 -0800 | [diff] [blame] | 174 | await WaitForStateChangedAsync(currentState, deadline).ConfigureAwait(false); |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 175 | currentState = handle.CheckConnectivityState(false); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /// <summary> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 180 | /// Waits until there are no more active calls for this channel and then cleans up |
| 181 | /// resources used by this channel. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 182 | /// </summary> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 183 | public async Task ShutdownAsync() |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 184 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 185 | lock (myLock) |
| 186 | { |
| 187 | Preconditions.CheckState(!shutdownRequested); |
| 188 | shutdownRequested = true; |
| 189 | } |
| 190 | |
| 191 | var activeCallCount = activeCallCounter.Count; |
| 192 | if (activeCallCount > 0) |
| 193 | { |
| 194 | Logger.Warning("Channel shutdown was called but there are still {0} active calls for that channel.", activeCallCount); |
| 195 | } |
| 196 | |
| 197 | handle.Dispose(); |
| 198 | |
Jan Tattermusch | 723c34b | 2015-12-07 08:02:01 -0800 | [diff] [blame] | 199 | await Task.Run(() => GrpcEnvironment.Release()).ConfigureAwait(false); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 202 | internal ChannelSafeHandle Handle |
| 203 | { |
| 204 | get |
| 205 | { |
| 206 | return this.handle; |
| 207 | } |
| 208 | } |
| 209 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 210 | internal GrpcEnvironment Environment |
| 211 | { |
| 212 | get |
| 213 | { |
| 214 | return this.environment; |
| 215 | } |
| 216 | } |
| 217 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 218 | internal void AddCallReference(object call) |
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 | activeCallCounter.Increment(); |
| 221 | |
| 222 | bool success = false; |
| 223 | handle.DangerousAddRef(ref success); |
| 224 | Preconditions.CheckState(success); |
| 225 | } |
| 226 | |
| 227 | internal void RemoveCallReference(object call) |
| 228 | { |
| 229 | handle.DangerousRelease(); |
| 230 | |
| 231 | activeCallCounter.Decrement(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 232 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 233 | |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame^] | 234 | private static void EnsureUserAgentChannelOption(Dictionary<string, ChannelOption> options) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 235 | { |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame^] | 236 | var key = ChannelOptions.PrimaryUserAgentString; |
| 237 | var userAgentString = ""; |
| 238 | |
| 239 | ChannelOption option; |
| 240 | if (options.TryGetValue(key, out option)) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 241 | { |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame^] | 242 | // user-provided userAgentString needs to be at the beginning |
| 243 | userAgentString = option.StringValue + " "; |
| 244 | }; |
| 245 | |
| 246 | // TODO(jtattermusch): it would be useful to also provide .NET/mono version. |
| 247 | userAgentString += string.Format("grpc-csharp/{0}", VersionInfo.CurrentVersion); |
| 248 | |
| 249 | options[ChannelOptions.PrimaryUserAgentString] = new ChannelOption(key, userAgentString); |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame^] | 252 | private static Dictionary<string, ChannelOption> CreateOptionsDictionary(IEnumerable<ChannelOption> options) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 253 | { |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame^] | 254 | var dict = new Dictionary<string, ChannelOption>(); |
| 255 | if (options == null) |
| 256 | { |
| 257 | return dict; |
| 258 | } |
| 259 | foreach (var option in options) |
| 260 | { |
| 261 | dict.Add(option.Name, option); |
| 262 | } |
| 263 | return dict; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 264 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 265 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 266 | } |