Skip to content

Commit

Permalink
Merge pull request #678 from Cysharp/feature/GrpcNetClientOptionsFactory
Browse files Browse the repository at this point in the history
Make to accept a factory for GrpcChannelOptions
  • Loading branch information
mayuki authored Sep 19, 2023
2 parents edfe2f0 + 4d30f48 commit 7f7c2b1
Showing 1 changed file with 22 additions and 16 deletions.
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
}
}

0 comments on commit 7f7c2b1

Please sign in to comment.