Spot the bug
Under load, the server sees many refresh calls at once. Which line lets that happen?
class TokenAuthenticator(private val store: TokenStore, private val auth: AuthApi) : Authenticator {override fun authenticate(route: Route?, response: okhttp3.Response): Request? {val fresh = auth.refreshBlocking(store.refreshToken) ?: return nullstore.accessToken = freshreturn response.request.newBuilder().header("Authorization", "Bearer $fresh").build()}}
Hint 1 of 1
What happens when five threads reach this line together?
Choose an answer, then open Show answer below to check it.
Show answer
- Answer: line 3
val fresh = auth.refreshBlocking(store.refreshToken) ?: return null
Every 401 refreshes on its own. Wrap this in synchronized(this), and first check whether store.accessToken already differs from the token on the failed request; if so, retry with it.