-
Notifications
You must be signed in to change notification settings - Fork 58
Single-flight Call.join to stop concurrent-join race #1764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PratimMallick
wants to merge
3
commits into
develop
Choose a base branch
from
fix/join-single-flight
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
149 changes: 149 additions & 0 deletions
149
...ain/kotlin/io/getstream/video/android/core/utils/StreamRefCountedSingleFlightProcessor.kt
This file contains hidden or 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,149 @@ | ||
| /* | ||
| * Copyright (c) 2014-2026 Stream.io Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Stream License; | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://github.com/GetStream/stream-video-android/blob/main/LICENSE | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.getstream.video.android.core.utils | ||
|
|
||
| // package io.getstream.android.core.internal.processing | ||
|
|
||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Deferred | ||
| import kotlinx.coroutines.NonCancellable | ||
| import kotlinx.coroutines.async | ||
| import kotlinx.coroutines.channels.ClosedSendChannelException | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.sync.withLock | ||
| import kotlinx.coroutines.withContext | ||
| import java.util.concurrent.atomic.AtomicBoolean | ||
|
|
||
| /** | ||
| * Single-flight that coalesces concurrent calls by key, runs the shared work on [scope], | ||
| * and tracks how many callers are still awaiting. | ||
| * | ||
| * Compared to [StreamSingleFlightProcessorImpl]: | ||
| * - Cancelling **one** waiter does **not** cancel the shared job (work is owned by [scope]). | ||
| * - Cancelling the **last** waiter **does** cancel the shared job (sole-caller cancel still | ||
| * aborts the operation). | ||
| * - [CancellationException] is rethrown to the cancelled waiter instead of being wrapped in | ||
| * [Result.failure]. | ||
| * | ||
| * Use this when the operation should survive Activity/ViewModel teardown of some waiters | ||
| * (e.g. screen handoff, UI join + call-scoped auto-join) but should not keep running after | ||
| * nobody is waiting — unless [scope] itself is cancelled (leave / call cleanup). | ||
| * | ||
| * Candidate for Stream Android Core v2 alongside [StreamSingleFlightProcessorImpl]. | ||
| */ | ||
| internal class StreamRefCountedSingleFlightProcessor( | ||
| private val scope: CoroutineScope, | ||
| ) { | ||
| private class Flight<T>( | ||
| val deferred: Deferred<Result<T>>, | ||
| var waiters: Int, | ||
| ) | ||
|
|
||
| private val mutex = Mutex() | ||
| private val flights = mutableMapOf<String, Flight<*>>() | ||
| private val closed = AtomicBoolean(false) | ||
|
|
||
| /** | ||
| * Runs [block] once for [key] while concurrent callers await the same result. | ||
| * | ||
| * Returns [Result.failure] with a [ClosedSendChannelException] if [stop] has already | ||
| * been called. [CancellationException] is still rethrown when this waiter (or the shared | ||
| * job, including last-waiter cancel) is cancelled. | ||
| */ | ||
| @Suppress("UNCHECKED_CAST") | ||
| suspend fun <T> run(key: String, block: suspend () -> T): Result<T> { | ||
|
Check failure on line 69 in stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/utils/StreamRefCountedSingleFlightProcessor.kt
|
||
| if (closed.get()) { | ||
| return Result.failure(ClosedSendChannelException("RefCountedSingleFlight is closed")) | ||
| } | ||
|
|
||
| val flight = mutex.withLock { | ||
| val running = flights[key]?.takeUnless { it.deferred.isCompleted } as Flight<T>? | ||
| if (running != null) { | ||
| running.waiters++ | ||
| running | ||
| } else { | ||
| val deferred = scope.async { | ||
| try { | ||
| // Complete normally even when [block] fails so the scope does not see an | ||
| // uncaught child exception; waiters receive Result.failure after await. | ||
| try { | ||
| Result.success(block()) | ||
| } catch (ce: CancellationException) { | ||
| throw ce | ||
| } catch (t: Throwable) { | ||
| Result.failure(t) | ||
| } | ||
| } finally { | ||
| mutex.withLock { | ||
| if (flights[key]?.deferred === this@async) { | ||
| flights.remove(key) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Flight(deferred = deferred, waiters = 1).also { flights[key] = it } | ||
| } | ||
| } | ||
|
|
||
| var released = false | ||
| suspend fun releaseWaiter(cancelIfLast: Boolean) { | ||
| if (released) return | ||
| released = true | ||
| val shouldCancelShared = mutex.withLock { | ||
| flight.waiters = (flight.waiters - 1).coerceAtLeast(0) | ||
| cancelIfLast && flight.waiters == 0 && flight.deferred.isActive | ||
| } | ||
| if (shouldCancelShared) { | ||
| flight.deferred.cancel() | ||
| } | ||
| } | ||
|
|
||
| return try { | ||
| flight.deferred.await() | ||
| } catch (ce: CancellationException) { | ||
| // NonCancellable: waiter bookkeeping must run while this coroutine is cancelling. | ||
| withContext(NonCancellable) { | ||
| releaseWaiter(cancelIfLast = true) | ||
| } | ||
| throw ce | ||
| } finally { | ||
| withContext(NonCancellable) { | ||
| releaseWaiter(cancelIfLast = false) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun has(key: String): Boolean = flights.containsKey(key) | ||
|
|
||
| fun cancel(key: String): Result<Unit> = runCatching { | ||
| flights[key]?.deferred?.cancel() | ||
| } | ||
|
|
||
| fun clear(cancelRunning: Boolean): Result<Unit> = runCatching { | ||
| if (cancelRunning) { | ||
| flights.values.forEach { it.deferred.cancel() } | ||
| } | ||
| flights.clear() | ||
| } | ||
|
|
||
| fun stop(): Result<Unit> = runCatching { | ||
| if (closed.compareAndSet(false, true)) { | ||
| clear(cancelRunning = true).getOrThrow() | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.