-
Notifications
You must be signed in to change notification settings - Fork 51
/
ChannelPool.cs
161 lines (143 loc) · 7.69 KB
/
ChannelPool.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
/*
* Copyright 2016 Google Inc. All Rights Reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://developers.google.com/open-source/licenses/bsd
*/
using Grpc.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Google.Api.Gax.Grpc
{
/// <summary>
/// A pool of channels for the same service, but with potentially different endpoints. Each endpoint
/// has a single channel. All channels created by this pool use default application credentials.
/// This class is thread-safe.
/// </summary>
public sealed class ChannelPool
{
private readonly ServiceMetadata _serviceMetadata;
private readonly DefaultChannelCredentialsCache _credentialCache;
internal bool UseJwtAccessWithScopes => _credentialCache.UseJwtAccessWithScopes;
// TODO: See if we could use ConcurrentDictionary instead of locking. I suspect the issue would be making an atomic
// "clear and fetch values" for shutdown.
private readonly Dictionary<Key, ChannelBase> _channels = new Dictionary<Key, ChannelBase>();
private readonly object _lock = new object();
/// <summary>
/// Creates a channel pool which will use the given service metadata to determine scopes and the use of self-signed JWTs.
/// </summary>
/// <param name="serviceMetadata">The metadata for the service that this pool will be used with. Must not be null.</param>
public ChannelPool(ServiceMetadata serviceMetadata)
{
_serviceMetadata = GaxPreconditions.CheckNotNull(serviceMetadata, nameof(serviceMetadata));
_credentialCache = new DefaultChannelCredentialsCache(serviceMetadata);
}
/// <summary>
/// Shuts down all the currently-allocated channels asynchronously. This does not prevent the channel
/// pool from being used later on, but the currently-allocated channels will not be reused.
/// </summary>
/// <returns>A task which will complete when all the (current) channels have been shut down.</returns>
public Task ShutdownChannelsAsync()
{
List<ChannelBase> channelsToShutdown;
lock (_lock)
{
channelsToShutdown = _channels.Values.ToList();
_channels.Clear();
}
var shutdownTasks = channelsToShutdown.Select(c => c.ShutdownAsync());
return Task.WhenAll(shutdownTasks);
}
/// <summary>
/// Returns a channel from this pool, creating a new one if there is no channel
/// already associated with <paramref name="endpoint"/>.
/// The specified channel options are applied, but only those options.
/// </summary>
/// <param name="grpcAdapter">The gRPC implementation to use. Must not be null.</param>
/// <param name="endpoint">The endpoint to connect to. Must not be null.</param>
/// <param name="channelOptions">The channel options to include. May be null.</param>
/// <returns>A channel for the specified endpoint.</returns>
internal ChannelBase GetChannel(GrpcAdapter grpcAdapter, string endpoint, GrpcChannelOptions channelOptions)
{
GaxPreconditions.CheckNotNull(grpcAdapter, nameof(grpcAdapter));
GaxPreconditions.CheckNotNull(endpoint, nameof(endpoint));
var credentials = _credentialCache.GetCredentials();
return GetChannel(grpcAdapter, endpoint, channelOptions, credentials);
}
/// <summary>
/// Asynchronously returns a channel from this pool, creating a new one if there is no channel
/// already associated with <paramref name="endpoint"/>.
/// The specified channel options are applied, but only those options.
/// </summary>
/// <param name="grpcAdapter">The gRPC implementation to use. Must not be null.</param>
/// <param name="endpoint">The endpoint to connect to. Must not be null.</param>
/// <param name="channelOptions">The channel options to include. May be null.</param>
/// <param name="cancellationToken">A cancellation token for the operation.</param>
/// <returns>A task representing the asynchronous operation. The value of the completed
/// task will be channel for the specified endpoint.</returns>
internal async Task<ChannelBase> GetChannelAsync(GrpcAdapter grpcAdapter, string endpoint, GrpcChannelOptions channelOptions, CancellationToken cancellationToken)
{
GaxPreconditions.CheckNotNull(grpcAdapter, nameof(grpcAdapter));
GaxPreconditions.CheckNotNull(endpoint, nameof(endpoint));
var credentials = await WithCancellationToken(_credentialCache.GetCredentialsAsync(), cancellationToken).ConfigureAwait(false);
return GetChannel(grpcAdapter, endpoint, channelOptions, credentials);
}
private ChannelBase GetChannel(GrpcAdapter grpcAdapter, string endpoint, GrpcChannelOptions channelOptions, ChannelCredentials credentials)
{
var key = new Key(grpcAdapter, endpoint, channelOptions);
lock (_lock)
{
ChannelBase channel;
if (!_channels.TryGetValue(key, out channel))
{
channel = grpcAdapter.CreateChannel(_serviceMetadata, endpoint, credentials, channelOptions);
_channels[key] = channel;
}
return channel;
}
}
// Note: this is duplicated in Google.Apis.Auth, Google.Apis.Core and Google.Api.Gax.Rest as well so it can stay internal.
// Please change all implementations at the same time.
/// <summary>
/// Returns a task which can be cancelled by the given cancellation token, but otherwise observes the original
/// task's state. This does *not* cancel any work that the original task was doing, and should be used carefully.
/// </summary>
private static Task<T> WithCancellationToken<T>(Task<T> task, CancellationToken cancellationToken)
{
if (!cancellationToken.CanBeCanceled)
{
return task;
}
return ImplAsync();
// Separate async method to allow the above optimization to avoid creating any new state machines etc.
async Task<T> ImplAsync()
{
var cts = new TaskCompletionSource<T>();
using (cancellationToken.Register(() => cts.TrySetCanceled()))
{
var completedTask = await Task.WhenAny(task, cts.Task).ConfigureAwait(false);
return await completedTask.ConfigureAwait(false);
}
}
}
private struct Key : IEquatable<Key>
{
public readonly string Endpoint;
public readonly GrpcChannelOptions Options;
public readonly GrpcAdapter GrpcAdapter;
public Key(GrpcAdapter grpcAdapter, string endpoint, GrpcChannelOptions options) =>
(GrpcAdapter, Endpoint, Options) = (grpcAdapter, endpoint, options);
public override int GetHashCode() =>
GaxEqualityHelpers.CombineHashCodes(
GrpcAdapter.GetHashCode(),
Endpoint.GetHashCode(),
Options.GetHashCode());
public override bool Equals(object obj) => obj is Key other && Equals(other);
public bool Equals(Key other) =>
GrpcAdapter.Equals(other.GrpcAdapter) && Endpoint.Equals(other.Endpoint) && Options.Equals(other.Options);
}
}
}