/// <summary> /// Returns the options to use when creating a channel. /// </summary> /// <returns>The options to use when creating a channel.</returns> protected virtual IReadOnlyList <ChannelOption> GetChannelOptions() => UserAgent == null ? s_defaultChannelPoolOptions : new List <ChannelOption>(s_defaultChannelPoolOptions) { GrpcChannelOptions.PrimaryUserAgent(UserAgent) };
/// <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 = _lazyScopedDefaultChannelCredentials.Value.ResultWithUnwrappedExceptions(); return(GetChannel(grpcAdapter, endpoint, channelOptions, credentials)); }
/// <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)); }
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(endpoint, credentials, channelOptions); _channels[key] = channel; } return(channel); } }
/// <summary> /// Converts a <see cref="GrpcChannelOptions"/> (defined in Google.Api.Gax.Grpc) into a list /// of ChannelOption (defined in Grpc.Core). This is generic to allow the simple use of delegates /// for option factories. Internal for testing. /// </summary> /// <param name="options">The options to convert. Must not be null.</param> /// <param name="int32OptionFactory">Factory delegate to create an option from an integer value</param> /// <param name="stringOptionFactory">Factory delegate to create an option from an integer value</param> internal static IReadOnlyList <T> ConvertOptions <T>(GrpcChannelOptions options, Func <string, int, T> int32OptionFactory, Func <string, string, T> stringOptionFactory) { GaxPreconditions.CheckNotNull(options, nameof(options)); List <T> ret = new List <T>(); if (options.EnableServiceConfigResolution is bool enableServiceConfigResolution) { ret.Add(int32OptionFactory(ServiceConfigDisableResolution, enableServiceConfigResolution ? 0 : 1)); } if (options.KeepAliveTime is TimeSpan keepAlive) { ret.Add(int32OptionFactory(KeepAliveTimeMs, (int)keepAlive.TotalMilliseconds)); } if (options.KeepAliveTimeout is TimeSpan keepAliveout) { ret.Add(int32OptionFactory(KeepAliveTimeoutMs, (int)keepAliveout.TotalMilliseconds)); } if (options.MaxReceiveMessageSize is int maxReceiveMessageSize) { ret.Add(int32OptionFactory(MaxReceiveMessageLength, maxReceiveMessageSize)); } if (options.MaxSendMessageSize is int maxSendMessageSize) { ret.Add(int32OptionFactory(MaxSendMessageLength, maxSendMessageSize)); } if (options.PrimaryUserAgent is string primaryUserAgent) { ret.Add(stringOptionFactory(PrimaryUserAgentString, primaryUserAgent)); } foreach (var customOption in options.CustomOptions) { var channelOption = customOption.Type switch { GrpcChannelOptions.CustomOption.OptionType.Integer => int32OptionFactory(customOption.Name, customOption.IntegerValue), GrpcChannelOptions.CustomOption.OptionType.String => stringOptionFactory(customOption.Name, customOption.StringValue), _ => throw new InvalidOperationException($"Unknown custom option type: {customOption.Type}") }; ret.Add(channelOption); } return(ret.AsReadOnly()); } }
/// <inheritdoc /> private protected override ChannelBase CreateChannelImpl(ServiceMetadata serviceMetadata, string endpoint, ChannelCredentials credentials, GrpcChannelOptions options) => channelFactory.Value.Invoke(endpoint, credentials, options);
/// <summary> /// Creates a channel for the given endpoint, using the given credentials and options. All parameters /// are pre-validated to be non-null. /// </summary> /// <param name="endpoint">The endpoint to connect to. Will not be null.</param> /// <param name="credentials">The channel credentials to use. Will not be null.</param> /// <param name="options">The channel options to use. Will not be null.</param> /// <returns>A channel for the specified settings.</returns> protected abstract ChannelBase CreateChannelImpl(string endpoint, ChannelCredentials credentials, GrpcChannelOptions options);
/// <summary> /// Creates a channel for the given endpoint, using the given credentials and options. /// </summary> /// <param name="endpoint">The endpoint to connect to. Must not be null.</param> /// <param name="credentials">The channel credentials to use. Must not be null.</param> /// <param name="options">The channel options to use. Must not be null.</param> /// <returns>A channel for the specified settings.</returns> public ChannelBase CreateChannel(string endpoint, ChannelCredentials credentials, GrpcChannelOptions options) { GaxPreconditions.CheckNotNull(endpoint, nameof(endpoint)); GaxPreconditions.CheckNotNull(credentials, nameof(credentials)); GaxPreconditions.CheckNotNull(options, nameof(options)); return(CreateChannelImpl(endpoint, credentials, options)); }
public Key(GrpcAdapter grpcAdapter, string endpoint, GrpcChannelOptions options) => (GrpcAdapter, Endpoint, Options) = (grpcAdapter, endpoint, options);
/// <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(_lazyScopedDefaultChannelCredentials.Value, cancellationToken).ConfigureAwait(false); return(GetChannel(grpcAdapter, endpoint, channelOptions, credentials)); }
// Internal for testing internal static global::Grpc.Net.Client.GrpcChannelOptions ConvertOptions(ChannelCredentials credentials, GrpcChannelOptions options) { // If service config resolution is explicitly enabled, throw - we can't support that, // and users may be depending on it. if (options.EnableServiceConfigResolution == true) { throw new ArgumentException($"{nameof(options.EnableServiceConfigResolution)} is not currently supported in {nameof(GrpcNetClientAdapter)}"); } // Options we ignore: // - PrimaryUserAgent // - KeepAliveTime // - Custom options return(new global::Grpc.Net.Client.GrpcChannelOptions { Credentials = credentials, MaxReceiveMessageSize = options.MaxReceiveMessageSize, MaxSendMessageSize = options.MaxSendMessageSize }); }
/// <inheritdoc /> private protected override ChannelBase CreateChannelImpl(ServiceMetadata serviceMetadata, string endpoint, ChannelCredentials credentials, GrpcChannelOptions options) { var grpcNetClientOptions = ConvertOptions(credentials, options); _optionsConfigurationAction?.Invoke(grpcNetClientOptions); var address = ConvertEndpoint(endpoint); return(GrpcChannel.ForAddress(address, grpcNetClientOptions)); }
/// <summary> /// Creates a channel for the given endpoint, using the given credentials and options. All parameters /// are pre-validated to be non-null. /// </summary> /// <param name="apiMetadata"></param> /// <param name="endpoint">The endpoint to connect to. Will not be null.</param> /// <param name="credentials">The channel credentials to use. Will not be null.</param> /// <param name="options">The channel options to use. Will not be null.</param> /// <returns>A channel for the specified settings.</returns> private protected abstract ChannelBase CreateChannelImpl(ServiceMetadata apiMetadata, string endpoint, ChannelCredentials credentials, GrpcChannelOptions options);
/// <summary> /// Creates a channel for the given endpoint, using the given credentials and options. /// </summary> /// <param name="serviceMetadata">The metadata for the service. Must not be null.</param> /// <param name="endpoint">The endpoint to connect to. Must not be null.</param> /// <param name="credentials">The channel credentials to use. Must not be null.</param> /// <param name="options">The channel options to use. Must not be null.</param> /// <returns>A channel for the specified settings.</returns> internal ChannelBase CreateChannel(ServiceMetadata serviceMetadata, string endpoint, ChannelCredentials credentials, GrpcChannelOptions options) { if (!SupportsApi(serviceMetadata)) { throw new ArgumentException($"API {serviceMetadata.Name} does not have any transports in common with {GetType().Name}"); } GaxPreconditions.CheckNotNull(endpoint, nameof(endpoint)); GaxPreconditions.CheckNotNull(credentials, nameof(credentials)); GaxPreconditions.CheckNotNull(options, nameof(options)); return(CreateChannelImpl(serviceMetadata, endpoint, credentials, options)); }