Updated on 2026-08-14
This commit is contained in:
parent
1bb825744e
commit
92e21cc79c
5 changed files with 62 additions and 26 deletions
|
|
@ -13,6 +13,7 @@ import kotlinx.coroutines.sync.Mutex
|
|||
import kotlinx.coroutines.sync.withLock
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
|
|
@ -32,7 +33,7 @@ object Analytics : GlobalAnalyticsEventHandler {
|
|||
|
||||
private val handlers = mutableMapOf<String, AnalyticsHandler>()
|
||||
private val paramsInterceptors = ConcurrentHashMap<String, ParamsInterceptor>()
|
||||
private val oneEventsPerSession = ConcurrentHashMap<String, Boolean>()
|
||||
private val throttledEventsState = ConcurrentHashMap<String, Long>()
|
||||
private val analyticsFilters = mutableSetOf<AnalyticsEventFilter>()
|
||||
private val analyticsMutex = Mutex()
|
||||
|
||||
|
|
@ -87,9 +88,7 @@ object Analytics : GlobalAnalyticsEventHandler {
|
|||
|
||||
override fun send(event: AnalyticsEvent) {
|
||||
analyticsScope.launch {
|
||||
if (event is OneTimePerSessionEvent &&
|
||||
oneEventsPerSession.putIfAbsent(event.oneTimeEventId, true) != null
|
||||
) {
|
||||
if (event is OneTimePerSessionEvent && !shouldSendThrottledEvent(event)) {
|
||||
return@launch
|
||||
}
|
||||
event.params = applyParamsInterceptors(event)
|
||||
|
|
@ -137,6 +136,21 @@ object Analytics : GlobalAnalyticsEventHandler {
|
|||
return interceptedParams
|
||||
}
|
||||
|
||||
private fun shouldSendThrottledEvent(event: OneTimePerSessionEvent): Boolean {
|
||||
val now = System.currentTimeMillis()
|
||||
val id = event.oneTimeEventId
|
||||
return when (val throttleMs = event.throttleSeconds?.let(TimeUnit.SECONDS::toMillis)) {
|
||||
null -> throttledEventsState.putIfAbsent(id, now) == null
|
||||
else -> throttledEventsState.compute(id) { _, lastSendTime ->
|
||||
when {
|
||||
lastSendTime == null -> now
|
||||
now - lastSendTime >= throttleMs -> now
|
||||
else -> lastSendTime
|
||||
}
|
||||
} == now
|
||||
}
|
||||
}
|
||||
|
||||
private fun createScope(): CoroutineScope {
|
||||
val name = "Analytics"
|
||||
val dispatcher = Executors.newFixedThreadPool(1).asCoroutineDispatcher()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue