https://kotlinlang.org logo
Title
t

taer

04/23/2020, 11:11 PM
Scope question wrt channels and a "servlet style" setup.. We have a system that creates instance of our class(think servlet) and has lifecycles for that instance. In the "do get" method, I create a produce channel(Bar), and then proceed to consume from it. I might swap to flows, but in this case, is this all setup right wrt the structured concurrency? When I close the second instance, the channels seem to shutdown correctly. The only thing that feels like it's missing is a server level context(that would parent the
instanceScope
)
the
spawn
call in the system I'm converting is expected to block untill the work is done. Side question though. What
couroutingScope
is the
this
inside that runBlocking? The
for (x in streamer)
call is a suspending call, so I'm curious what scope is managing that call
a

araqnid

04/23/2020, 11:19 PM
runBlocking with no context parameter will create a new top-level Job iirc
t

taer

04/23/2020, 11:19 PM
yeah.. I validated that just now in debugger.
a

araqnid

04/23/2020, 11:19 PM
It looks like you should do
runBlocking(instanceScope) { … }
uh,
runBlocking(instanceScope.context)
t

taer

04/23/2020, 11:20 PM
is that kosher w/ all the parent things? 🙂 I just read a bit in this channel about messing up the whole heirarchy sometimes.
a

araqnid

04/23/2020, 11:21 PM
although tbh I’d just have an
instanceJob : Job
and pass
context + instanceJob
as the parameter to
runBlocking
t

taer

04/23/2020, 11:22 PM
ah.. so my close() cancels the instanceJob instead
I see it both ways.. "dont manage jobs, that's the scope's job" vs this method
a

araqnid

04/23/2020, 11:22 PM
I think it’s all ok, a coroutine running with a top-level Job by accident is one of the pitfalls though
t

taer

04/23/2020, 11:23 PM
ah that CoroutineScope is just
context + Job()
a

araqnid

04/23/2020, 11:23 PM
so where is the Job getting created at all? I thought
CoroutineScope(..)
didn’t create one, as opposed to
coroutineScope { }
oh it does?
t

taer

04/23/2020, 11:24 PM
ContextScope(if (context[Job] != null) context else context + Job())
The names are blurring now.. CoroutineScope, CoroutineContext.
It's at least workin. I'm gonna have to reread the hierarchy medium article 3 more times. 🙂
1
a

araqnid

04/23/2020, 11:26 PM
ah, it creates a Job only if there wasn’t one already
good luck!
t

taer

04/23/2020, 11:27 PM
thx!