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.Runtime.InteropServices; |
| 36 | using System.Threading; |
| 37 | using System.Threading.Tasks; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 38 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 39 | using Grpc.Core.Internal; |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 40 | using Grpc.Core.Logging; |
| 41 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 42 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 43 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 44 | { |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 45 | /// <summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame^] | 46 | /// Represents a gRPC channel. Channels are an abstraction of long-lived connections to remote servers. |
| 47 | /// More client objects can reuse the same channel. Creating a channel is an expensive operation compared to invoking |
| 48 | /// 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] | 49 | /// </summary> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 50 | public class Channel |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 51 | { |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 52 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Channel>(); |
| 53 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 54 | readonly object myLock = new object(); |
| 55 | readonly AtomicCounter activeCallCounter = new AtomicCounter(); |
| 56 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 57 | readonly string target; |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 58 | readonly GrpcEnvironment environment; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 59 | readonly ChannelSafeHandle handle; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 60 | readonly List<ChannelOption> options; |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 61 | |
| 62 | bool shutdownRequested; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 63 | |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 64 | /// <summary> |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 65 | /// Creates a channel that connects to a specific host. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 66 | /// 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] | 67 | /// </summary> |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 68 | /// <param name="target">Target of the channel.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 69 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 70 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 71 | public Channel(string target, Credentials credentials, IEnumerable<ChannelOption> options = null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 72 | { |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 73 | this.target = Preconditions.CheckNotNull(target, "target"); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 74 | this.environment = GrpcEnvironment.AddRef(); |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 75 | this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>(); |
| 76 | |
| 77 | EnsureUserAgentChannelOption(this.options); |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 78 | using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials()) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 79 | using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options)) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 80 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 81 | if (nativeCredentials != null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 82 | { |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 83 | this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 84 | } |
| 85 | else |
| 86 | { |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 87 | this.handle = ChannelSafeHandle.CreateInsecure(target, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 90 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 91 | |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 92 | /// <summary> |
| 93 | /// Creates a channel that connects to a specific host and port. |
| 94 | /// </summary> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 95 | /// <param name="host">The name or IP address of the host.</param> |
| 96 | /// <param name="port">The port.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 97 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 98 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 99 | public Channel(string host, int port, Credentials credentials, IEnumerable<ChannelOption> options = null) : |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 100 | this(string.Format("{0}:{1}", host, port), credentials, options) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 101 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 104 | /// <summary> |
| 105 | /// Gets current connectivity state of this channel. |
| 106 | /// </summary> |
| 107 | public ChannelState State |
| 108 | { |
| 109 | get |
| 110 | { |
| 111 | return handle.CheckConnectivityState(false); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// <summary> |
| 116 | /// Returned tasks completes once channel state has become different from |
| 117 | /// given lastObservedState. |
| 118 | /// If deadline is reached or and error occurs, returned task is cancelled. |
| 119 | /// </summary> |
| 120 | public Task WaitForStateChangedAsync(ChannelState lastObservedState, DateTime? deadline = null) |
| 121 | { |
| 122 | Preconditions.CheckArgument(lastObservedState != ChannelState.FatalFailure, |
| 123 | "FatalFailure is a terminal state. No further state changes can occur."); |
| 124 | var tcs = new TaskCompletionSource<object>(); |
| 125 | var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture; |
| 126 | var handler = new BatchCompletionDelegate((success, ctx) => |
| 127 | { |
| 128 | if (success) |
| 129 | { |
| 130 | tcs.SetResult(null); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | tcs.SetCanceled(); |
| 135 | } |
| 136 | }); |
| 137 | handle.WatchConnectivityState(lastObservedState, deadlineTimespec, environment.CompletionQueue, environment.CompletionRegistry, handler); |
| 138 | return tcs.Task; |
| 139 | } |
| 140 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 141 | /// <summary>Resolved address of the remote endpoint in URI format.</summary> |
| 142 | public string ResolvedTarget |
Jan Tattermusch | dead905 | 2015-08-01 21:34:31 -0700 | [diff] [blame] | 143 | { |
| 144 | get |
| 145 | { |
| 146 | return handle.GetTarget(); |
| 147 | } |
| 148 | } |
| 149 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 150 | /// <summary>The original target used to create the channel.</summary> |
| 151 | public string Target |
| 152 | { |
| 153 | get |
| 154 | { |
| 155 | return this.target; |
| 156 | } |
| 157 | } |
| 158 | |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 159 | /// <summary> |
| 160 | /// Allows explicitly requesting channel to connect without starting an RPC. |
| 161 | /// Returned task completes once state Ready was seen. If the deadline is reached, |
| 162 | /// or channel enters the FatalFailure state, the task is cancelled. |
| 163 | /// There is no need to call this explicitly unless your use case requires that. |
| 164 | /// Starting an RPC on a new channel will request connection implicitly. |
| 165 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame^] | 166 | /// <param name="deadline">The deadline. <c>null</c> indicates no deadline.</param> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 167 | public async Task ConnectAsync(DateTime? deadline = null) |
| 168 | { |
| 169 | var currentState = handle.CheckConnectivityState(true); |
| 170 | while (currentState != ChannelState.Ready) |
| 171 | { |
| 172 | if (currentState == ChannelState.FatalFailure) |
| 173 | { |
| 174 | throw new OperationCanceledException("Channel has reached FatalFailure state."); |
| 175 | } |
| 176 | await WaitForStateChangedAsync(currentState, deadline); |
| 177 | currentState = handle.CheckConnectivityState(false); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /// <summary> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 182 | /// Waits until there are no more active calls for this channel and then cleans up |
| 183 | /// resources used by this channel. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame] | 184 | /// </summary> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 185 | public async Task ShutdownAsync() |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 186 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 187 | lock (myLock) |
| 188 | { |
| 189 | Preconditions.CheckState(!shutdownRequested); |
| 190 | shutdownRequested = true; |
| 191 | } |
| 192 | |
| 193 | var activeCallCount = activeCallCounter.Count; |
| 194 | if (activeCallCount > 0) |
| 195 | { |
| 196 | Logger.Warning("Channel shutdown was called but there are still {0} active calls for that channel.", activeCallCount); |
| 197 | } |
| 198 | |
| 199 | handle.Dispose(); |
| 200 | |
| 201 | await Task.Run(() => GrpcEnvironment.Release()); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 204 | internal ChannelSafeHandle Handle |
| 205 | { |
| 206 | get |
| 207 | { |
| 208 | return this.handle; |
| 209 | } |
| 210 | } |
| 211 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 212 | internal GrpcEnvironment Environment |
| 213 | { |
| 214 | get |
| 215 | { |
| 216 | return this.environment; |
| 217 | } |
| 218 | } |
| 219 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 220 | internal void AddCallReference(object call) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 221 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 222 | activeCallCounter.Increment(); |
| 223 | |
| 224 | bool success = false; |
| 225 | handle.DangerousAddRef(ref success); |
| 226 | Preconditions.CheckState(success); |
| 227 | } |
| 228 | |
| 229 | internal void RemoveCallReference(object call) |
| 230 | { |
| 231 | handle.DangerousRelease(); |
| 232 | |
| 233 | activeCallCounter.Decrement(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 234 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 235 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 236 | private static void EnsureUserAgentChannelOption(List<ChannelOption> options) |
| 237 | { |
| 238 | if (!options.Any((option) => option.Name == ChannelOptions.PrimaryUserAgentString)) |
| 239 | { |
| 240 | options.Add(new ChannelOption(ChannelOptions.PrimaryUserAgentString, GetUserAgentString())); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | private static string GetUserAgentString() |
| 245 | { |
| 246 | // TODO(jtattermusch): it would be useful to also provide .NET/mono version. |
| 247 | return string.Format("grpc-csharp/{0}", VersionInfo.CurrentVersion); |
| 248 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 249 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 250 | } |