Dico
09/09/2019, 3:53 PMprivate val processor = scope.launch {
val batch = ArrayList<String>(MAX_BATCH)
suspend fun sendBatch() {
val payload = batch.joinToString(separator = "\n")
batch.clear()
return sendPayload(payload)
}
try {
while (isActive) {
batch.add(hitChannel.receive())
if (batch.size == MAX_BATCH) {
sendBatch()
}
}
} finally {
if (batch.isNotEmpty()) {
sendBatch()
}
}
}
I expect that final sendBatch
will throw exception because coroutine no longer isActive
. I can't do withContext(NonCancellable)
either at that point.Dominaezzz
09/09/2019, 3:59 PMDico
09/09/2019, 4:00 PMDominaezzz
09/09/2019, 4:00 PMhitChannel.receive()
fails, you want to call sendBatch
?Dominaezzz
09/09/2019, 4:02 PMDico
09/09/2019, 4:03 PMDico
09/09/2019, 4:05 PMreceive()
will fail I thinkDico
09/09/2019, 4:06 PMsendBatch
Dominaezzz
09/09/2019, 4:06 PMDico
09/09/2019, 4:06 PMDominaezzz
09/09/2019, 4:06 PMDico
09/09/2019, 4:07 PMDico
09/09/2019, 4:08 PMDico
09/09/2019, 4:08 PMDominaezzz
09/09/2019, 4:09 PMDominaezzz
09/09/2019, 4:09 PMDominaezzz
09/09/2019, 4:10 PMDominaezzz
09/09/2019, 4:12 PMDico
09/09/2019, 4:13 PMDico
09/09/2019, 4:14 PMprivate val processor = scope.launch {
val batch = ArrayList<String>(MAX_BATCH)
suspend fun sendBatch() {
val payload = batch.joinToString(separator = "\n")
batch.clear()
return sendPayload(payload)
}
for (item in hitChannel) {
batch.add(hitChannel.receive())
if (batch.size == MAX_BATCH) {
sendBatch()
}
}
if (batch.isNotEmpty()) {
sendBatch()
}
scope.cancel()
}
fun stop() {
hitChannel.close()
}
Dominaezzz
09/09/2019, 4:16 PMDico
09/09/2019, 4:17 PMDico
09/09/2019, 4:17 PMDico
09/09/2019, 4:17 PMDico
09/09/2019, 4:18 PMDominaezzz
09/09/2019, 4:18 PMhitChannel
is closed before MAX_BATCH
is attained, then some items won't be sent, even though they've gone through the channel.Dico
09/09/2019, 4:19 PMDico
09/09/2019, 4:19 PMDominaezzz
09/09/2019, 4:19 PMbatch.add(item)
Dico
09/09/2019, 4:19 PMDominaezzz
09/09/2019, 4:20 PMhitChannel.receive()
.Dominaezzz
09/09/2019, 4:20 PMreceive
for you.Dico
09/09/2019, 4:20 PMDico
09/09/2019, 4:20 PMDominaezzz
09/09/2019, 4:21 PM