Skip to content

Commit

Permalink
Automatically add default Akka.Streams HOCON (#489)
Browse files Browse the repository at this point in the history
This is in response to a customer issue where the following happened:

1. Using `StreamRef`s
2. Transmits a `StreamRef` to a node that has never called any materialization functions
3. Serialization error due to missing serializer definitions.

In normal Akka.NET you'd have to fix this by manually appending `ActorMaterializer.DefaultConfig()` as a fallback. In Akka.Hosting we can just do this automatically at startup.
  • Loading branch information
Aaronontheweb authored Aug 30, 2024
1 parent b0afcd9 commit d7cf636
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Akka.Hosting/Akka.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="Akka.DependencyInjection" Version="$(AkkaVersion)" />
<PackageReference Include="Akka.Streams" Version="$(AkkaVersion)" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsVersion)" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Akka.Hosting/AkkaConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public sealed class AkkaConfigurationBuilder
{
internal readonly string ActorSystemName;
internal readonly IServiceCollection ServiceCollection;
internal readonly HashSet<SerializerRegistration> Serializers = new HashSet<SerializerRegistration>();
internal readonly List<Type> Extensions = new List<Type>();
internal readonly HashSet<SerializerRegistration> Serializers = new();
internal readonly List<Type> Extensions = new();

/// <summary>
/// INTERNAL API.
Expand All @@ -95,7 +95,7 @@ public sealed class AkkaConfigurationBuilder
/// Use the provided <see cref="AddSetup"/> method instead.
/// </summary>
[InternalApi]
public readonly HashSet<Setup> Setups = new HashSet<Setup>();
public readonly HashSet<Setup> Setups = new();

/// <summary>
/// The currently configured <see cref="ProviderSelection"/>.
Expand Down
7 changes: 7 additions & 0 deletions src/Akka.Hosting/AkkaHostingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Akka.Configuration;
using Akka.DependencyInjection;
using Akka.Hosting.Configuration;
using Akka.Streams;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -58,6 +59,12 @@ public static IServiceCollection AddAkka(this IServiceCollection services, strin
public static IServiceCollection AddAkka<T>(this IServiceCollection services, string actorSystemName, Action<AkkaConfigurationBuilder, IServiceProvider> builder) where T:AkkaHostedService
{
var b = new AkkaConfigurationBuilder(services, actorSystemName);

// add the default Akka.Streams configuration by default - hurts nothing, but
// ensures that StreamRefs work correctly out of the box in case users
// haven't attempted to materialize a stream yet
b.AddHocon(ActorMaterializer.DefaultConfig(), HoconAddMode.Append);

services.AddSingleton<AkkaConfigurationBuilder>(sp =>
{
builder(b, sp);
Expand Down
3 changes: 1 addition & 2 deletions src/Akka.Hosting/LoggerConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Akka.Configuration;
using Akka.Dispatch;
Expand All @@ -17,7 +16,7 @@ namespace Akka.Hosting
{
public sealed class LoggerConfigBuilder
{
private readonly List<Type> _loggers = new List<Type> { typeof(DefaultLogger) };
private readonly List<Type> _loggers = new() { typeof(DefaultLogger) };
private Type _logMessageFormatter = typeof(DefaultLogMessageFormatter);
internal AkkaConfigurationBuilder Builder { get; }

Expand Down

0 comments on commit d7cf636

Please sign in to comment.