Skip to content

Commit

Permalink
Use container services to configure middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhen committed Dec 17, 2019
1 parent 58c2d41 commit 1642243
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 118 deletions.
11 changes: 4 additions & 7 deletions Source/Orleankka.Runtime/Cluster/ClusterActorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

namespace Orleankka.Cluster
{
Expand All @@ -14,14 +15,10 @@ class ClusterActorSystem : ActorSystem

readonly IActorMiddleware actorMiddleware;

internal ClusterActorSystem(
Assembly[] assemblies,
IServiceProvider serviceProvider,
IActorRefMiddleware actorRefMiddleware = null,
IActorMiddleware actorMiddleware = null)
: base(assemblies, serviceProvider, actorRefMiddleware)
internal ClusterActorSystem(Assembly[] assemblies, IServiceProvider serviceProvider)
: base(assemblies, serviceProvider)
{
this.actorMiddleware = actorMiddleware ?? DefaultActorMiddleware.Instance;
this.actorMiddleware = serviceProvider.GetService<IActorMiddleware>() ?? DefaultActorMiddleware.Instance;
Register(assemblies);
}

Expand Down
75 changes: 16 additions & 59 deletions Source/Orleankka.Runtime/Cluster/ClusterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,40 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

using Orleans;
using Orleans.ApplicationParts;
using Orleans.CodeGeneration;
using Orleans.Configuration.Overrides;
using Orleans.Hosting;
using Orleans.Runtime;

namespace Orleankka.Cluster
{
using Client;
using Utility;

public sealed class OrleankkaClusterOptions
public static class SiloHostBuilderExtension
{
IActorRefMiddleware actorRefMiddleware;
IActorMiddleware actorMiddleware;

/// <summary>
/// Registers global actor middleware (interceptor).
/// </summary>
/// <param name="middleware">The middleware.</param>
public OrleankkaClusterOptions ActorMiddleware(IActorMiddleware middleware)
{
actorMiddleware = middleware;
return this;
}

/// <summary>
/// Registers global cluster-wide <see cref="ActorRef"/> middleware (interceptor)
/// </summary>
/// <param name="middleware">The middleware.</param>
public OrleankkaClusterOptions ActorRefMiddleware(IActorRefMiddleware middleware)
{
Requires.NotNull(middleware, nameof(middleware));
public static ISiloHostBuilder UseOrleankka(this ISiloHostBuilder builder) =>
builder.ConfigureServices(services => UseOrleankka(builder.GetApplicationPartManager(), services));

if (actorRefMiddleware != null)
throw new InvalidOperationException("ActorRef middleware for cluster has been already registered");
public static ISiloBuilder UseOrleankka(this ISiloBuilder builder) =>
builder.ConfigureServices(services => UseOrleankka(builder.GetApplicationPartManager(), services));

actorRefMiddleware = middleware;
return this;
static void UseOrleankka(IApplicationPartManager apm, IServiceCollection services)
{
Configure(apm, services);
apm.AddApplicationPart(typeof(IClientEndpoint).Assembly)
.WithCodeGeneration();
}
internal void Configure(IApplicationPartManager apm, IServiceCollection services)

static void Configure(IApplicationPartManager apm, IServiceCollection services)
{
var assemblies = apm.ApplicationParts
.OfType<AssemblyPart>().Select(x => x.Assembly)
.ToArray();

services.AddSingleton(sp => new ClusterActorSystem(assemblies, sp, actorRefMiddleware, actorMiddleware));
services.AddSingleton(sp => new ClientActorSystem(assemblies, sp, actorRefMiddleware));
services.AddSingleton(sp => new ClusterActorSystem(assemblies, sp));
services.AddSingleton(sp => new ClientActorSystem(assemblies, sp));

services.AddSingleton<IActorSystem>(sp => sp.GetService<ClusterActorSystem>());
services.AddSingleton<IClientActorSystem>(sp => sp.GetService<ClientActorSystem>());
Expand All @@ -70,7 +54,7 @@ internal void Configure(IApplicationPartManager apm, IServiceCollection services
services.AddOptions<DispatcherOptions>();
}

DispatcherRegistry BuildDispatcherRegistry(IServiceProvider services, IEnumerable<Assembly> assemblies)
static DispatcherRegistry BuildDispatcherRegistry(IServiceProvider services, IEnumerable<Assembly> assemblies)
{
var dispatchActors = assemblies.SelectMany(x => x.GetTypes())
.Where(x => typeof(DispatchActorGrain).IsAssignableFrom(x) && !x.IsAbstract);
Expand All @@ -83,33 +67,6 @@ DispatcherRegistry BuildDispatcherRegistry(IServiceProvider services, IEnumerabl

return dispatcherRegistry;
}
}

public static class SiloHostBuilderExtension
{
public static ISiloHostBuilder UseOrleankka(this ISiloHostBuilder builder) =>
UseOrleankka(builder, new OrleankkaClusterOptions());

public static ISiloHostBuilder UseOrleankka(this ISiloHostBuilder builder, Func<OrleankkaClusterOptions, OrleankkaClusterOptions> configure) =>
UseOrleankka(builder, configure(new OrleankkaClusterOptions()));

public static ISiloHostBuilder UseOrleankka(this ISiloHostBuilder builder, OrleankkaClusterOptions cfg) =>
builder.ConfigureServices(services => UseOrleankka(builder.GetApplicationPartManager(), services, cfg));

public static ISiloBuilder UseOrleankka(this ISiloBuilder builder) =>
UseOrleankka(builder, new OrleankkaClusterOptions());

public static ISiloBuilder UseOrleankka(this ISiloBuilder builder, Func<OrleankkaClusterOptions, OrleankkaClusterOptions> configure) =>
UseOrleankka(builder, configure(new OrleankkaClusterOptions()));

public static ISiloBuilder UseOrleankka(this ISiloBuilder builder, OrleankkaClusterOptions cfg) =>
builder.ConfigureServices(services => UseOrleankka(builder.GetApplicationPartManager(), services, cfg));

static void UseOrleankka(IApplicationPartManager apm, IServiceCollection services, OrleankkaClusterOptions cfg)
{
cfg.Configure(apm, services);
apm.AddApplicationPart(typeof(IClientEndpoint).Assembly).WithCodeGeneration();
}

public static IClientActorSystem ActorSystem(this ISiloHost host) => host.Services.GetRequiredService<IClientActorSystem>();
}
Expand Down
7 changes: 2 additions & 5 deletions Source/Orleankka/ActorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ public abstract class ActorSystem : IActorSystem
readonly IGrainFactory grainFactory;
readonly IActorRefMiddleware actorRefMiddleware;

protected ActorSystem(
Assembly[] assemblies,
IServiceProvider serviceProvider,
IActorRefMiddleware actorRefMiddleware = null)
protected ActorSystem(Assembly[] assemblies, IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
this.grainFactory = serviceProvider.GetService<IGrainFactory>();
this.actorRefMiddleware = actorRefMiddleware ?? DefaultActorRefMiddleware.Instance;
this.actorRefMiddleware = serviceProvider.GetService<IActorRefMiddleware>() ?? DefaultActorRefMiddleware.Instance;

Register(assemblies);
}
Expand Down
7 changes: 2 additions & 5 deletions Source/Orleankka/Client/ClientActorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ public sealed class ClientActorSystem : ActorSystem, IClientActorSystem
{
readonly IGrainFactory grainFactory;

internal ClientActorSystem(
Assembly[] assemblies,
IServiceProvider serviceProvider,
IActorRefMiddleware actorRefMiddleware = null)
: base(assemblies, serviceProvider, actorRefMiddleware)
internal ClientActorSystem(Assembly[] assemblies, IServiceProvider serviceProvider)
: base(assemblies, serviceProvider)
{
grainFactory = serviceProvider.GetService<IGrainFactory>();
}
Expand Down
50 changes: 11 additions & 39 deletions Source/Orleankka/Client/ClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,26 @@

namespace Orleankka.Client
{
using Utility;

public sealed class OrleankkaClientOptions
public static class ClientBuilderExtension
{
IActorRefMiddleware middleware;

/// <summary>
/// Registers global <see cref="ActorRef"/> middleware (interceptor)
/// </summary>
/// <param name="middleware">The middleware.</param>
public OrleankkaClientOptions ActorRefMiddleware(IActorRefMiddleware middleware)
{
Requires.NotNull(middleware, nameof(middleware));

if (this.middleware != null)
throw new InvalidOperationException("ActorRef middleware has been already registered");

this.middleware = middleware;
return this;
}
public static IClientBuilder UseOrleankka(this IClientBuilder builder) =>
builder
.ConfigureServices(services => Configure(builder, services))
.ConfigureApplicationParts(apm => apm
.AddApplicationPart(typeof(IClientEndpoint).Assembly)
.WithCodeGeneration());

internal void Configure(IClientBuilder builder, IServiceCollection services)
static void Configure(IClientBuilder builder, IServiceCollection services)
{
var assemblies = builder.GetApplicationPartManager().ApplicationParts
.OfType<AssemblyPart>().Select(x => x.Assembly)
.ToArray();
.OfType<AssemblyPart>().Select(x => x.Assembly)
.ToArray();

services.AddSingleton<IActorSystem>(sp => sp.GetService<ClientActorSystem>());
services.AddSingleton<IClientActorSystem>(sp => sp.GetService<ClientActorSystem>());

services.AddSingleton(sp => new ClientActorSystem(assemblies, sp, middleware));
services.AddSingleton(sp => new ClientActorSystem(assemblies, sp));
}
}

public static class ClientBuilderExtension
{
public static IClientBuilder UseOrleankka(this IClientBuilder builder) =>
UseOrleankka(builder, new OrleankkaClientOptions());

public static IClientBuilder UseOrleankka(this IClientBuilder builder, Func<OrleankkaClientOptions, OrleankkaClientOptions> configure) =>
UseOrleankka(builder, configure(new OrleankkaClientOptions()));

public static IClientBuilder UseOrleankka(this IClientBuilder builder, OrleankkaClientOptions cfg) =>
builder
.ConfigureServices(services => cfg.Configure(builder, services))
.ConfigureApplicationParts(apm => apm
.AddApplicationPart(typeof(IClientEndpoint).Assembly)
.WithCodeGeneration());

public static IClientActorSystem ActorSystem(this IClusterClient client) => client.ServiceProvider.GetRequiredService<IClientActorSystem>();
}
Expand Down
7 changes: 4 additions & 3 deletions Tests/Orleankka.Tests/Testing/TestActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ public override void BeforeTest(ITest test)
var customConventions = Dispatcher.DefaultHandlerNamingConventions.Concat(new[]{"OnFoo"});
o.HandlerNamingConventions = customConventions.ToArray();
});
services.AddSingleton<IActorRefMiddleware>(s => new TestActorRefMiddleware());
services.AddSingleton<IActorMiddleware>(s => new TestActorMiddleware());
})
.ConfigureApplicationParts(x => x
.AddApplicationPart(GetType().Assembly)
.AddApplicationPart(typeof(MemoryGrainStorage).Assembly)
.WithCodeGeneration())
.UseOrleankka(x => x
.ActorMiddleware(new TestActorMiddleware())
.ActorRefMiddleware(new TestActorRefMiddleware()))
.UseOrleankka()
.UseOrleankkaLegacyFeatures(x => x
.AddSimpleMessageStreamProvider("sms")
.RegisterPersistentStreamProviders("aqp"));
Expand Down

0 comments on commit 1642243

Please sign in to comment.