diff --git a/src/DotNext/Func.cs b/src/DotNext/Func.cs index ec5448d0b..833c6df9c 100644 --- a/src/DotNext/Func.cs +++ b/src/DotNext/Func.cs @@ -92,21 +92,16 @@ public static Func Constant(T obj) if (typeof(T) == typeof(bool)) return Unsafe.As>(Constant(Unsafe.As(ref obj))); - // cache nulls - if (obj is null) - return Default!; - // slow path - allocates a new delegate - unsafe - { - return DelegateHelpers.CreateDelegate(&ConstantCore, obj); - } - - static T ConstantCore(object? obj) => (T)obj!; + return obj is null + ? Default! + : obj.ConstantCore; static T? Default() => default; } + private static T ConstantCore(this object obj) => (T)obj; + private static Func Constant(bool value) { return value ? True : False; diff --git a/src/cluster/DotNext.AspNetCore.Cluster/Net/Cluster/Consensus/Raft/Http/RaftHttpCluster.cs b/src/cluster/DotNext.AspNetCore.Cluster/Net/Cluster/Consensus/Raft/Http/RaftHttpCluster.cs index 3fe0c08e8..96bfd4c70 100644 --- a/src/cluster/DotNext.AspNetCore.Cluster/Net/Cluster/Consensus/Raft/Http/RaftHttpCluster.cs +++ b/src/cluster/DotNext.AspNetCore.Cluster/Net/Cluster/Consensus/Raft/Http/RaftHttpCluster.cs @@ -150,7 +150,7 @@ HttpMessageHandler IHostingContext.CreateHttpHandler() public override async Task StartAsync(CancellationToken token) { configurator?.OnStart(this, metadata); - ConfigurationStorage.ActiveConfigurationChanged += GetConfigurationEventHandler(configurationEvents.Writer); + ConfigurationStorage.ActiveConfigurationChanged += configurationEvents.Writer.WriteConfigurationEvent; if (coldStart) { @@ -193,7 +193,7 @@ async Task StopAsync() { configurator?.OnStop(this); duplicationDetector.Trim(100); - ConfigurationStorage.ActiveConfigurationChanged -= GetConfigurationEventHandler(configurationEvents.Writer); + ConfigurationStorage.ActiveConfigurationChanged -= configurationEvents.Writer.WriteConfigurationEvent; configurationEvents.Writer.TryComplete(); await pollingLoopTask.ConfigureAwait(false); pollingLoopTask = Task.CompletedTask; @@ -205,17 +205,6 @@ async Task StopAsync() } } - private static Func GetConfigurationEventHandler(ChannelWriter<(UriEndPoint, bool)> writer) - { - unsafe - { - return DelegateHelpers.CreateDelegate, UriEndPoint, bool, CancellationToken, ValueTask>(&WriteConfigurationEvent, writer); - } - - static ValueTask WriteConfigurationEvent(ChannelWriter<(UriEndPoint, bool)> writer, UriEndPoint address, bool isAdded, CancellationToken token) - => writer.WriteAsync(new(address, isAdded), token); - } - /// ISubscriber? IPeerMesh.TryGetPeer(EndPoint peer) { @@ -243,4 +232,10 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } +} + +file static class RaftHttpClusterHelpers +{ + internal static ValueTask WriteConfigurationEvent(this ChannelWriter<(UriEndPoint, bool)> writer, UriEndPoint address, bool isAdded, CancellationToken token) + => writer.WriteAsync(new(address, isAdded), token); } \ No newline at end of file diff --git a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/RaftCluster.DefaultImpl.cs b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/RaftCluster.DefaultImpl.cs index b978016b2..145f7ba8f 100644 --- a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/RaftCluster.DefaultImpl.cs +++ b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/RaftCluster.DefaultImpl.cs @@ -121,7 +121,7 @@ public RaftCluster(NodeConfiguration configuration) /// The task representing asynchronous execution of the method. public override async Task StartAsync(CancellationToken token = default) { - ConfigurationStorage.ActiveConfigurationChanged += GetConfigurationEventHandler(configurationEvents.Writer); + ConfigurationStorage.ActiveConfigurationChanged += configurationEvents.Writer.WriteConfigurationEvent; if (coldStart) { @@ -151,17 +151,6 @@ public override async Task StartAsync(CancellationToken token = default) await announcer(LocalMemberAddress, metadata, token).ConfigureAwait(false); } - private static Func GetConfigurationEventHandler(ChannelWriter<(EndPoint, bool)> writer) - { - unsafe - { - return DelegateHelpers.CreateDelegate, EndPoint, bool, CancellationToken, ValueTask>(&WriteConfigurationEvent, writer); - } - - static ValueTask WriteConfigurationEvent(ChannelWriter<(EndPoint, bool)> writer, EndPoint address, bool isAdded, CancellationToken token) - => writer.WriteAsync(new(address, isAdded), token); - } - /// protected override ValueTask DetectLocalMemberAsync(RaftClusterMember candidate, CancellationToken token) => new(EndPointComparer.Equals(LocalMemberAddress, candidate.EndPoint)); @@ -181,7 +170,7 @@ async Task StopAsync() { await (server?.DisposeAsync() ?? ValueTask.CompletedTask).ConfigureAwait(false); server = null; - ConfigurationStorage.ActiveConfigurationChanged -= GetConfigurationEventHandler(configurationEvents.Writer); + ConfigurationStorage.ActiveConfigurationChanged -= configurationEvents.Writer.WriteConfigurationEvent; configurationEvents.Writer.TryComplete(); await pollingLoopTask.ConfigureAwait(false); pollingLoopTask = Task.CompletedTask; @@ -355,4 +344,10 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } +} + +file static class RaftClusterHelpers +{ + internal static ValueTask WriteConfigurationEvent(this ChannelWriter<(EndPoint, bool)> writer, EndPoint address, bool isAdded, CancellationToken token) + => writer.WriteAsync(new(address, isAdded), token); } \ No newline at end of file