Unverified Commit 1130fd04 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito Committed by GitHub

Merge pull request #2121 from RocketChat/revert-2013-enhancement/coroutine-yield

[FIX][CI] Revert "[REFACTOR] Coroutines yield"
parents c452c7bb 14379551
...@@ -22,11 +22,14 @@ import com.shopify.livedataktx.map ...@@ -22,11 +22,14 @@ import com.shopify.livedataktx.map
import com.shopify.livedataktx.nonNull import com.shopify.livedataktx.nonNull
import kotlinx.coroutines.experimental.android.UI import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.delay import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.isActive
import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.newSingleThreadContext import kotlinx.coroutines.experimental.newSingleThreadContext
import kotlinx.coroutines.experimental.withContext import kotlinx.coroutines.experimental.withContext
import kotlinx.coroutines.experimental.yield
import timber.log.Timber import timber.log.Timber
import java.lang.IllegalArgumentException
import kotlin.coroutines.experimental.coroutineContext
class ChatRoomsViewModel( class ChatRoomsViewModel(
private val connectionManager: ConnectionManager, private val connectionManager: ConnectionManager,
...@@ -50,11 +53,18 @@ class ChatRoomsViewModel( ...@@ -50,11 +53,18 @@ class ChatRoomsViewModel(
// debounce, to not query while the user is writing // debounce, to not query while the user is writing
delay(200) delay(200)
// TODO - find a better way for cancellation checking
if (!coroutineContext.isActive) return@wrap
val rooms = repository.search(string).let { mapper.map(it, showLastMessage = this.showLastMessage) } val rooms = repository.search(string).let { mapper.map(it, showLastMessage = this.showLastMessage) }
data.postValue(rooms.toMutableList() + LoadingItemHolder()) data.postValue(rooms.toMutableList() + LoadingItemHolder())
yield()
if (!coroutineContext.isActive) return@wrap
val spotlight = spotlight(query.query)?.let { mapper.map(it, showLastMessage = this.showLastMessage) } val spotlight = spotlight(query.query)?.let { mapper.map(it, showLastMessage = this.showLastMessage) }
yield() if (!coroutineContext.isActive) return@wrap
spotlight?.let { spotlight?.let {
data.postValue(rooms.toMutableList() + spotlight) data.postValue(rooms.toMutableList() + spotlight)
}.ifNull { }.ifNull {
......
...@@ -295,7 +295,7 @@ class ConnectionManager( ...@@ -295,7 +295,7 @@ class ConnectionManager(
val batch = ArrayList<T>(maxSize) val batch = ArrayList<T>(maxSize)
var deadline = 0L // deadline for sending this batch to callback block var deadline = 0L // deadline for sending this batch to callback block
while(isActive) { while(true) {
// when deadline is reached or size is exceeded, pass the batch to the callback block // when deadline is reached or size is exceeded, pass the batch to the callback block
val remainingTime = deadline - System.currentTimeMillis() val remainingTime = deadline - System.currentTimeMillis()
if (batch.isNotEmpty() && remainingTime <= 0 || batch.size >= maxSize) { if (batch.isNotEmpty() && remainingTime <= 0 || batch.size >= maxSize) {
...@@ -317,6 +317,8 @@ class ConnectionManager( ...@@ -317,6 +317,8 @@ class ConnectionManager(
// when timeout is reached just finish select, note: no timeout when batch is empty // when timeout is reached just finish select, note: no timeout when batch is empty
if (batch.isNotEmpty()) onTimeout(remainingTime.orZero()) {} if (batch.isNotEmpty()) onTimeout(remainingTime.orZero()) {}
} }
if (!isActive) break
} }
} }
} }
......
...@@ -5,7 +5,6 @@ import chat.rocket.common.RocketChatNetworkErrorException ...@@ -5,7 +5,6 @@ import chat.rocket.common.RocketChatNetworkErrorException
import kotlinx.coroutines.experimental.TimeoutCancellationException import kotlinx.coroutines.experimental.TimeoutCancellationException
import kotlinx.coroutines.experimental.delay import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.isActive import kotlinx.coroutines.experimental.isActive
import kotlinx.coroutines.experimental.yield
import timber.log.Timber import timber.log.Timber
import kotlin.coroutines.experimental.coroutineContext import kotlin.coroutines.experimental.coroutineContext
...@@ -22,17 +21,20 @@ suspend fun <T> retryIO( ...@@ -22,17 +21,20 @@ suspend fun <T> retryIO(
{ {
var currentDelay = initialDelay var currentDelay = initialDelay
repeat(times - 1) { currentTry -> repeat(times - 1) { currentTry ->
yield() if (!coroutineContext.isActive) throw TimeoutCancellationException("job canceled")
try { try {
return block() return block()
} catch (e: RocketChatNetworkErrorException) { } catch (e: RocketChatNetworkErrorException) {
Timber.d(e, "failed call($currentTry): $description") Timber.d(e, "failed call($currentTry): $description")
e.printStackTrace() e.printStackTrace()
} }
if (!coroutineContext.isActive) throw TimeoutCancellationException("job canceled")
delay(currentDelay) delay(currentDelay)
currentDelay = (currentDelay * factor).toLong().coerceAtMost(maxDelay) currentDelay = (currentDelay * factor).toLong().coerceAtMost(maxDelay)
} }
yield()
if (!coroutineContext.isActive) throw TimeoutCancellationException("job canceled")
return block() // last attempt return block() // last attempt
} }
...@@ -46,15 +48,19 @@ suspend fun <T> retryDB( ...@@ -46,15 +48,19 @@ suspend fun <T> retryDB(
{ {
var currentDelay = initialDelay var currentDelay = initialDelay
repeat(times - 1) { currentTry -> repeat(times - 1) { currentTry ->
if (!coroutineContext.isActive) throw TimeoutCancellationException("job canceled")
try { try {
return block() return block()
} catch (e: SQLiteDatabaseLockedException) { } catch (e: SQLiteDatabaseLockedException) {
Timber.d(e, "failed call($currentTry): $description") Timber.d(e, "failed call($currentTry): $description")
e.printStackTrace() e.printStackTrace()
} }
if (!coroutineContext.isActive) throw TimeoutCancellationException("job canceled")
delay(currentDelay) delay(currentDelay)
currentDelay = (currentDelay * factor).toLong().coerceAtMost(maxDelay) currentDelay = (currentDelay * factor).toLong().coerceAtMost(maxDelay)
} }
yield()
if (!coroutineContext.isActive) throw TimeoutCancellationException("job canceled")
return block() // last attempt return block() // last attempt
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment