https://kotlinlang.org logo
c

Colton Idle

04/25/2022, 2:40 AM
My app has a typical OAuth flow in terms of, 401 comes into the app, the app (via an interceptor) detects this 401, and tries to exchange the refresh token for a new set of auth and refresh tokens. This works great with okhttp interceptors... but I've noticed in cases where the user comes back to the app and 2 network calls are made at the same, I will get two 401s, and therefore (unfortunately) my interceptor will trigger twice. Anyone have any guidance/experience around this. My initial reaction is to write some sort of state to my user
isAuthRefreshInProgress
and short-circuit the interceptor if it's already in progress, but then I'm not sure how I'd wait for the auth refresh to be complete, so I can make that second call again. Here's a gist of my current interceptor.
e

ephemient

04/25/2022, 4:32 AM
if a second call to refresh comes in while the first is still processing, wouldn't you prefer to wait for it to finish and use its result?
p

Paul Woitaschek

04/25/2022, 4:33 AM
Our auth code is ancient but we have synchronized on the authenticators body
e

ephemient

04/25/2022, 4:53 AM
perhaps with something like this. untested but it should convey the idea.
start=LAZY
because
updateAndGet
can be restarted on conflict,
GlobalScope
because it doesn't belong to the caller's scope if it's shared
c

Colton Idle

04/25/2022, 6:13 AM
@Paul Woitaschek yeah I thought the synchronized I had in my gist would be enough, but I guess right now I'm likely to do a "short circuit" check in the very first line of the synchronized block so that I don't continue if I already got a new auth + refresh token set. @ephemient I will have to play around with your code a bit as a lot of it is admittedly going over my head. I guess in a general sense it doesn't seem like interceptors really have anything built in to help with this situation?
e

ephemient

04/25/2022, 6:17 AM
interceptors are pretty much independent of each other and AFAIK there's nothing built-in to provide synchronization
c

Colton Idle

04/25/2022, 1:46 PM
Cool cool. Okay. I think just checking this new state property, and short circuiting should be enough then. I do have to try to play around with my use of `synchronized`as I don't fully understand it, but I hope that it actually makes my parallel interceptors work in serial.
2 Views