M9 — Device diagnostics & debug reporting (connectivity + UPnP desync) #104
+11
-8
@@ -70,8 +70,10 @@ class DiagnosticsUploader @Inject constructor(
|
|||||||
|
|
||||||
/** Coalesces concurrent triggers via tryLock — a drain in flight wins. */
|
/** Coalesces concurrent triggers via tryLock — a drain in flight wins. */
|
||||||
private suspend fun drainSafe() {
|
private suspend fun drainSafe() {
|
||||||
if (authStore.sessionCookie.value.isNullOrEmpty()) return
|
val clientId = authStore.clientId.value
|
||||||
val clientId = authStore.clientId.value ?: return
|
// Signed out / no client id yet → nothing to do. Single guard keeps
|
||||||
|
// the return count within the detekt gate.
|
||||||
|
if (authStore.sessionCookie.value.isNullOrEmpty() || clientId == null) return
|
||||||
if (!mutex.tryLock()) return
|
if (!mutex.tryLock()) return
|
||||||
try {
|
try {
|
||||||
drain(clientId)
|
drain(clientId)
|
||||||
@@ -81,13 +83,14 @@ class DiagnosticsUploader @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun drain(clientId: String) {
|
private suspend fun drain(clientId: String) {
|
||||||
while (true) {
|
var more = true
|
||||||
|
while (more) {
|
||||||
val batch = runCatching { dao.takeBatch(BATCH_SIZE) }.getOrNull().orEmpty()
|
val batch = runCatching { dao.takeBatch(BATCH_SIZE) }.getOrNull().orEmpty()
|
||||||
if (batch.isEmpty()) return
|
val sent = batch.isNotEmpty() && runCatching { upload(clientId, batch) }.isSuccess
|
||||||
val sent = runCatching { upload(clientId, batch) }.isSuccess
|
if (sent) runCatching { dao.deleteByIds(batch.map { it.id }) }
|
||||||
if (!sent) return // leave rows for the next trigger
|
// Keep going only while a full batch sent cleanly; otherwise stop
|
||||||
runCatching { dao.deleteByIds(batch.map { it.id }) }
|
// (empty buffer, or a failure to retry on the next trigger).
|
||||||
if (batch.size < BATCH_SIZE) return
|
more = sent && batch.size >= BATCH_SIZE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user