mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-25 12:01:45 +02:00
Abandoning the precision of Mills to seconds on the isOnline Check
This commit is contained in:
@@ -24,6 +24,7 @@ import android.util.Log
|
||||
import android.util.LruCache
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.utils.RandomInstance
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import okhttp3.EventListener
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Protocol
|
||||
@@ -33,29 +34,37 @@ import okio.ByteString.Companion.toByteString
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
|
||||
@Immutable data class OnlineCheckResult(
|
||||
val timeInMs: Long,
|
||||
val timeInSecs: Long,
|
||||
val online: Boolean,
|
||||
)
|
||||
|
||||
object OnlineChecker {
|
||||
val checkOnlineCache = LruCache<String, OnlineCheckResult>(100)
|
||||
val fiveMinutes = 1000 * 60 * 5
|
||||
|
||||
fun isOnlineCached(url: String?): Boolean {
|
||||
if (url.isNullOrBlank()) return false
|
||||
if ((checkOnlineCache.get(url)?.timeInMs ?: 0) > System.currentTimeMillis() - fiveMinutes) {
|
||||
return checkOnlineCache.get(url).online
|
||||
val cached = checkOnlineCache.get(url)
|
||||
if (cached != null && cached.timeInSecs > TimeUtils.fiveMinutesAgo()) {
|
||||
return cached.online
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun resetIfOfflineToRetry(url: String) {
|
||||
val cached = checkOnlineCache.get(url)
|
||||
if (cached != null && !cached.online) {
|
||||
checkOnlineCache.remove(url)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun isOnline(
|
||||
url: String?,
|
||||
okHttpClient: (String) -> OkHttpClient,
|
||||
): Boolean {
|
||||
if (url.isNullOrBlank()) return false
|
||||
if ((checkOnlineCache.get(url)?.timeInMs ?: 0) > System.currentTimeMillis() - fiveMinutes) {
|
||||
return checkOnlineCache.get(url).online
|
||||
val cached = checkOnlineCache.get(url)
|
||||
if (cached != null && cached.timeInSecs > TimeUtils.fiveMinutesAgo()) {
|
||||
return cached.online
|
||||
}
|
||||
|
||||
return try {
|
||||
@@ -89,14 +98,16 @@ object OnlineChecker {
|
||||
.build()
|
||||
val client = okHttpClient(url)
|
||||
|
||||
client.newCall(request).executeAsync().use { it.isSuccessful }
|
||||
client.newCall(request).executeAsync().use {
|
||||
it.isSuccessful
|
||||
}
|
||||
}
|
||||
|
||||
checkOnlineCache.put(url, OnlineCheckResult(System.currentTimeMillis(), result))
|
||||
checkOnlineCache.put(url, OnlineCheckResult(TimeUtils.now(), result))
|
||||
result
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
checkOnlineCache.put(url, OnlineCheckResult(System.currentTimeMillis(), false))
|
||||
checkOnlineCache.put(url, OnlineCheckResult(TimeUtils.now(), false))
|
||||
Log.e("LiveActivities", "Failed to check streaming url $url", e)
|
||||
false
|
||||
}
|
||||
|
Reference in New Issue
Block a user