tarant
tarant copied to clipboard
[Feature] Support releasing/freeing actors
Reason / Issue that solves
Right now there is no easy way to free resources of an actor when it's not useful anymore. Releasing actors should allow systems to clean up resources and dematerialise on the given persistence mechanism, if any.
How does it work?

An actor is released by calling the parent actor system with a reference to the actor to release.
system.releaseActor(clock)
The Release Message message will be sent to the actor mailbox, to be processed. This guarantees that all received messages before the release message are processed in order.
Once the message is consumed by the actor, materialisers are notified on onBeforeRelease, so they can prepare any clean up, if necessary.
Once all materialisers are notified (async), the actor system unsubscribes the actor from all mailboxes and topics. Then, the actor notifies for one last time the materialisers on onAfterRelease. For example, if we store the information in the browser local storage or on a database, onAfterRelease would be a good entrypoint to clean up resources.
Released actors that receive messages will discard the message and throw an exception. The exception contains a field isActorReleased that should be true if the cause is the release of an actor. Also contains a getMessage method with a human-readable explanation.
Breaking Changes
Two new methods in the materialisers that are mandatory:
onBeforeRelease(actor: Actor): void
onAfterRelease(actor: Actor): void
@kmruiz we are breaking backwards compatibility with this, right? I am curious if we should pack this as a beta and have a conversation of other breaking changes we want to bring in tarant.
We are if we decide that all materialisers must implement the two new functions. However, we could decide to make them optional and maintain compatibility with older versions.
I agree however having a call and discuss next steps ♥️.
LGTM