Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: third_party/grpc/src/csharp/Grpc.Core/Channel.cs

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #region Copyright notice and license
2 // Copyright 2015-2016, Google Inc.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
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.
18 //
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.
30 #endregion
31
32 using System;
33 using System.Collections.Generic;
34 using System.Linq;
35 using System.Threading.Tasks;
36
37 using Grpc.Core.Internal;
38 using Grpc.Core.Logging;
39 using Grpc.Core.Utils;
40
41 namespace Grpc.Core
42 {
43 /// <summary>
44 /// Represents a gRPC channel. Channels are an abstraction of long-lived con nections 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 man y calls as possible.
47 /// </summary>
48 public class Channel
49 {
50 static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Channel> ();
51
52 readonly object myLock = new object();
53 readonly AtomicCounter activeCallCounter = new AtomicCounter();
54
55 readonly string target;
56 readonly GrpcEnvironment environment;
57 readonly ChannelSafeHandle handle;
58 readonly Dictionary<string, ChannelOption> options;
59
60 bool shutdownRequested;
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 sec ure channel.
65 /// </summary>
66 /// <param name="target">Target of the channel.</param>
67 /// <param name="credentials">Credentials to secure the channel.</param>
68 /// <param name="options">Channel options.</param>
69 public Channel(string target, ChannelCredentials credentials, IEnumerabl e<ChannelOption> options = null)
70 {
71 this.target = GrpcPreconditions.CheckNotNull(target, "target");
72 this.options = CreateOptionsDictionary(options);
73 EnsureUserAgentChannelOption(this.options);
74 this.environment = GrpcEnvironment.AddRef();
75
76 using (var nativeCredentials = credentials.ToNativeCredentials())
77 using (var nativeChannelArgs = ChannelOptions.CreateChannelArgs(this .options.Values))
78 {
79 if (nativeCredentials != null)
80 {
81 this.handle = ChannelSafeHandle.CreateSecure(nativeCredentia ls, target, nativeChannelArgs);
82 }
83 else
84 {
85 this.handle = ChannelSafeHandle.CreateInsecure(target, nativ eChannelArgs);
86 }
87 }
88 }
89
90 /// <summary>
91 /// Creates a channel that connects to a specific host and port.
92 /// </summary>
93 /// <param name="host">The name or IP address of the host.</param>
94 /// <param name="port">The port.</param>
95 /// <param name="credentials">Credentials to secure the channel.</param>
96 /// <param name="options">Channel options.</param>
97 public Channel(string host, int port, ChannelCredentials credentials, IE numerable<ChannelOption> options = null) :
98 this(string.Format("{0}:{1}", host, port), credentials, options)
99 {
100 }
101
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 fro m
115 /// given lastObservedState.
116 /// If deadline is reached or and error occurs, returned task is cancell ed.
117 /// </summary>
118 public Task WaitForStateChangedAsync(ChannelState lastObservedState, Dat eTime? deadline = null)
119 {
120 GrpcPreconditions.CheckArgument(lastObservedState != ChannelState.Fa talFailure,
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(dea dline.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, e nvironment.CompletionQueue, environment.CompletionRegistry, handler);
136 return tcs.Task;
137 }
138
139 /// <summary>Resolved address of the remote endpoint in URI format.</sum mary>
140 public string ResolvedTarget
141 {
142 get
143 {
144 return handle.GetTarget();
145 }
146 }
147
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
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 i s 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 requir es that.
162 /// Starting an RPC on a new channel will request connection implicitly.
163 /// </summary>
164 /// <param name="deadline">The deadline. <c>null</c> indicates no deadli ne.</param>
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 Fa talFailure state.");
173 }
174 await WaitForStateChangedAsync(currentState, deadline).Configure Await(false);
175 currentState = handle.CheckConnectivityState(false);
176 }
177 }
178
179 /// <summary>
180 /// Waits until there are no more active calls for this channel and then cleans up
181 /// resources used by this channel.
182 /// </summary>
183 public async Task ShutdownAsync()
184 {
185 lock (myLock)
186 {
187 GrpcPreconditions.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
199 await Task.Run(() => GrpcEnvironment.Release()).ConfigureAwait(false );
200 }
201
202 internal ChannelSafeHandle Handle
203 {
204 get
205 {
206 return this.handle;
207 }
208 }
209
210 internal GrpcEnvironment Environment
211 {
212 get
213 {
214 return this.environment;
215 }
216 }
217
218 internal void AddCallReference(object call)
219 {
220 activeCallCounter.Increment();
221
222 bool success = false;
223 handle.DangerousAddRef(ref success);
224 GrpcPreconditions.CheckState(success);
225 }
226
227 internal void RemoveCallReference(object call)
228 {
229 handle.DangerousRelease();
230
231 activeCallCounter.Decrement();
232 }
233
234 private static void EnsureUserAgentChannelOption(Dictionary<string, Chan nelOption> options)
235 {
236 var key = ChannelOptions.PrimaryUserAgentString;
237 var userAgentString = "";
238
239 ChannelOption option;
240 if (options.TryGetValue(key, out option))
241 {
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.Curr entVersion);
248
249 options[ChannelOptions.PrimaryUserAgentString] = new ChannelOption(k ey, userAgentString);
250 }
251
252 private static Dictionary<string, ChannelOption> CreateOptionsDictionary (IEnumerable<ChannelOption> options)
253 {
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;
264 }
265 }
266 }
OLDNEW
« no previous file with comments | « third_party/grpc/src/csharp/Grpc.Core/Calls.cs ('k') | third_party/grpc/src/csharp/Grpc.Core/ChannelCredentials.cs » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698