Skip to content

Commit

Permalink
Restore code coming from element about ReportedDecryptionFailurePersi…
Browse files Browse the repository at this point in the history
…stence
  • Loading branch information
yostyle committed Jul 31, 2024
1 parent faf9209 commit 98c391a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private const val MAX_WAIT_MILLIS = 60_000
class DecryptionFailureTracker @Inject constructor(
private val analyticsTracker: AnalyticsTracker,
private val sessionDataSource: ActiveSessionDataSource,
private val decryptionFailurePersistence: ReportedDecryptionFailurePersistence,
private val clock: Clock
) : Session.Listener, LiveEventListener {

Expand All @@ -76,9 +77,6 @@ class DecryptionFailureTracker @Inject constructor(
// Only accessed on a `post` call, ensuring sequential access
private val trackedEventsMap = mutableMapOf<String, DecryptionFailure>()

// List of eventId that have been reported, to avoid double reporting
private val alreadyReported = mutableListOf<String>()

// Mutex to ensure sequential access to internal state
private val mutex = Mutex()

Expand All @@ -98,10 +96,16 @@ class DecryptionFailureTracker @Inject constructor(
this.scope = scope
}
observeActiveSession()
post {
decryptionFailurePersistence.load()
}
}

fun stop() {
Timber.v("Stop DecryptionFailureTracker")
post {
decryptionFailurePersistence.persist()
}
activeSessionSourceDisposable.cancel(CancellationException("Closing DecryptionFailureTracker"))

activeSession?.removeListener(this)
Expand All @@ -123,6 +127,7 @@ class DecryptionFailureTracker @Inject constructor(
delay(CHECK_INTERVAL)
post {
checkFailures()
decryptionFailurePersistence.persist()
currentTicker = null
if (trackedEventsMap.isNotEmpty()) {
// Reschedule
Expand All @@ -137,15 +142,15 @@ class DecryptionFailureTracker @Inject constructor(
.distinctUntilChanged()
.onEach {
Timber.v("Active session changed ${it.getOrNull()?.myUserId}")
it.orNull()?.let { session ->
it.getOrNull()?.let { session ->
post {
onSessionActive(session)
}
}
}.launchIn(scope)
}

private fun onSessionActive(session: Session) {
private suspend fun onSessionActive(session: Session) {
Timber.v("onSessionActive ${session.myUserId} previous: ${activeSession?.myUserId}")
val sessionId = session.sessionId
if (sessionId == activeSession?.sessionId) {
Expand Down Expand Up @@ -202,7 +207,8 @@ class DecryptionFailureTracker @Inject constructor(
// already tracked
return
}
if (alreadyReported.contains(eventId)) {
if (decryptionFailurePersistence.hasBeenReported(eventId)) {
Timber.v("Event $eventId already reported")
// already reported
return
}
Expand Down Expand Up @@ -237,7 +243,7 @@ class DecryptionFailureTracker @Inject constructor(
}
}

private fun handleEventDecrypted(eventId: String) {
private suspend fun handleEventDecrypted(eventId: String) {
Timber.v("Handle event decrypted $eventId time: ${clock.epochMillis()}")
// Only consider if it was tracked as a failure
val trackedFailure = trackedEventsMap[eventId] ?: return
Expand Down Expand Up @@ -270,7 +276,7 @@ class DecryptionFailureTracker @Inject constructor(
}

// This will mutate the trackedEventsMap, so don't call it while iterating on it.
private fun reportFailure(decryptionFailure: DecryptionFailure) {
private suspend fun reportFailure(decryptionFailure: DecryptionFailure) {
Timber.v("Report failure for event ${decryptionFailure.failedEventId}")
val error = decryptionFailure.toAnalyticsEvent()

Expand All @@ -279,10 +285,10 @@ class DecryptionFailureTracker @Inject constructor(
// now remove from tracked
trackedEventsMap.remove(decryptionFailure.failedEventId)
// mark as already reported
alreadyReported.add(decryptionFailure.failedEventId)
decryptionFailurePersistence.markAsReported(decryptionFailure.failedEventId)
}

private fun checkFailures() {
private suspend fun checkFailures() {
val now = clock.epochMillis()
Timber.v("Check failures now $now")
// report the definitely failed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package im.vector.app.features.analytics

import android.content.Context
import android.util.LruCache
import com.google.common.hash.BloomFilter
import com.google.common.hash.Funnels
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.checkerframework.com.google.common.hash.BloomFilter
import org.checkerframework.com.google.common.hash.Funnels
import timber.log.Timber
import java.io.File
import java.io.FileOutputStream
Expand Down

0 comments on commit 98c391a

Please sign in to comment.