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

Akka.Streams: when using Context.Materializer, encourage users to cache it into a backing property or field #109

Open
Aaronontheweb opened this issue Dec 10, 2024 · 0 comments
Labels
AK2000 API usage rules

Comments

@Aaronontheweb
Copy link
Member

Here's the issue - using an example from Phobos's test suite:

private sealed class LocalActor: UntypedActor
    {
        private readonly int[] _messages = { 1, 2, 3 };
        private readonly IActorRef _remote;
        private readonly ILoggingAdapter _log;

        public LocalActor(IActorRef remote)
        {
            _remote = remote;
            _log = Context.GetLogger();
        }

        protected override void OnReceive(object message)
        {
            if (message is "start-manual")
            {
                _log.Info("Starting stream");

                // test manual span
                using (CreateSpan("StartStream"))
                {
                    Source.From(_messages)
                        .RunWith(
                            Sink.ActorRef<int>(
                                _remote,
                                onCompleteMessage: "complete",
                                onFailureMessage: e => $"fail: {e}"), 
                            Context.Materializer());
                }
            }
            else if (message is "start")
            {
                _log.Info("Starting stream");

                // no manual span
                Source.From(_messages)
                    .RunWith(
                        Sink.ActorRef<int>(
                            _remote,
                            onCompleteMessage: "complete",
                            onFailureMessage: e => $"fail: {e}"), 
                        Context.Materializer());
            }
            else
            {
                Unhandled(message);
            }
        }

Each time we call Context.Materializer() this operation allocates a new group of stream materialization actors - which is not usually what the user intends. So generally, it's a good idea for performance / memory / stability reasons to encourage users to always cache Context.Materalizer() calls.

Important Distinction

Worth nothing: you do not need to cache ActorSystem.Materializer() calls - these are already cached internally as part of akkadotnet/akka.net#6453

@Aaronontheweb Aaronontheweb added the AK2000 API usage rules label Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AK2000 API usage rules
Projects
None yet
Development

No branches or pull requests

1 participant