Skip to content

Commit ac026ec

Browse files
authored
Use a CopyOnWriteArrayList in Mediator instead of synchronize {}. (#141)
1 parent 108a98f commit ac026ec

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/platform/Mediator.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ import com.segment.analytics.kotlin.core.Analytics
44
import com.segment.analytics.kotlin.core.BaseEvent
55
import com.segment.analytics.kotlin.core.platform.plugins.logger.LogFilterKind
66
import com.segment.analytics.kotlin.core.platform.plugins.logger.segmentLog
7+
import java.util.concurrent.CopyOnWriteArrayList
78
import kotlin.reflect.KClass
89

910
// Platform abstraction for managing plugins' execution (of a specific type)
10-
// All operations are thread safe via the `synchronized` function
11-
internal class Mediator(internal val plugins: MutableList<Plugin>) {
11+
// All operations are thread safe via the CopyOnWriteArrayList. Which allows multiple
12+
// threads to read the list but when a modification is made the modifier is given a new copy of
13+
// list and that becomes the new version of the list.
14+
// More info: https://developer.android.com/reference/kotlin/java/util/concurrent/CopyOnWriteArrayList
15+
internal class Mediator(internal val plugins: CopyOnWriteArrayList<Plugin>) {
1216

13-
fun add(plugin: Plugin) = synchronized(plugins) {
17+
fun add(plugin: Plugin) {
1418
plugins.add(plugin)
1519
}
1620

17-
fun remove(plugin: Plugin) = synchronized(plugins) {
21+
fun remove(plugin: Plugin) {
1822
plugins.removeAll { it === plugin } // remove only if reference is the same
1923
}
2024

21-
fun execute(event: BaseEvent): BaseEvent? = synchronized(plugins) {
25+
fun execute(event: BaseEvent): BaseEvent? {
2226
var result: BaseEvent? = event
2327

2428
plugins.forEach { plugin ->
@@ -42,13 +46,13 @@ internal class Mediator(internal val plugins: MutableList<Plugin>) {
4246
return result
4347
}
4448

45-
fun applyClosure(closure: (Plugin) -> Unit) = synchronized(plugins) {
49+
fun applyClosure(closure: (Plugin) -> Unit) {
4650
plugins.forEach {
4751
closure(it)
4852
}
4953
}
5054

51-
fun <T : Plugin> find(pluginClass: KClass<T>): T? = synchronized(plugins) {
55+
fun <T : Plugin> find(pluginClass: KClass<T>): T? {
5256
plugins.forEach {
5357
if (pluginClass.isInstance(it)) {
5458
return it as T
@@ -57,7 +61,7 @@ internal class Mediator(internal val plugins: MutableList<Plugin>) {
5761
return null
5862
}
5963

60-
fun <T : Plugin> findAll(pluginClass: KClass<T>): List<T> = synchronized(plugins) {
64+
fun <T : Plugin> findAll(pluginClass: KClass<T>): List<T> {
6165
return plugins.filter { pluginClass.isInstance(it) } as List<T>
6266
}
6367
}

core/src/main/java/com/segment/analytics/kotlin/core/platform/Timeline.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import com.segment.analytics.kotlin.core.Analytics
44
import com.segment.analytics.kotlin.core.BaseEvent
55
import com.segment.analytics.kotlin.core.System
66
import kotlinx.coroutines.launch
7+
import java.util.concurrent.CopyOnWriteArrayList
78
import kotlin.reflect.KClass
89

910
// Platform abstraction for managing all plugins and their execution
1011
// Currently the execution follows
1112
// Before -> Enrichment -> Destination -> After
1213
internal class Timeline {
1314
internal val plugins: Map<Plugin.Type, Mediator> = mapOf(
14-
Plugin.Type.Before to Mediator(mutableListOf()),
15-
Plugin.Type.Enrichment to Mediator(mutableListOf()),
16-
Plugin.Type.Destination to Mediator(mutableListOf()),
17-
Plugin.Type.After to Mediator(mutableListOf()),
18-
Plugin.Type.Utility to Mediator(mutableListOf())
15+
Plugin.Type.Before to Mediator(CopyOnWriteArrayList()),
16+
Plugin.Type.Enrichment to Mediator(CopyOnWriteArrayList()),
17+
Plugin.Type.Destination to Mediator(CopyOnWriteArrayList()),
18+
Plugin.Type.After to Mediator(CopyOnWriteArrayList()),
19+
Plugin.Type.Utility to Mediator(CopyOnWriteArrayList())
1920
)
2021
lateinit var analytics: Analytics
2122

0 commit comments

Comments
 (0)