Skip to content

Commit ac21ad2

Browse files
authored
fix: 1. Reset connectionId for coordinator socket when connection is disconnected (#1582)
2. Coordinator socket will execute its task serially
1 parent 6d57e26 commit ac21ad2

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/socket/common/scope/UserScope.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import kotlinx.coroutines.CoroutineScope
2323
import kotlinx.coroutines.Job
2424
import kotlinx.coroutines.job
2525
import kotlinx.coroutines.plus
26+
import kotlin.coroutines.CoroutineContext
27+
import kotlin.coroutines.EmptyCoroutineContext
2628

2729
/**
2830
* A user aware implementation of [CoroutineScope].
@@ -38,22 +40,29 @@ internal interface UserScope : CoroutineScope {
3840
* Cancels all children of the [UserJob] in this scope's context connected to the specified [userId].
3941
*/
4042
fun cancelChildren(userId: UserId? = null)
43+
44+
operator fun plus(context: CoroutineContext): UserScope
4145
}
4246

4347
/**
4448
* Creates a user aware [CoroutineScope].
4549
*/
46-
internal fun UserScope(clientScope: ClientScope): UserScope = UserScopeImpl(clientScope)
50+
internal fun UserScope(
51+
clientScope: ClientScope = ClientScope(),
52+
context: CoroutineContext = EmptyCoroutineContext,
53+
): UserScope = UserScopeImpl(clientScope, context)
4754

4855
/**
4956
* Inherits [ClientScope] and adds elements such as [UserIdentifier] and [UserJob].
5057
*/
5158
private class UserScopeImpl(
52-
clientScope: ClientScope,
53-
userIdentifier: UserIdentifier = UserIdentifier(),
59+
val clientScope: ClientScope,
60+
additionalContext: CoroutineContext,
61+
val userIdentifier: UserIdentifier = UserIdentifier(),
5462
) : UserScope,
5563
CoroutineScope by (
56-
clientScope + userIdentifier + UserJob(clientScope.coroutineContext.job) { userIdentifier.value }
64+
clientScope + userIdentifier + UserJob(clientScope.coroutineContext.job) { userIdentifier.value } +
65+
additionalContext
5766
) {
5867

5968
/**
@@ -68,4 +77,7 @@ private class UserScopeImpl(
6877
override fun cancelChildren(userId: UserId?) {
6978
(coroutineContext[Job] as UserJob).cancelChildren(userId)
7079
}
80+
81+
override fun plus(context: CoroutineContext): UserScope =
82+
UserScopeImpl(clientScope, this.coroutineContext + context, userIdentifier)
7183
}

stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/socket/coordinator/CoordinatorSocketConnection.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import io.getstream.video.android.model.User
4646
import io.getstream.video.android.model.User.Companion.isAnonymous
4747
import io.getstream.video.android.model.UserToken
4848
import kotlinx.coroutines.CoroutineScope
49+
import kotlinx.coroutines.Dispatchers
4950
import kotlinx.coroutines.channels.BufferOverflow
5051
import kotlinx.coroutines.flow.Flow
5152
import kotlinx.coroutines.flow.MutableSharedFlow
@@ -109,7 +110,7 @@ public open class CoordinatorSocketConnection(
109110
parser,
110111
httpClient,
111112
),
112-
scope as? UserScope ?: UserScope(ClientScope()),
113+
scope as? UserScope ?: UserScope(ClientScope(), Dispatchers.IO.limitedParallelism(1)),
113114
StreamLifecycleObserver(scope, lifecycle),
114115
networkStateProvider,
115116
).also {
@@ -155,6 +156,7 @@ public open class CoordinatorSocketConnection(
155156

156157
override fun onConnecting() {
157158
super.onConnecting()
159+
connectionId.value = null
158160
logger.d { "[onConnecting] Socket is connecting" }
159161
}
160162

@@ -186,6 +188,7 @@ public open class CoordinatorSocketConnection(
186188

187189
override fun onDisconnected(cause: DisconnectCause) {
188190
super.onDisconnected(cause)
191+
connectionId.value = null
189192
logger.d { "[onDisconnected] Socket disconnected. Cause: $cause" }
190193
}
191194

0 commit comments

Comments
 (0)