Reputation: 303
I'm new to Kotlin/Coroutines and I've noticed two different ways to use CoroutineScope
.
Option 1 is as follows, within any function:
CoroutineScope(Dispatchers.Default).launch {
expensiveOperation()
}
Option 2 is by implementing the CoroutineScope
interface in your class, overriding the CoroutineContext
, and then you can just launch coroutines easily with launch
or async
:
@Service
class ServiceImpl() : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Default + Job()
fun someFunction() {
launch {
expensiveOperation()
}
}
}
I am currently developing a backend endpoint that will do the following:
What is the difference in the two use cases, and for this scenario, which is the preferred method for obtaining a CoroutineScope
?
This endpoint may receive multiple requests per second, and the lengthy operation will take a minute or two, so there will definitely be multiple requests processing at the same time, originating from various requests.
Also, if it's option 2, do I want to pass the scope/context to the function that does the heavy processing? Or is that unnecessary? For example:
class ServiceImpl() : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Default + Job()
fun someFunction() {
launch {
expensiveOperation(CoroutineScope(coroutineContext))
}
}
private fun expensiveOperation(scope: CoroutineScope)
{
// perform expensive operation
}
}
This is a Spring Boot app, and I'm using version 1.3
of Kotlin
.
Please let me know if you have any thoughts/suggestions on how to best structure this service class. Thanks
Upvotes: 2
Views: 1421
Reputation: 4992
I would recommend option 2. It will give you chance to clearly define parent Job
for all of your coroutines. That gives a chance to shut down the whole execution correctly too.
There are several more coroutine context keys to include - CoroutineName
, CoroutineExceptionHandler
and so one.
Lastly, the structural concurrency may work better if you pass the CoroutineScope
and the associated Job explicitly.
https://medium.com/@elizarov/structured-concurrency-722d765aa952
Also, take a look the explanation on that from Roman: https://medium.com/@elizarov/coroutine-context-and-scope-c8b255d59055
Upvotes: 2