Skip to content

Conversation

@dfed
Copy link
Owner

@dfed dfed commented Dec 11, 2022

This PR is built on #3

This PR is similar to #6 but applied to the ActorQueue. However, rather than adding API, this PR replaces existing async and await API with methods where the tasks sent to the queue to execute on the designated actor's context.

The goals of this PR are:

  1. Enable multiple calls to the sam actor within a single task without requiring multiple suspension points (await calls) within the `task
  2. Ensure that await calls within a single task actually result in suspension

These goals are important because an ActorQueue's task allows other tasks to begin execution as soon as the task suspends (via an await). Since ActorQueues should only be utilized with a single actor instance, it made sense to replace the existing API rather than add new API – there's no good reason (that I can come up with) to use the the replaced APIs now that these new APIs exist.

All that said, I don't love this API change. ActorQueues are meant to be used as private implementation details of an actor, and as such the isolatedActor that will be sent to the ActorQueue's API is almost always going to be self. The big drawback of this change is that Swift doesn't allow parameters to a closure to be named self, so ActorQueue calls will likely end up looking like the following:

queue.async(on: self) { myself in
    myself.doSomething()
}

If someone were to try to ignore the parameter in the closure, they'd end up having to write an await:

queue.async(on: self) { _ in
    // This `task` is isolated to the parameter, which Swift doesn't know is the same as `self`.
    // The result is that Swift requires us to add an `await` here even though we're executing within `self`'s context.
    await self.doSomething()
}

I really like the goals I'm trying to achieve, but I don't love how the semantics play out. Curious to get your thoughts!

@dfed dfed force-pushed the dfed--only-isolated-actor-task branch from cf7c00b to 15d770f Compare December 11, 2022 19:09
}
}
await systemUnderTest.await { /* Drain the queue */ }
await systemUnderTest.await(on: counter) { _ in /* Drain the queue */ }
Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm tempted to add a new internal method called func allEnqueuedTasksSuspendedOrCompleted() async to make this test call-site read more clearly since we have a few of these.

@dfed dfed force-pushed the dfed--only-isolated-actor-task branch from 15d770f to 37c441d Compare December 11, 2022 19:13
@dfed dfed force-pushed the dfed--only-isolated-actor-task branch from 37c441d to 261001f Compare December 11, 2022 19:16
@codecov
Copy link

codecov bot commented Dec 11, 2022

Codecov Report

Merging #7 (fa286f8) into dfed--fifo-vs-actor (ea6ade9) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@                  Coverage Diff                  @@
##           dfed--fifo-vs-actor        #7   +/-   ##
=====================================================
  Coverage               100.00%   100.00%           
=====================================================
  Files                        2         2           
  Lines                       95        95           
=====================================================
  Hits                        95        95           
Impacted Files Coverage Δ
Sources/AsyncQueue/ActorQueue.swift 100.00% <100.00%> (ø)

@dfed dfed marked this pull request as ready for review December 11, 2022 19:19
@dfed dfed requested review from bachand and omv-libs December 11, 2022 19:19
@dfed
Copy link
Owner Author

dfed commented Dec 12, 2022

Removing reviewers and bringing this PR back to drafts. Had some ideas I want to play with – I may be able to simplify call-sites make the async for-loop run on the target actor's context.

@dfed dfed removed request for bachand and omv-libs December 12, 2022 17:05
@dfed dfed marked this pull request as draft December 12, 2022 17:05
@dfed
Copy link
Owner Author

dfed commented Dec 13, 2022

Ideally the ActorQueue would run entirely on its target actor's execution context, but that's not possible today given how Tasks and isolated parameters interact today.

I'm going to close this out for now, though I'll want to explore this path more in the future.

@dfed dfed closed this Dec 13, 2022
@dfed dfed deleted the dfed--only-isolated-actor-task branch December 13, 2022 22:21
dfed added a commit that referenced this pull request Feb 12, 2023
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