Skip to content

Commit

Permalink
wrap tokens.get key i mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
rtc11 committed Jan 24, 2024
1 parent ddd1e5e commit 8488e8e
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ktor-auth-azuread/main/no/nav/aap/ktor/client/Token.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package no.nav.aap.ktor.client
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.time.Instant
import java.util.concurrent.ConcurrentHashMap

internal data class Token(val expires_in: Long, val access_token: String) {
private val expiry: Instant = Instant.now().plusSeconds(expires_in - LEEWAY_SECONDS)
Expand All @@ -25,11 +26,14 @@ internal class TokenCache<K> {
}

internal suspend fun get(key: K): Token? {
tokens[key]?.let {
mutex.withLock {
tokens[key]
}?.let {
if (it.expired()) {
rm(key)
}
}

return mutex.withLock {
tokens[key]
}
Expand Down

0 comments on commit 8488e8e

Please sign in to comment.