-
Notifications
You must be signed in to change notification settings - Fork 44
[AIT-934] feat(liveobjects): LiveObjects implementation to new path-based API (Kotlin)
#1223
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
Draft
sacOO7
wants to merge
2
commits into
feature/uts-liveobjects-unit-tests
Choose a base branch
from
feature/liveobjects-kotlin-implementation
base: feature/uts-liveobjects-unit-tests
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.
Draft
Changes from all commits
Commits
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
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
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
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
36 changes: 36 additions & 0 deletions
36
liveobjects/src/main/kotlin/io/ably/lib/liveobjects/DefaultLiveObjectsPlugin.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,36 @@ | ||
| package io.ably.lib.liveobjects | ||
|
|
||
| import io.ably.lib.liveobjects.adapter.AblyClientAdapter | ||
| import io.ably.lib.realtime.ChannelState | ||
| import io.ably.lib.types.ProtocolMessage | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| public class DefaultLiveObjectsPlugin(private val adapter: AblyClientAdapter) : LiveObjectsPlugin { | ||
|
|
||
| private val objects = ConcurrentHashMap<String, DefaultRealtimeObject>() | ||
|
|
||
| override fun getInstance(channelName: String): RealtimeObject { | ||
| return objects.getOrPut(channelName) { DefaultRealtimeObject(channelName, adapter) } | ||
| } | ||
|
|
||
| override fun handle(msg: ProtocolMessage) { | ||
| val channelName = msg.channel | ||
| objects[channelName]?.handle(msg) | ||
| } | ||
|
|
||
| override fun handleStateChange(channelName: String, state: ChannelState, hasObjects: Boolean) { | ||
| objects[channelName]?.handleStateChange(state, hasObjects) | ||
| } | ||
|
|
||
| override fun dispose(channelName: String) { | ||
| objects.remove(channelName) | ||
| ?.dispose(clientError("Channel has been released using channels.release()")) | ||
| } | ||
|
|
||
| override fun dispose() { | ||
| objects.values.forEach { | ||
| it.dispose(clientError("AblyClient has been closed using client.close()")) | ||
| } | ||
| objects.clear() | ||
| } | ||
| } | ||
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.
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.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: ably/ably-java
Length of output: 14405
🏁 Script executed:
Repository: ably/ably-java
Length of output: 8199
🌐 Web query:
Kotlin stdlib ConcurrentMap getOrPut defaultValue may be invoked even if key is already in the map documentation💡 Result:
Yes, it is explicitly documented that the defaultValue function for ConcurrentMap.getOrPut in the Kotlin standard library may be invoked even if the key is already present in the map [1][2][3]. The official Kotlin documentation states: "This method guarantees not to put the value into the map if the key is already there, but the defaultValue function may be invoked even if the key is already in the map" [2][4][3]. This behavior occurs because of the implementation of the function [1][3]: public inline fun <K, V> ConcurrentMap<K, V>.getOrPut(key: K, defaultValue: -> V): V { // Do not use computeIfAbsent on JVM8 as it would change locking behavior return this.get(key)?: defaultValue.let { default -> this.putIfAbsent(key, default)?: default } } As shown above, the function first attempts to retrieve the value using get(key) [1][3]. If the key is missing or mapped to null, it proceeds to invoke the defaultValue function before attempting to put the result into the map using putIfAbsent [1][3]. Because this check and the subsequent invocation are not performed within a single atomic operation, the defaultValue function may be called unnecessarily if another thread inserts the key into the map after the initial get(key) call but before the defaultValue function is executed [5].
Citations:
🏁 Script executed:
Repository: ably/ably-java
Length of output: 1287
🏁 Script executed:
Repository: ably/ably-java
Length of output: 3505
🏁 Script executed:
Repository: ably/ably-java
Length of output: 302
Avoid
getOrPuthere; concurrent first-time calls can start and drop extraDefaultRealtimeObjectinstances.ConcurrentMap.getOrPutmay invoke the factory even when another thread wins the race, andDefaultRealtimeObjectstarts a long-lived coroutine ininit. That leaves losing instances undisposed. UsecomputeIfAbsentso only one object is constructed per channel.🤖 Prompt for AI Agents