Skip to content

Commit

Permalink
di enabled cluster setup (#1823)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeralsing authored Oct 17, 2022
1 parent 6c27bc8 commit 4767ec0
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Proto.Cluster/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,60 @@

namespace Proto.Cluster;

[PublicAPI]
public class HostedClusterConfig
{
public string ClusterName { get; set; } = "MyCluster";
public string BindToHost { get; set; }= "localhost";
public int Port{ get; set; } = 0;
public Func<ActorSystemConfig, ActorSystemConfig>? ConfigureSystem { get; set; }
public Func<GrpcNetRemoteConfig, GrpcNetRemoteConfig>? ConfigureRemote { get; set; }
public Func<ClusterConfig, ClusterConfig>? ConfigureCluster { get; set; }
public IClusterProvider? ClusterProvider { get; set; }
public IIdentityLookup? IdentityLookup { get; set; }
public bool RunAsClient { get; set; }
}

[PublicAPI]
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddProtoCluster(this IServiceCollection self, Action<IServiceProvider, HostedClusterConfig> configure)
{
var boot = new HostedClusterConfig();
self.AddSingleton(p =>
{
var loggerFactory = p.GetRequiredService<ILoggerFactory>();
Log.SetLoggerFactory(loggerFactory);
configure(p, boot);
var s = new ActorSystemConfig();
s = boot.ConfigureSystem?.Invoke(s) ?? s;
var r = GrpcNetRemoteConfig.BindTo(boot.BindToHost, boot.Port);
r = boot.ConfigureRemote?.Invoke(r) ?? r;
boot.ClusterProvider ??= new SeedNodeClusterProvider();
boot.IdentityLookup ??= new PartitionIdentityLookup();
var c = ClusterConfig.Setup(boot.ClusterName, boot.ClusterProvider, boot.IdentityLookup);
c = boot.ConfigureCluster?.Invoke(c) ?? c;
var system = new ActorSystem(s)
.WithRemote(r)
.WithCluster(c)
.WithServiceProvider(p);
return system;
});

self.AddSingleton(p => p.GetRequiredService<ActorSystem>().Cluster());
self.AddSingleton(p => p.GetRequiredService<ActorSystem>().Root);
self.AddHostedService(p => new ProtoActorLifecycleHost(p.GetRequiredService<Cluster>(), boot.RunAsClient));

return self;
}

public static IServiceCollection AddProtoCluster(this IServiceCollection self, string clusterName,
string bindToHost = "localhost", int port = 0,
Func<ActorSystemConfig, ActorSystemConfig>? configureSystem = null,
Expand Down

0 comments on commit 4767ec0

Please sign in to comment.