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

Avoid caching nobody when actor doesn't exist in registry #501

Merged
merged 1 commit into from
Oct 10, 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
33 changes: 33 additions & 0 deletions src/Akka.Hosting.Tests/RequiredActorSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,37 @@ public async Task ShouldFailRetrieveRequiredActorWhenNotDefined()
// assert
shouldThrow.Should().Throw<MissingActorRegistryEntryException>();
}

[Fact]
public async Task ShouldNotCacheNobodyAfterWhenWaitedForRegistration()
{
// arrange
using var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddAkka("MySys", (builder, _) =>
{
builder.WithActors((system, registry) =>
{
var actor = system.ActorOf(Props.Create(() => new MyActorType()), "myactor");
registry.Register<MyActorType>(actor);
});
});
})
.Build();

var myRequiredActor = host.Services.GetRequiredService<IRequiredActor<MyActorType>>();

var task = myRequiredActor.GetAsync();
task.IsCompletedSuccessfully.Should().BeFalse();

await host.StartAsync();
_ = await task;

// act
var cachedActorRef = await myRequiredActor.GetAsync();

// assert
cachedActorRef.Should().NotBeOfType<Nobody>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}
}
6 changes: 3 additions & 3 deletions src/Akka.Hosting/ActorRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public async Task<IActorRef> GetAsync(CancellationToken cancellationToken = defa
return _internalRef;

// attempt 2 - synchronously check the registry (fast path)
if (_registry.TryGet<TActor>(out _internalRef))
if (_registry.TryGet<TActor>(out var internalRef))
{
return _internalRef;
return _internalRef = internalRef;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - good catch

}

// attempt 3 - wait for the actor to be registered
return await _registry.GetAsync<TActor>(cancellationToken).ConfigureAwait(false);
return _internalRef = await _registry.GetAsync<TActor>(cancellationToken).ConfigureAwait(false);
}
}

Expand Down
Loading