mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-11 07:29:14 +01:00
Harden the pending-action queue after review
- Order direct stars against the queue so a successful online unstar is no longer overwritten by an older queued star, and serialize all queue mutations under one lock. - Treat a SubsonicException with a negative code as a transport failure: queue it in setStar and keep it on flush instead of dropping it. - Make coalescing atomic, keep the offline collector alive across errors, and drain from the persisted offline flag so a queued action flushes on a cold start that is already online. - Give ServerActionSink.flush a default body and drop the no-op overrides.
This commit is contained in:
@@ -3,6 +3,7 @@ package com.subtracks.data.db
|
||||
import androidx.room3.Dao
|
||||
import androidx.room3.Insert
|
||||
import androidx.room3.Query
|
||||
import androidx.room3.Transaction
|
||||
import com.subtracks.data.model.PendingAction
|
||||
|
||||
@Dao
|
||||
@@ -25,4 +26,10 @@ interface PendingActionDao {
|
||||
starType: String,
|
||||
targetId: String,
|
||||
)
|
||||
|
||||
@Transaction
|
||||
suspend fun replace(action: PendingAction) {
|
||||
if (action.starType != null) clearStar(action.sourceId, action.starType, action.targetId)
|
||||
insert(action)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,4 @@ class NetworkServerActionSink(
|
||||
val source = sources.activeMusicSource() ?: throw IllegalStateException("No active server")
|
||||
source.setStar(type, id, starred)
|
||||
}
|
||||
|
||||
override suspend fun flush() = Unit
|
||||
}
|
||||
|
||||
@@ -8,58 +8,54 @@ import com.subtracks.data.source.StarType
|
||||
import com.subtracks.data.source.subsonic.SubsonicException
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.drop
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import java.io.IOException
|
||||
|
||||
class PendingServerActionSink(
|
||||
private val delegate: ServerActionSink,
|
||||
private val db: SubtracksDatabase,
|
||||
private val activeSourceId: suspend () -> Long?,
|
||||
private val offline: StateFlow<Boolean>,
|
||||
offlinePreferences: Flow<Boolean>,
|
||||
scope: CoroutineScope,
|
||||
) : ServerActionSink {
|
||||
private val flushLock = Mutex()
|
||||
private val lock = Mutex()
|
||||
|
||||
init {
|
||||
scope.launch {
|
||||
// The first emission is the StateFlow's default, not necessarily the persisted value, and
|
||||
// replaying onto the network must not race a cold start with offline mode on.
|
||||
offline
|
||||
.drop(1)
|
||||
.collect { isOffline -> if (!isOffline) flush() }
|
||||
offlinePreferences.collect { isOffline ->
|
||||
// The persisted value is authoritative, unlike the process flag's default at cold
|
||||
// start, so wait for that flag to agree before replaying.
|
||||
if (!isOffline) {
|
||||
offline.first { !it }
|
||||
flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun nowPlaying(songId: String) {
|
||||
if (offline.value) return
|
||||
try {
|
||||
delegate.nowPlaying(songId)
|
||||
flush()
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
if (attempt { delegate.nowPlaying(songId) } == null) flush()
|
||||
}
|
||||
|
||||
override suspend fun scrobble(
|
||||
songId: String,
|
||||
time: Long,
|
||||
) {
|
||||
if (offline.value) {
|
||||
queue(PendingActionKind.Scrobble, songId, null, time)
|
||||
return
|
||||
}
|
||||
try {
|
||||
delegate.scrobble(songId, time)
|
||||
flush()
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (_: Exception) {
|
||||
queue(PendingActionKind.Scrobble, songId, null, time)
|
||||
lock.withLock {
|
||||
if (offline.value) {
|
||||
enqueueLocked(PendingActionKind.Scrobble, songId, null, time)
|
||||
return
|
||||
}
|
||||
when (val failure = attempt { delegate.scrobble(songId, time) }) {
|
||||
null -> drainSafelyLocked()
|
||||
else -> if (isRetryable(failure)) enqueueLocked(PendingActionKind.Scrobble, songId, null, time)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,41 +65,58 @@ class PendingServerActionSink(
|
||||
starred: Boolean,
|
||||
) {
|
||||
val kind = if (starred) PendingActionKind.Star else PendingActionKind.Unstar
|
||||
if (offline.value) {
|
||||
if (!queue(kind, id, type.name, 0)) throw IllegalStateException("No active server")
|
||||
return
|
||||
}
|
||||
try {
|
||||
delegate.setStar(type, id, starred)
|
||||
flush()
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (rejected: SubsonicException) {
|
||||
// The server answered and refused; retrying the same request will not help.
|
||||
throw rejected
|
||||
} catch (failure: Exception) {
|
||||
if (!queue(kind, id, type.name, 0)) throw failure
|
||||
lock.withLock {
|
||||
if (offline.value) {
|
||||
if (!enqueueLocked(kind, id, type.name, 0)) throw IllegalStateException("No active server")
|
||||
return
|
||||
}
|
||||
when (val failure = attempt { delegate.setStar(type, id, starred) }) {
|
||||
null -> {
|
||||
// The absolute state just sent wins; a queued star for the same item is older.
|
||||
clearStarLocked(type.name, id)
|
||||
drainSafelyLocked()
|
||||
}
|
||||
|
||||
else -> {
|
||||
if (!isRetryable(failure) || !enqueueLocked(kind, id, type.name, 0)) throw failure
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun flush() {
|
||||
flushLock.withLock {
|
||||
if (offline.value) return
|
||||
val sourceId = activeSourceId() ?: return
|
||||
val dao = db.pendingActionDao()
|
||||
for (action in dao.pending(sourceId)) {
|
||||
try {
|
||||
deliver(action)
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (_: IOException) {
|
||||
// Still unreachable: keep this and the remaining actions for the next attempt.
|
||||
return
|
||||
} catch (_: Exception) {
|
||||
// The server answered and refused; drop the action rather than block the queue.
|
||||
}
|
||||
dao.delete(action.id)
|
||||
try {
|
||||
lock.withLock { drainLocked() }
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun drainLocked() {
|
||||
if (offline.value) return
|
||||
val sourceId = activeSourceId() ?: return
|
||||
val dao = db.pendingActionDao()
|
||||
for (action in dao.pending(sourceId)) {
|
||||
try {
|
||||
deliver(action)
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (failure: Exception) {
|
||||
// Still unreachable: keep this and the remaining actions for the next attempt.
|
||||
if (isRetryable(failure)) return
|
||||
// The server answered and refused; drop the action rather than block the queue.
|
||||
}
|
||||
dao.delete(action.id)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun drainSafelyLocked() {
|
||||
try {
|
||||
drainLocked()
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,16 +133,38 @@ class PendingServerActionSink(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun queue(
|
||||
private suspend fun enqueueLocked(
|
||||
kind: PendingActionKind,
|
||||
targetId: String,
|
||||
starType: String?,
|
||||
time: Long,
|
||||
): Boolean {
|
||||
val sourceId = activeSourceId() ?: return false
|
||||
val dao = db.pendingActionDao()
|
||||
if (starType != null) dao.clearStar(sourceId, starType, targetId)
|
||||
dao.insert(PendingAction(sourceId = sourceId, kind = kind, targetId = targetId, starType = starType, time = time))
|
||||
db.pendingActionDao().replace(
|
||||
PendingAction(sourceId = sourceId, kind = kind, targetId = targetId, starType = starType, time = time),
|
||||
)
|
||||
return true
|
||||
}
|
||||
|
||||
private suspend fun clearStarLocked(
|
||||
starType: String,
|
||||
targetId: String,
|
||||
) {
|
||||
val sourceId = activeSourceId() ?: return
|
||||
db.pendingActionDao().clearStar(sourceId, starType, targetId)
|
||||
}
|
||||
|
||||
private suspend fun attempt(block: suspend () -> Unit): Exception? =
|
||||
try {
|
||||
block()
|
||||
null
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (failure: Exception) {
|
||||
failure
|
||||
}
|
||||
|
||||
// A SubsonicException with a negative code is a transport or protocol failure (HTTP non-2xx,
|
||||
// malformed body), not a refusal, so it is still worth retrying.
|
||||
private fun isRetryable(failure: Exception): Boolean = failure !is SubsonicException || failure.code < 0
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@ interface ServerActionSink {
|
||||
starred: Boolean,
|
||||
)
|
||||
|
||||
suspend fun flush()
|
||||
suspend fun flush() = Unit
|
||||
}
|
||||
|
||||
@@ -84,6 +84,7 @@ fun appModule(
|
||||
db = get(),
|
||||
activeSourceId = get<SourceRepository>()::activeSourceIdOnce,
|
||||
offline = get<SourceRepository>().offline,
|
||||
offlinePreferences = get<UserPreferences>().offlineMode(),
|
||||
scope = get(),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user