Browse Source

use newSingleThreadContext instead of mutex for token refresh

fixes #57
develop
Jannik 1 month ago
parent
commit
bc5509cf93
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 3
      app/build.gradle
  2. 8
      app/src/main/java/org/mosad/teapod/parser/crunchyroll/Crunchyroll.kt

3
app/build.gradle

@ -38,6 +38,9 @@ android {
}
kotlinOptions {
jvmTarget = '1.8'
kotlin.sourceSets.all {
languageSettings.optIn("kotlin.RequiresOptIn")
}
}
namespace 'org.mosad.teapod'
}

8
app/src/main/java/org/mosad/teapod/parser/crunchyroll/Crunchyroll.kt

@ -33,8 +33,6 @@ import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
@ -59,7 +57,8 @@ object Crunchyroll {
private lateinit var token: Token
private var tokenValidUntil: Long = 0
private val tokeRefreshMutex = Mutex()
@OptIn(DelicateCoroutinesApi::class)
private val tokenRefreshContext = newSingleThreadContext("TokenRefreshContext")
private var accountID = ""
@ -141,8 +140,7 @@ object Crunchyroll {
params: List<Pair<String, Any?>> = listOf(),
bodyObject: Any = Any()
): T = coroutineScope {
// TODO find a better way to make token refresh thread safe, currently it's blocking
tokeRefreshMutex.withLock {
withContext(tokenRefreshContext) {
if (System.currentTimeMillis() > tokenValidUntil) refreshToken()
}

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close