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

Make all workers run on the Unconfined dispatcher. #851

Merged
merged 1 commit into from Jan 8, 2020

Conversation

zach-klippenstein
Copy link
Collaborator

@zach-klippenstein zach-klippenstein commented Dec 24, 2019

From the kdoc:

The dispatcher is always set to Unconfined to

  • minimize overhead for workers that don't care which thread they're executed on (e.g. logging
  • side effects, workers that wrap third-party reactive libraries, etc.). If your work cares
  • which thread it runs on, use withContext or
  • flowOn to specify a dispatcher.

Previously, the workers would run on the same dispatcher as the workflow runtime (Main.immediate), which meant that if the worker was wrapping, e.g., an Observable that was emitting on a background thread, the coroutine would dispatch back onto the main thread before sending to the channel, which is completely unnecessary, since channels are already thread-safe.

This doesn't change the threading behavior for simple side-effect-only workers – since they're started from the workflow runtime, they will be dispatched onto the nested event loop that Main.immediate creates for "immediate" dispatches.

@zach-klippenstein zach-klippenstein added the enhancement New feature or request label Dec 24, 2019
@zach-klippenstein zach-klippenstein added this to the kotlin v0.23.0 milestone Dec 24, 2019
@zach-klippenstein zach-klippenstein added this to Needs review in Workflow (Kotlin) via automation Dec 24, 2019
@zach-klippenstein zach-klippenstein force-pushed the zachklipp/named-worker-coroutines branch 2 times, most recently from 51cb696 to 5467ed0 Compare December 24, 2019 20:05
@zach-klippenstein zach-klippenstein changed the base branch from zachklipp/named-worker-coroutines to master January 8, 2020 17:54
@zach-klippenstein zach-klippenstein merged commit a678c73 into master Jan 8, 2020
Workflow (Kotlin) automation moved this from Needs review to Done Jan 8, 2020
@zach-klippenstein zach-klippenstein deleted the zachklipp/unconfined-workers branch January 8, 2020 22:34
zach-klippenstein added a commit that referenced this pull request Feb 6, 2020
This is helpful for tests that need to be able to control time.

This functionality was covered by the test's dispatcher, but since #851
changed the dispatcher used for workers, we need to be able to configure
workers' contexts separately.
@zach-klippenstein zach-klippenstein added the kotlin Affects the Kotlin library. label Mar 3, 2020
zach-klippenstein added a commit to square/workflow-kotlin that referenced this pull request Jun 20, 2020
First part of Kotlin implementation of square/workflow#1021.

Kotlin counterpart to square/workflow#1174.

This implementation intentionally does not run the side effect coroutine
on the `workerContext` `CoroutineContext` that is threaded through the
runtime for testing infrastructure. Initially, workers ran in the same
context as the workflow runtime. The behavior of running workers on a
different dispatcher by default (`Unconfined`) was introduced in
square/workflow#851 as an optimization to reduce
the overhead for running workers that only perform wiring tasks with
other async libraries. This was a theoretical optimization, since running
on the `Unconfined` dispatcher inherently involves less dispatching work,
but the overhead of dispatching wiring coroutines was never actually shown
to be a problem. Additionally, because tests often need to be in full
control of dispatchers, the ability to override the context used for
workers was introduced in square/workflow#940,
which introduced `workerContext`. I am dropping that complexity here
because it adds a decent amount of complexity to worker/side effect
machinery without any proven value. It is also complexity that needs to
be documented, and is probably just more confusing than anything. The
old behavior for workers is maintained for now to reduce the risk of this
change, but side effects will always run in the workflow runtime's
context. This is nice and simple and unsurprising and easy to reason
about.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request kotlin Affects the Kotlin library.
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants