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 all commits
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
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 @@ -169,6 +169,7 @@ public class DefaultGrpcChannelProvider : GrpcNetClientGrpcChannelProvider
{
public DefaultGrpcChannelProvider() : base() {}
public DefaultGrpcChannelProvider(GrpcChannelOptions channelOptions) : base(channelOptions) {}
public DefaultGrpcChannelProvider(Func<GrpcChannelOptions> channelOptionsFactory) : base(channelOptionsFactory) {}
}
#else
public class DefaultGrpcChannelProvider : GrpcCCoreGrpcChannelProvider
Expand All @@ -185,15 +186,20 @@ public DefaultGrpcChannelProvider(GrpcCCoreChannelOptions channelOptions) : base
/// </summary>
public class GrpcNetClientGrpcChannelProvider : GrpcChannelProviderBase
{
private readonly GrpcChannelOptions _defaultChannelOptions;
readonly Func<GrpcChannelOptions> defaultChannelOptionsFactory;
public GrpcNetClientGrpcChannelProvider()
: this(new GrpcChannelOptions())
{
}

public GrpcNetClientGrpcChannelProvider(GrpcChannelOptions options)
: this(() => options)
{
_defaultChannelOptions = options ?? throw new ArgumentNullException(nameof(options));
}

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

/// <summary>
Expand All @@ -202,7 +208,7 @@ public GrpcNetClientGrpcChannelProvider(GrpcChannelOptions options)
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>() ?? _defaultChannelOptions;
var channelOptions = context.ChannelOptions.Get<GrpcChannelOptions>() ?? defaultChannelOptionsFactory();
var channel = GrpcChannel.ForAddress(address, channelOptions);
var channelHolder = new GrpcChannelx(
id,
Expand All @@ -223,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 @@ -237,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 All @@ -271,4 +277,4 @@ public GrpcCCoreChannelOptions(IReadOnlyList<ChannelOption> channelOptions = nul
}
}
#endif
}
}
Loading