Skip to content

[actor] Introduce module - #1107

Merged
hearnadam merged 15 commits into
mainfrom
actor
Mar 27, 2025
Merged

hearnadam merged 15 commits into
mainfrom
actor

Conversation

@fwbrasil

@fwbrasil fwbrasil commented Mar 24, 2025 •

Copy link
Copy Markdown
Collaborator

Problem

Dealing with effects that can't be directly forked like a local Var isn't trivial and, in some cases, Isolates can't even be provided due to the specific semantics of effects. For example, I'm working on a Browser effect that uses playwright to interact with a browser. Given that playwright isn't thread-safe, providing an Isolate would require a more advanced mechanism like forking multiple playwright engines, which doesn't seem desirable.

Solution

Actors provide a straightforward way to isolate a computation with arbitrary effects and state. Inputs can be serially consumed via Poll while keeping the complete state of the computation, including its effect handlers.

Stream provides a similar solution via fold but the execution model isn't very flexible. Unlike streaming, Actor references can be freely passed around and receive messages in multiple code paths. The implementation also provides supervision hierarchy, which is a more convenient way to handle resources.

Notes

This is based on the collaboration with @DamianReeves for Functional Scala last year. Thank you!

tag: Tag[Poll[V]],
frame: Frame
): (Maybe[V] => A < (Poll[V] & S)) < S =
): Either[A, Maybe[V] => A < (Poll[V] & S)] < S =

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The API was hiding the information if the poll had finished or not

val defaultCapacity =
import AllowUnsafe.embrace.danger
given Frame = Frame.internal
IO.Unsafe.evalOrThrow(System.property[Int]("kyo.actor.capacity.default", 100))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How about a power of 2?

Comment on lines +328 to +329
// Create an unbounded channel to serve as the actor's mailbox
// Unbounded capacity in this initial prototype

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this still true?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

no, I've removed it

* @tparam B
* The type of result this actor produces upon completion
*/
abstract class Actor[+E, A, B]:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

sealed?

* @return
* The actor's final result of type B
*/
def result(using Frame): B < (Async & Abort[Closed | E]) = fiber.get

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe this should be await?

*/
def init[E, A](promise: Promise[E, A])(using frame: Frame): Subject[A] =
def tryComplete(r: A) =
Abort.whenNot(promise.complete(Result.succeed(r)))(Closed("Subject", frame))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've updated Abort.when to allow effects in any of its parameters. Not allowing effects in the condition makes the API not very useful. I've also added Abort.whenNot.

* @return
* A unit computation that may fail if the condition is false
*/
inline def whenNot[E, S](b: Boolean < S)(inline value: => E < S)(using inline frame: Frame): Unit < (Abort[E] & S) =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How about calling this unless?

for
mailbox <-
// Create a bounded channel to serve as the actor's mailbox
Channel.init[A](Int.MaxValue, Access.MultiProducerSingleConsumer)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Channel.init[A](Int.MaxValue, Access.MultiProducerSingleConsumer)
Channel.init[A](capacity, Access.MultiProducerSingleConsumer)

* @return
* A Subject[A] that can be used to send messages to this actor
*/
val subject: Subject[A]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Any reason these are val in the class definition?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It was a lazy way to allow export subject.* so the methods are available in the actor instance directly. I've changed it to use constructor params + avoid val.

@fwbrasil

Copy link
Copy Markdown
Collaborator Author

@hearnadam can we merge this?

@hearnadam
hearnadam merged commit d5f385b into main Mar 27, 2025
@hearnadam
hearnadam deleted the actor branch March 27, 2025 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants