-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from ably-labs/feature/room-lifecycle-manager
[ECO-4953][Integration] Implement RoomLifecycleManager + Async Rooms.get and Rooms.release
- Loading branch information
Showing
41 changed files
with
4,194 additions
and
340 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
chat-android/src/main/java/com/ably/chat/AtomicCoroutineScope.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.ably.chat | ||
|
||
import java.util.concurrent.PriorityBlockingQueue | ||
import kotlin.coroutines.cancellation.CancellationException | ||
import kotlin.coroutines.coroutineContext | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancelChildren | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* AtomicCoroutineScope is a thread safe wrapper to run multiple operations mutually exclusive. | ||
* All operations are atomic and run with given priority. | ||
* Accepts scope as a constructor parameter to run operations under the given scope. | ||
* See [Kotlin Dispatchers](https://kt.academy/article/cc-dispatchers) for more information. | ||
*/ | ||
class AtomicCoroutineScope(private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default)) { | ||
|
||
private val sequentialScope: CoroutineScope = CoroutineScope(Dispatchers.Default.limitedParallelism(1)) | ||
|
||
private class Job<T : Any>( | ||
private val priority: Int, | ||
val coroutineBlock: suspend CoroutineScope.() -> T, | ||
val deferredResult: CompletableDeferred<T>, | ||
val queuedPriority: Int, | ||
) : Comparable<Job<*>> { | ||
override fun compareTo(other: Job<*>) = when { | ||
this.priority == other.priority -> this.queuedPriority.compareTo(other.queuedPriority) | ||
else -> this.priority.compareTo(other.priority) | ||
} | ||
} | ||
|
||
// Handles jobs of any type | ||
private val jobs: PriorityBlockingQueue<Job<*>> = PriorityBlockingQueue() // Accessed from both sequentialScope and async method | ||
private var isRunning = false // Only accessed from sequentialScope | ||
private var queueCounter = 0 // Only accessed from synchronized method | ||
|
||
val finishedProcessing: Boolean | ||
get() = jobs.isEmpty() && !isRunning | ||
|
||
val pendingJobCount: Int | ||
get() = jobs.size | ||
|
||
/** | ||
* Defines priority for the operation execution and | ||
* executes given coroutineBlock mutually exclusive under given scope. | ||
*/ | ||
@Synchronized | ||
fun <T : Any> async(priority: Int = 0, coroutineBlock: suspend CoroutineScope.() -> T): CompletableDeferred<T> { | ||
val deferredResult = CompletableDeferred<T>() | ||
jobs.add(Job(priority, coroutineBlock, deferredResult, queueCounter++)) | ||
sequentialScope.launch { | ||
if (!isRunning) { | ||
isRunning = true | ||
while (jobs.isNotEmpty()) { | ||
val job = jobs.poll() | ||
job?.let { | ||
safeExecute(it) | ||
} | ||
} | ||
isRunning = false | ||
} | ||
} | ||
return deferredResult | ||
} | ||
|
||
private suspend fun <T : Any> safeExecute(job: Job<T>) { | ||
try { | ||
// Appends coroutineContext to cancel current/pending jobs when AtomicCoroutineScope is cancelled | ||
scope.launch(coroutineContext) { | ||
try { | ||
val result = job.coroutineBlock(this) | ||
job.deferredResult.complete(result) | ||
} catch (t: Throwable) { | ||
job.deferredResult.completeExceptionally(t) | ||
} | ||
}.join() | ||
} catch (t: Throwable) { | ||
job.deferredResult.completeExceptionally(t) | ||
} | ||
} | ||
|
||
/** | ||
* Cancels ongoing and pending operations with given error. | ||
* See [Coroutine cancellation](https://kt.academy/article/cc-cancellation#cancellation-in-a-coroutine-scope) for more information. | ||
*/ | ||
@Synchronized | ||
fun cancel(message: String?, cause: Throwable? = null) { | ||
queueCounter = 0 | ||
sequentialScope.coroutineContext.cancelChildren(CancellationException(message, cause)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 0 additions & 101 deletions
101
chat-android/src/main/java/com/ably/chat/ConnectionStatus.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.