Skip to content

Commit

Permalink
added ShardRegionProxy support to cluster hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Apr 1, 2022
1 parent e18db7c commit 253cf8e
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/Akka.Cluster.Hosting/AkkaClusterHostingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,58 @@ public static AkkaConfigurationBuilder WithShardRegion<TKey>(this AkkaConfigurat
registry.TryRegister<TKey>(shardRegion);
});
}

/// <summary>
/// Starts a ShardRegionProxy that points to a <see cref="ShardRegion"/> hosted on a different role inside the cluster
/// and registers the <see cref="IActorRef"/> with <see cref="TKey"/> in the
/// <see cref="ActorRegistry"/> for this <see cref="ActorSystem"/>.
/// </summary>
/// <param name="builder">The builder instance being configured.</param>
/// <param name="typeName">The name of the entity type</param>
/// <param name="roleName">The role of the Akka.Cluster member that is hosting this <see cref="ShardRegion"/>.</param>
/// <param name="extractEntityId">
/// Partial function to extract the entity id and the message to send to the entity from the incoming message,
/// if the partial function does not match the message will be `unhandled`,
/// i.e.posted as `Unhandled` messages on the event stream
/// </param>
/// <param name="extractShardId">
/// Function to determine the shard id for an incoming message, only messages that passed the `extractEntityId` will be used
/// </param>
/// <typeparam name="TKey">The type key to use to retrieve the <see cref="IActorRef"/> for this <see cref="ShardRegion"/>.</typeparam>
/// <returns>The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.</returns>
public static AkkaConfigurationBuilder WithShardRegionProxy<TKey>(this AkkaConfigurationBuilder builder,
string typeName, string roleName, ExtractEntityId extractEntityId, ExtractShardId extractShardId)
{
return builder.WithActors(async (system, registry) =>
{
var shardRegionProxy = await ClusterSharding.Get(system)
.StartProxyAsync(typeName, roleName, extractEntityId, extractShardId);
registry.TryRegister<TKey>(shardRegionProxy);
});
}

/// <summary>
/// Starts a ShardRegionProxy that points to a <see cref="ShardRegion"/> hosted on a different role inside the cluster
/// and registers the <see cref="IActorRef"/> with <see cref="TKey"/> in the
/// <see cref="ActorRegistry"/> for this <see cref="ActorSystem"/>.
/// </summary>
/// <param name="builder">The builder instance being configured.</param>
/// <param name="typeName">The name of the entity type</param>
/// <param name="roleName">The role of the Akka.Cluster member that is hosting this <see cref="ShardRegion"/>.</param>
/// <param name="messageExtractor">
/// Functions to extract the entity id, shard id, and the message to send to the entity from the incoming message.
/// </param>
/// <typeparam name="TKey">The type key to use to retrieve the <see cref="IActorRef"/> for this <see cref="ShardRegion"/>.</typeparam>
/// <returns>The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.</returns>
public static AkkaConfigurationBuilder WithShardRegionProxy<TKey>(this AkkaConfigurationBuilder builder,
string typeName, string roleName, IMessageExtractor messageExtractor)
{
return builder.WithActors(async (system, registry) =>
{
var shardRegionProxy = await ClusterSharding.Get(system)
.StartProxyAsync(typeName, roleName, messageExtractor);
registry.TryRegister<TKey>(shardRegionProxy);
});
}
}
}

0 comments on commit 253cf8e

Please sign in to comment.