-
Notifications
You must be signed in to change notification settings - Fork 96
/
AdsChannel.cs
293 lines (262 loc) · 10.6 KB
/
AdsChannel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Google.Ads.Gax.Config;
using Grpc.Auth;
using Grpc.Core;
using Grpc.Net.Client;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace Google.Ads.Gax.Lib
{
/// <summary>
/// A Grpc channel that wraps <code>Google.Api.Core</code> and <code>Grpc.Net.Client</code>
/// implementations.
/// </summary>
public class AdsChannel : ChannelBase, IDisposable
{
private ChannelBase channel;
/// <summary>
/// Keeps track of whether this object is disposed.
/// </summary>
private bool disposed;
/// <summary>
/// A variable to use for locks when disposing this object.
/// </summary>
private readonly object disposeLock = new object();
/// <summary>
/// The gRPC setting name for maximum message length in bytes that can be received.
/// </summary>
private const string GRPC_MAX_RECEIVE_MESSAGE_LENGTH_IN_BYTES_SETTING_NAME =
"grpc.max_receive_message_length";
/// <summary>
/// The gRPC setting name for maximum metadata size in bytes that can be handled.
/// </summary>
private const string GRPC_MAX_METADATA_SIZE_IN_BYTES_SETTING_NAME =
"grpc.max_metadata_size";
/// <summary>
/// The gRPC setting name for HTTP proxy.
/// </summary>
private const string GRPC_HTTP_PROXY_SETTING_NAME = "grpc.http_proxy";
private AdsConfig config;
private static bool GrpcNetClientSupported
{
get;
set;
}
static AdsChannel()
{
GrpcNetClientSupported = DetectGrpcNetClientTransport();
}
/// <summary>
/// Initializes a new instance of the <see cref="AdsChannel"/> class.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="url">Target of the channel.</param>
public AdsChannel(AdsConfig config, string url) : base(url)
{
this.config = config;
this.channel = CreateChannel(url);
}
/// <summary>
/// Create a new <see cref="T:Grpc.Core.CallInvoker" /> for the channel.
/// </summary>
/// <returns>
/// A new <see cref="T:Grpc.Core.CallInvoker" />.
/// </returns>
public override CallInvoker CreateCallInvoker()
{
return channel.CreateCallInvoker();
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources.</param>
/// <remarks>We only have unmanaged resources to dispose, so we don't use
/// <paramref name="disposing"/>.</remarks>
protected virtual void Dispose(bool disposing)
{
// First, mark the object as disposed, so other calls don't have to wait.
lock (disposeLock)
{
if (disposed)
{
return;
}
disposed = true;
}
if (channel is Channel)
{
if ((channel as Channel).State != ChannelState.Shutdown)
{
var shutdownTask = channel.ShutdownAsync();
// Offload waiting for shutdown to finish to another thread.
Task.Run(delegate ()
{
try
{
shutdownTask.Wait();
}
catch (Exception)
{
// Nothing to do, except to log this error.
// TODO(Anash): Add logging once
// https://github.com/googleads/google-ads-dotnet/issues/381 is fixed.
}
});
}
}
else if (channel is GrpcChannel)
{
(channel as GrpcChannel).Dispose();
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting
/// unmanaged resources.
/// </summary>
public void Dispose()
{
// Dispose of unmanaged resources.
Dispose(true);
// Suppress finalization.
GC.SuppressFinalize(this);
}
/// <summary>
/// Finalizes an instance of the <see cref="AdsChannel"/> class.
/// </summary>
~AdsChannel()
{
Dispose(false);
}
internal bool IsDisposed()
{
if (disposed)
{
return true;
}
return (channel is Channel && (channel as Channel).State == ChannelState.Shutdown);
}
/// <summary>
/// Detects if <code>Grpc.Net.Client</code> transport is supported.
/// Always returns false if running on .NET Framework.
/// </summary>
/// <returns><code>True</code> if the <code>Grpc.Net.Client</code> is supported,
/// <code>false</code> otherwise.</returns>
private static bool DetectGrpcNetClientTransport()
{
#if NET5_0_OR_GREATER
try
{
GrpcChannel.ForAddress("https://ignored.com");
return true;
}
catch
{
return false;
}
#else
return false;
#endif
}
/// <summary>
/// Creates the channel.
/// </summary>
/// <param name="url">Target of the channel.</param>
/// <returns>The newly created channel.</returns>
private ChannelBase CreateChannel(string url)
{
ChannelCredentials channelCredentials;
if (config.AuthorizationMethod == AuthorizationMethod.Insecure)
{
channelCredentials = ChannelCredentials.Insecure;
}
else // if config.AuthenticationMode == AuthenticationMode.OAUTH)
{
channelCredentials =
GoogleGrpcCredentials.ToChannelCredentials(config.Credentials);
}
Uri uri = new Uri(url);
if (config.UseGrpcCore || !GrpcNetClientSupported)
{
return CreateGrpcCoreChannel(uri, channelCredentials);
}
return CreateGrpcNetClientChannel(uri, channelCredentials);
}
/// <summary>
/// Creates the channel using Grpc.Net.Client library.
/// </summary>
/// <param name="uri">The chanel URI.</param>
/// <param name="credentials">The channel credentials.</param>
/// <returns>The newly created channel.</returns>
private ChannelBase CreateGrpcNetClientChannel(Uri uri, ChannelCredentials credentials)
{
if (!GrpcNetClientSupported)
{
throw new PlatformNotSupportedException(ErrorMessages.GrpcNetClientNotSupported);
}
GrpcChannelOptions options = new GrpcChannelOptions();
options.MaxReceiveMessageSize = config.MaxReceiveMessageSizeInBytes;
options.Credentials = credentials;
// Since there are no settings on GrpcChannelOptions to set the proxy and trailing
// metadata size, we write our own custom logic.
// TODO(Anash): Use GrpcChannelOptions in the future if
// https://github.com/grpc/grpc-dotnet/issues/1842 is fixed.
// Note: https://github.com/grpc/grpc-dotnet/issues/1842 suggests using
// SocketsHttpHandler directly, but
// https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler?view=netcore-3.1#httpclienthandler-in-net-core
// says that HttpClientHandler will internally use SocketsHttpHandler on newer
// platforms. So we will use HttpClientHandler directly.
HttpClientHandler httpClientHandler = new HttpClientHandler();
if (config.Proxy != null)
{
httpClientHandler.Proxy = config.Proxy;
}
// config.MaxMetadataSizeInBytes is in bytes, but MaxResponseHeadersLength is in KB.
// Note: Since we are dividing by 1024, we are not affected by
// https://github.com/dotnet/runtime/issues/73848
httpClientHandler.MaxResponseHeadersLength = config.MaxMetadataSizeInBytes / 1024;
options.HttpHandler = httpClientHandler;
return GrpcChannel.ForAddress(uri, options);
}
/// <summary>
/// Creates the channel using Grpc.Core library.
/// </summary>
/// <param name="uri">The chanel URI.</param>
/// <param name="credentials">The channel credentials.</param>
/// <returns>The newly created channel.</returns>
private ChannelBase CreateGrpcCoreChannel(Uri uri, ChannelCredentials credentials)
{
// GRPC uses c-ares DNS resolver, which doesn't seem to work on some Windows machines.
// Turn it off for now.
// https://github.com/googleads/google-ads-dotnet/issues/59
Environment.SetEnvironmentVariable("GRPC_DNS_RESOLVER", "native");
List<ChannelOption> options = new List<ChannelOption>()
{
new ChannelOption(GRPC_MAX_RECEIVE_MESSAGE_LENGTH_IN_BYTES_SETTING_NAME,
config.MaxReceiveMessageSizeInBytes),
new ChannelOption(GRPC_MAX_METADATA_SIZE_IN_BYTES_SETTING_NAME,
config.MaxMetadataSizeInBytes),
};
if (config.Proxy != null)
{
options.Add(new ChannelOption(GRPC_HTTP_PROXY_SETTING_NAME,
config.Proxy.Address.ToString()));
}
return new Channel(uri.Host, uri.Port, credentials, options);
}
}
}