Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added AK2001 warning docs #7047

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/articles/debugging/akka-analyzers.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Akka.Analyzer is a [Roslyn Analysis and Code Fix](https://learn.microsoft.com/en
| [AK1000](xref:AK1000) | Do not use `new` to create actors. | Error | Actor Design |
| [AK1001](xref:AK1001) | Should always close over `Sender` when using `PipeTo`. | Error | Actor Design |
| [AK2000](xref:AK2000) | Do not use `Ask` with `TimeSpan.Zero` for timeout. | Error | API Usage |
| [AK2001](xref:AK2001) | Do not use automatically handled messages in inside `Akka.Cluster.Sharding.IMessageExtractor`s. | Warning | API Usage |
61 changes: 61 additions & 0 deletions docs/articles/debugging/rules/AK2001.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
uid: AK2001
title: Akka.Analyzers Rule AK2001 - "Do not use automatically handled messages in inside `Akka.Cluster.Sharding.IMessageExtractor`s."
---

# AK2000 - Warning

Do not use automatically handled messages in inside [`Akka.Cluster.Sharding.IMessageExtractor`](xref:Akka.Cluster.Sharding.IMessageExtractor)s.

## Cause

As of Akka.NET v1.5.15, [Akka.Cluster.Sharding is guaranteed to automatically handle the following built-in messages](https://github.com/akkadotnet/akka.net/pull/6863):

* [`ShardRegion.StartEntity`](xref:Akka.Cluster.Sharding.ShardRegion.StartEntity) - used whenever [Akka.Cluster.Sharding's `remember-entities` feature](xref:cluster-sharding#remembering-entities) is enabled.
* [`ShardingEnvelope`](xref:Akka.Cluster.Sharding.ShardingEnvelope) - a generic envelope type that can be used to send arbitrary messages to entity actors.

Whenever a user tries to manually handle either of these messages, they're performing duplicate work - this rule is in effect to spot wasteful situations and to automatically provide a Roslyn code fix to resolve them.

An example:

```csharp
using Akka.Cluster.Sharding;

public sealed class MessageExtractor : HashCodeMessageExtractor
{
public MessageExtractor() : base(maxNumberOfShards: 100) { }

public string EntityId(object message)
{
return message switch
{
string sharded => sharded,
ShardingEnvelope e => e.EntityId,
ShardRegion.StartEntity start => start.EntityId,
_ => null,
};
}
}
```

## Resolution

Akka.Analyzers comes with a code fix for this issue, which if accepted by the end user, will rewrite the previous example to the following:

```csharp
using Akka.Cluster.Sharding;

public sealed class MessageExtractor : HashCodeMessageExtractor
{
public MessageExtractor() : base(maxNumberOfShards: 100) { }

public string EntityId(object message)
{
return message switch
{
string sharded => sharded,
_ => null,
};
}
}
```
4 changes: 3 additions & 1 deletion docs/articles/debugging/rules/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
- name: AK1001
href: AK1001.md
- name: AK2000
href: AK2000.md
href: AK2000.md
- name: AK2001
href: AK2001.md
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ If you want to see the [full set of changes made in Akka.NET v1.5.14, click here
| 1 | 1 | 1 | szaliszali |</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup Label="Analyzers">
<PackageReference Include="Akka.Analyzers" Version="0.1.2" PrivateAssets="all" />
<PackageReference Include="Akka.Analyzers" Version="0.2.0" PrivateAssets="all" />
</ItemGroup>
<!-- SourceLink support for all Akka.NET projects -->
<ItemGroup>
Expand Down
Loading