Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make to accept a factory for GrpcChannelOptions #678

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix formatting
  • Loading branch information
mayuki committed Sep 19, 2023
commit 4d30f480b654c7c2767c030c1b34237e2b9dd3a3
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 40,17 @@ public static GrpcChannelx CreateChannel(this IGrpcChannelProvider provider, Grp
/// </summary>
public static class GrpcChannelProvider
{
private static IGrpcChannelProvider _defaultProvider;
static IGrpcChannelProvider defaultProvider;

/// <summary>
/// Gets a default channel provider. the provider will be initialized by <see cref="GrpcChannelProviderHost"/>.
/// </summary>
public static IGrpcChannelProvider Default
=> _defaultProvider ?? throw new InvalidOperationException("The default GrpcChannelProvider is not configured yet. Setup GrpcChannelProviderHost or initialize manually. ");
=> defaultProvider ?? throw new InvalidOperationException("The default GrpcChannelProvider is not configured yet. Setup GrpcChannelProviderHost or initialize manually. ");

public static void SetDefaultProvider(IGrpcChannelProvider provider)
{
_defaultProvider = provider;
defaultProvider = provider;
}
}

Expand All @@ -59,34 59,34 @@ public static void SetDefaultProvider(IGrpcChannelProvider provider)
/// </summary>
public sealed class LoggingGrpcChannelProvider : IGrpcChannelProvider
{
private readonly IGrpcChannelProvider _baseProvider;
readonly IGrpcChannelProvider baseProvider;

public LoggingGrpcChannelProvider(IGrpcChannelProvider baseProvider)
{
_baseProvider = baseProvider ?? throw new ArgumentNullException(nameof(baseProvider));
this.baseProvider = baseProvider ?? throw new ArgumentNullException(nameof(baseProvider));
}

public GrpcChannelx CreateChannel(CreateGrpcChannelContext context)
{
var channel = _baseProvider.CreateChannel(context);
var channel = baseProvider.CreateChannel(context);
Debug.Log($"Channel Created: {context.Target.Host}:{context.Target.Port} ({(context.Target.IsInsecure ? "Insecure" : "Secure")}) [{channel.Id}]");
return channel;
}

public IReadOnlyCollection<GrpcChannelx> GetAllChannels()
{
return _baseProvider.GetAllChannels();
return baseProvider.GetAllChannels();
}

public void UnregisterChannel(GrpcChannelx channel)
{
_baseProvider.UnregisterChannel(channel);
baseProvider.UnregisterChannel(channel);
Debug.Log($"Channel Unregistered: {channel.TargetUri.Host}:{channel.TargetUri.Port} [{channel.Id}]");
}

public void ShutdownAllChannels()
{
_baseProvider.ShutdownAllChannels();
baseProvider.ShutdownAllChannels();
}
}

Expand Down Expand Up @@ -186,7 186,7 @@ public DefaultGrpcChannelProvider(GrpcCCoreChannelOptions channelOptions) : base
/// </summary>
public class GrpcNetClientGrpcChannelProvider : GrpcChannelProviderBase
{
readonly Func<GrpcChannelOptions> _defaultChannelOptionsFactory;
readonly Func<GrpcChannelOptions> defaultChannelOptionsFactory;
public GrpcNetClientGrpcChannelProvider()
: this(new GrpcChannelOptions())
{
Expand All @@ -199,7 199,7 @@ public GrpcNetClientGrpcChannelProvider(GrpcChannelOptions options)

public GrpcNetClientGrpcChannelProvider(Func<GrpcChannelOptions> optionsFactory)
{
_defaultChannelOptionsFactory = optionsFactory ?? throw new ArgumentNullException(nameof(optionsFactory));
defaultChannelOptionsFactory = optionsFactory ?? throw new ArgumentNullException(nameof(optionsFactory));
}

/// <summary>
Expand All @@ -208,7 208,7 @@ public GrpcNetClientGrpcChannelProvider(Func<GrpcChannelOptions> optionsFactory)
protected override GrpcChannelx CreateChannelCore(int id, CreateGrpcChannelContext context)
{
var address = new Uri((context.Target.IsInsecure ? "http" : "https") $"://{context.Target.Host}:{context.Target.Port}");
var channelOptions = context.ChannelOptions.Get<GrpcChannelOptions>() ?? _defaultChannelOptionsFactory();
var channelOptions = context.ChannelOptions.Get<GrpcChannelOptions>() ?? defaultChannelOptionsFactory();
var channel = GrpcChannel.ForAddress(address, channelOptions);
var channelHolder = new GrpcChannelx(
id,
Expand All @@ -229,7 229,7 @@ protected override GrpcChannelx CreateChannelCore(int id, CreateGrpcChannelConte
/// </summary>
public class GrpcCCoreGrpcChannelProvider : GrpcChannelProviderBase
{
private readonly GrpcCCoreChannelOptions _defaultChannelOptions;
private readonly GrpcCCoreChannelOptions defaultChannelOptions;

public GrpcCCoreGrpcChannelProvider()
: this(new GrpcCCoreChannelOptions())
Expand All @@ -243,15 243,15 @@ public GrpcCCoreGrpcChannelProvider(IReadOnlyList<ChannelOption> channelOptions)

public GrpcCCoreGrpcChannelProvider(GrpcCCoreChannelOptions channelOptions)
{
_defaultChannelOptions = channelOptions ?? throw new ArgumentNullException(nameof(channelOptions));
defaultChannelOptions = channelOptions ?? throw new ArgumentNullException(nameof(channelOptions));
}

/// <summary>
/// Create a channel to the target and register the channel under the management of the provider.
/// </summary>
protected override GrpcChannelx CreateChannelCore(int id, CreateGrpcChannelContext context)
{
var channelOptions = context.ChannelOptions.Get<GrpcCCoreChannelOptions>() ?? _defaultChannelOptions;
var channelOptions = context.ChannelOptions.Get<GrpcCCoreChannelOptions>() ?? defaultChannelOptions;
var channel = new Channel(context.Target.Host, context.Target.Port, context.Target.IsInsecure ? ChannelCredentials.Insecure : channelOptions.ChannelCredentials, channelOptions.ChannelOptions);
var channelHolder = new GrpcChannelx(
id,
Expand Down
Loading