-
Notifications
You must be signed in to change notification settings - Fork 1
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
[CHA-RL2][ECO-5010] Room DETACH with retry #44
base: rename/room_and_connection_status
Are you sure you want to change the base?
[CHA-RL2][ECO-5010] Room DETACH with retry #44
Conversation
WalkthroughThe pull request introduces significant changes to the Changes
Assessment against linked issues
Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
8c96226
to
774cc3b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (2)
chat-android/src/main/java/com/ably/chat/RoomLifecycleManager.kt (2)
558-560
: Use a predefined constant for undefined error codesThe condition
err.errorInfo?.code != -1
uses a hardcoded value-1
, which can be unclear and error-prone. Consider defining a constant for undefined error codes to improve readability and maintainability.Suggestion: Define a constant in
ErrorCodes
:object ErrorCodes { const val UndefinedErrorCode = -1 // Existing error codes... }Then update the condition:
if (err is AblyException && err.errorInfo?.code != ErrorCodes.UndefinedErrorCode && channelFailedError == null) { channelFailedError = err }
505-511
: Consolidate repetitive exception handling into a helper functionThe exception handling for different room statuses (
Released
,Releasing
,Failed
) is repetitive. Refactoring this into a helper function will reduce code duplication and enhance maintainability.Suggestion: Create a helper function to handle these checks:
private fun checkInvalidStates(vararg invalidStates: RoomStatus) { if (_statusLifecycle.status in invalidStates) { val errorCode = when (_statusLifecycle.status) { RoomStatus.Released -> ErrorCodes.RoomIsReleased.errorCode RoomStatus.Releasing -> ErrorCodes.RoomIsReleasing.errorCode RoomStatus.Failed -> ErrorCodes.RoomInFailedState.errorCode else -> ErrorCodes.RoomLifecycleError.errorCode } val errorMessage = "unable to detach room; room is ${_statusLifecycle.status.name.lowercase()}" throw AblyException.fromErrorInfo( ErrorInfo( errorMessage, HttpStatusCodes.InternalServerError, errorCode, ), ) } }Then, replace the repetitive checks in
detach()
:// Before detachment logic -check for RoomStatus.Released -check for RoomStatus.Releasing -check for RoomStatus.Failed +checkInvalidStates(RoomStatus.Released, RoomStatus.Releasing, RoomStatus.Failed)Also applies to: 516-522, 527-533
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
chat-android/src/main/java/com/ably/chat/RoomLifecycleManager.kt
(1 hunks)chat-android/src/main/java/com/ably/chat/RoomStatus.kt
(1 hunks)
🔇 Additional comments (3)
chat-android/src/main/java/com/ably/chat/RoomStatus.kt (2)
212-213
: LGTM! Clean delegation implementation.
The delegation to the new internal method maintains backward compatibility while enabling the refactoring.
215-220
: 🛠️ Refactor suggestion
Consider adding thread safety and state transition validation.
The implementation has two potential areas for improvement:
- Thread Safety: The state updates (
_status
and_error
) are not synchronized, which could lead to race conditions in concurrent scenarios. - State Transition Validation: The method accepts any status without validating if the transition is valid according to the room lifecycle state machine.
Consider applying these improvements:
internal fun setStatus(status: RoomStatus, error: ErrorInfo? = null) {
+ synchronized(this) {
+ // Validate state transition
+ if (!isValidTransition(_status, status)) {
+ throw IllegalStateException("Invalid state transition from ${_status} to ${status}")
+ }
+
val change = RoomStatusChange(status, _status, error)
_status = change.current
_error = change.error
+ }
internalEmitter.emit(change.current, change)
externalEmitter.emit(change.current, change)
}
+ private fun isValidTransition(from: RoomStatus, to: RoomStatus): Boolean {
+ return when (from) {
+ RoomStatus.Initializing -> to == RoomStatus.Initialized
+ RoomStatus.Initialized -> to == RoomStatus.Attaching
+ RoomStatus.Attaching -> setOf(RoomStatus.Attached, RoomStatus.Failed).contains(to)
+ RoomStatus.Attached -> setOf(RoomStatus.Detaching, RoomStatus.Failed, RoomStatus.Suspended).contains(to)
+ RoomStatus.Detaching -> setOf(RoomStatus.Detached, RoomStatus.Failed).contains(to)
+ RoomStatus.Detached -> setOf(RoomStatus.Attaching, RoomStatus.Failed, RoomStatus.Released).contains(to)
+ RoomStatus.Suspended -> setOf(RoomStatus.Attaching, RoomStatus.Failed).contains(to)
+ RoomStatus.Failed -> setOf(RoomStatus.Attaching, RoomStatus.Released).contains(to)
+ RoomStatus.Releasing -> setOf(RoomStatus.Released).contains(to)
+ RoomStatus.Released -> false // Terminal state
+ }
+ }
Let's verify the state transition rules by checking the specifications:
chat-android/src/main/java/com/ably/chat/RoomLifecycleManager.kt (1)
496-546
: LGTM! The detach()
method handles room status transitions correctly
The implementation of the detach()
method appropriately checks the room's status before proceeding with detachment, ensuring that detachment is only attempted when the room is in a valid state. The method also sets the status to Detaching
and calls doDetach()
as expected.
private suspend fun doDetach() { | ||
var channelWindDown = kotlin.runCatching { doChannelWindDown() } | ||
var channelFailedError: AblyException? = null | ||
while (channelWindDown.isFailure) { | ||
val err = channelWindDown.exceptionOrNull() | ||
if (err is AblyException && err.errorInfo?.code != -1 && channelFailedError == null) { | ||
channelFailedError = err | ||
} | ||
delay(_retryDurationInMs) | ||
channelWindDown = kotlin.runCatching { doChannelWindDown() } | ||
} | ||
|
||
// If we aren't in the failed state, then we're detached | ||
if (_statusLifecycle.status !== RoomStatus.Failed) { | ||
_statusLifecycle.setStatus(RoomStatus.Detached) | ||
return | ||
} | ||
|
||
// If we're in the failed state, then we need to throw the error | ||
throw channelFailedError | ||
?: AblyException.fromErrorInfo( | ||
ErrorInfo( | ||
"unknown error in _doDetach", | ||
HttpStatusCodes.InternalServerError, | ||
ErrorCodes.RoomLifecycleError.errorCode, | ||
), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent potential infinite loop in doDetach()
In the doDetach()
method, there's a possibility of an infinite loop if doChannelWindDown()
continues to fail indefinitely. This could lead to resource exhaustion or unresponsive behavior.
Suggestion: Introduce a maximum retry limit to prevent an infinite loop. Here's how you can modify the code:
private suspend fun doDetach() {
var channelWindDown = kotlin.runCatching { doChannelWindDown() }
var channelFailedError: AblyException? = null
+ var retryCount = 0
+ val maxRetries = 5
while (channelWindDown.isFailure && retryCount < maxRetries) {
val err = channelWindDown.exceptionOrNull()
if (err is AblyException && err.errorInfo?.code != -1 && channelFailedError == null) {
channelFailedError = err
}
delay(_retryDurationInMs)
channelWindDown = kotlin.runCatching { doChannelWindDown() }
+ retryCount++
}
// Rest of the code remains the same
}
This ensures that after a certain number of retries, the method will throw an exception and prevent an endless loop.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
private suspend fun doDetach() { | |
var channelWindDown = kotlin.runCatching { doChannelWindDown() } | |
var channelFailedError: AblyException? = null | |
while (channelWindDown.isFailure) { | |
val err = channelWindDown.exceptionOrNull() | |
if (err is AblyException && err.errorInfo?.code != -1 && channelFailedError == null) { | |
channelFailedError = err | |
} | |
delay(_retryDurationInMs) | |
channelWindDown = kotlin.runCatching { doChannelWindDown() } | |
} | |
// If we aren't in the failed state, then we're detached | |
if (_statusLifecycle.status !== RoomStatus.Failed) { | |
_statusLifecycle.setStatus(RoomStatus.Detached) | |
return | |
} | |
// If we're in the failed state, then we need to throw the error | |
throw channelFailedError | |
?: AblyException.fromErrorInfo( | |
ErrorInfo( | |
"unknown error in _doDetach", | |
HttpStatusCodes.InternalServerError, | |
ErrorCodes.RoomLifecycleError.errorCode, | |
), | |
) | |
private suspend fun doDetach() { | |
var channelWindDown = kotlin.runCatching { doChannelWindDown() } | |
var channelFailedError: AblyException? = null | |
var retryCount = 0 | |
val maxRetries = 5 | |
while (channelWindDown.isFailure && retryCount < maxRetries) { | |
val err = channelWindDown.exceptionOrNull() | |
if (err is AblyException && err.errorInfo?.code != -1 && channelFailedError == null) { | |
channelFailedError = err | |
} | |
delay(_retryDurationInMs) | |
channelWindDown = kotlin.runCatching { doChannelWindDown() } | |
retryCount++ | |
} | |
// If we aren't in the failed state, then we're detached | |
if (_statusLifecycle.status !== RoomStatus.Failed) { | |
_statusLifecycle.setStatus(RoomStatus.Detached) | |
return | |
} | |
// If we're in the failed state, then we need to throw the error | |
throw channelFailedError | |
?: AblyException.fromErrorInfo( | |
ErrorInfo( | |
"unknown error in _doDetach", | |
HttpStatusCodes.InternalServerError, | |
ErrorCodes.RoomLifecycleError.errorCode, | |
), | |
) |
Fixed #22
Summary by CodeRabbit
New Features
Bug Fixes
Documentation