scoop:scoop.http.asyncio:0.1.0
src/owner_start.scoop
Selected production source · src/owner_start.scoop
1
package scoop.http.asyncio3
import scoop.concurrent.*5
// The caller owns allocated state until the child either adopts it or cannot start anymore.6
internal suspend fun awaitHttpOwnerStart(caller: Job, start: (Channel<Unit>) -> Job) {7
val ready = Channel<Unit>(1L)8
val task = start(ready)9
val stopped = task.invokeOnCancellation { _ -> ready.close() }10
val cancelled = caller.invokeOnCancellation { _ -> task.cancel() }11
try {12
nonCancellableScope { ready.receive() }13
} catch (error: ChannelClosedException) {14
caller.ensureActive()15
task.ensureActive()16
throw IllegalStateException(Some("HTTP owner ended before initialization"))17
} finally {18
cancelled.dispose()19
stopped.dispose()20
ready.close()21
}22
}