@@ -6,6 +6,8 @@ import com.segment.analytics.kotlin.core.platform.Plugin
66import com.segment.analytics.kotlin.core.platform.VersionedPlugin
77import com.segment.analytics.kotlin.core.platform.plugins.logger.*
88import com.segment.analytics.kotlin.core.utilities.putIntegrations
9+ import java.util.concurrent.locks.ReentrantLock
10+ import kotlin.concurrent.withLock
911
1012// A Destination plugin that adds session tracking to Amplitude cloud mode.
1113class AmplitudeSession (private val sessionTimeoutMs : Long = 300000 ) : EventPlugin, VersionedPlugin {
@@ -19,6 +21,7 @@ class AmplitudeSession (private val sessionTimeoutMs : Long = 300000) : EventPlu
1921 private val ampSessionStartEvent = " session_start"
2022 private var active = false
2123 private var lastEventFiredTime = java.lang.System .currentTimeMillis()
24+ private var sessionUpdateLock = ReentrantLock ()
2225
2326 override fun setup (analytics : Analytics ) {
2427 super .setup(analytics)
@@ -80,20 +83,23 @@ class AmplitudeSession (private val sessionTimeoutMs : Long = 300000) : EventPlu
8083 }
8184
8285 private fun startNewSessionIfNecessary () {
83- val current = java.lang.System .currentTimeMillis()
84- // Make sure the first event has a valid ID and we send a session start.
85- // Subsequent events should have session IDs updated after the session track event is sent
86- if (eventSessionId == - 1L || sessionId == - 1L ) {
87- sessionId = current
88- eventSessionId = current
89- startNewSession()
90- } else if (current - lastEventFiredTime >= sessionTimeoutMs) {
91- sessionId = current
92-
93- endSession()
94- startNewSession()
86+ sessionUpdateLock.withLock {
87+ val current = java.lang.System .currentTimeMillis()
88+ // Make sure the first event has a valid ID and we send a session start.
89+ // Subsequent events should have session IDs updated after the session track event is sent
90+ if (eventSessionId == - 1L || sessionId == - 1L ) {
91+ sessionId = current
92+ eventSessionId = current
93+ lastEventFiredTime = current
94+ startNewSession()
95+ } else if (current - lastEventFiredTime >= sessionTimeoutMs) {
96+ sessionId = current
97+ lastEventFiredTime = current
98+
99+ endSession()
100+ startNewSession()
101+ }
95102 }
96- lastEventFiredTime = current
97103 }
98104
99105 override fun version (): String {
0 commit comments